Jsonobject class Getinteger function Value exception

Source: Internet
Author: User

Product Experiment Bureau environment colleagues found that the distribution map and the actual business is not, because the page shows the large traffic rtsp, the customer does not have the relevant app business. At first thought it was a problem with the front-end business logic:

View the front-end database query code, the logic is very simple, get the value of the app field from the database, and then corresponding to the app dictionary, front-end display:

App_dict = get_app_dict () result = {"List": {}}if len (data)! = 0:for I in range (len data):     if data[i][1] in app_dict:< c1/>result["List"][app_dict[data[i][1]] [data[i][0]

Partial Dictionary Example:

    <PlatformDisplay_name= "RTSP"ID= "1"name= "RTSP"Name_en_us= "RTSP"NAME_ZH_CN= "RTSP"NAME_ZH_TW= "RTSP">      <appcategory= "Media"Display_name= "RTSP"ID= "9209509394251777"name= "RTSP"Name_en_us= "RTSP"NAME_ZH_CN= "RTSP"NAME_ZH_TW= "RTSP"Risk= "1"Subcategory= "Photo-video"Tags= "exband|tunnel|widely"Technology= "CS"/>    </Platform>    <PlatformDisplay_name= "United audiences"ID= "2"name= "United audiences"Name_en_us= "Ourgame"NAME_ZH_CN= "United audiences"NAME_ZH_TW= "The Audience">      <appcategory= "Media"Display_name= "Lianzhong Game"ID= "1354598325420033"name= "Lianzhong Game"Name_en_us= "Ourgame"NAME_ZH_CN= "Lianzhong Game"NAME_ZH_TW= "The Public game"Risk= "1"Subcategory= "Game"Tags= "exband|widely"Technology= "BS"/>      <appcategory= "CO"Display_name= "United Friends"ID= "1354598325420034"name= "United Friends"Name_en_us= "Ourfriend"NAME_ZH_CN= "United Friends"NAME_ZH_TW= "Friends of the audience"Risk= "1"Subcategory= "Im"Tags="

Front-end no problem, it must be data logic statistics have a problem, to see the data in hive is normal, the original data in the Kafka is also normal, speculation may be written in PostgreSQL logic problems.

Database This table other fields are not wrong, this field is wrong value:

ID | Alert_time | Process_time | protocol | App | App_proto | Bytesall | Packetsall
---------+------------+--------------+----------+-----+-----------+------------+------------
897737 | 1527704580 | 1527704640 | 6 | 2 | 100 | 7150 | 32
897772 | 1527704520 | 1527704700 | 6 | 0 | 8 | 6966 | 112
897773 | 1527704640 | 1527704700 | 6 | 0 | 8 | 1344022 | 2465
897774 | 1527704580 | 1527704700 | 6 | 0 | 8 | 232312 | 864
897775 | 1527704340 | 1527704700 | 6 | 0 | 8 | 56384 | 166
897776 | 1527704640 | 1527704700 | 17 | 4 | 31 | 552 | 6
897777 | 1527704700 | 1527704700 | 6 | 0 | 8 | 107814 | 776
897778 | 1527704400 | 1527704700 | 6 | 0 | 8 | 12524 | 52

Look at the code of the data write database, find the app this value defines int, before if the data exceeds int, should be overflow to negative value, why give a 0-10 integer values?

Jsonobject newObject = new Jsonobject ();
Newobject.put ("tablename", key);
For (string[] field:fieldlist) {
FieldName = field[0];
FieldType = field[1];
if (Fieldtype.equals ("String")) {
Newobject.put (fieldname,obj.getstring (fieldName));
}else if (fieldtype.equals ("Long")) {
Newobject.put (Fieldname,obj.getlong (fieldName));
}else{
Newobject.put (Fieldname,obj.getinteger (fieldName));
}
}
Return newobject.tojsonstring (). GetBytes ();

Here, the value of the parameter is obtained by Jsonobject, and if it is of type int, the value is obtained directly from Getinteger:

 Public class Test {    publicstaticvoid  main (string[] args) {        = "{app : 16026481486462977} ";         = json.parseobject (data);        System.out.println (newObject);        System.out.println (Newobject.getinteger ("app"));}    }

Defines the normal value of an app, obtained through the Getinteger function, and returns 1:

{"App": 16026481486462977}1

Proceed to the method of viewing this function:

     Public Integer Getinteger (String key) {        = get (key);         return Casttoint (value);    }

Discovery calls the Casttoint method, it should return the int value directly, continue to see Casttoint:

     Public StaticInteger casttoint (Object value) {if(Value = =NULL) {            return NULL; }        if(ValueinstanceofInteger) {            return(Integer) value; }        if(ValueinstanceofNumber ) {            return(number) value). Intvalue (); }        if(ValueinstanceofString) {String Strval=(String) value; if(strval.length () = = 0//|| "null". Equals (Strval)//|| "NULL". Equals (Strval)) {                return NULL; }                        if(Strval.indexof (', ')! = 0) {Strval= Strval.replaceall (",", "" "); }            returnInteger.parseint (Strval); }        if(ValueinstanceofBoolean) {            return((Boolean) value). Booleanvalue ()? 1:0; }        Throw NewJsonexception ("Can not cast to int, value:" +value); }

This method actually solves all the exception scenarios, and if you return directly for int, call the Intvalue method of the number class directly if it is a value of the other type:

Review the JDK's description of the method, forcing the value of type number to be converted to int:

Here the reason should be found, change to Getlong, modify the database the type of the field is bigint type, and start the inbound spark JOB again:

ID | Alert_time | Process_time | protocol | App | App_proto | Bytesall | Packetsall
----+------------+--------------+----------+-------------------+-----------+-----------+------------
1 | 1528426860 | 1528426980 | 17 | 2040693581152257 | 0 | 990 | 11
2 | 1528426920 | 1528426980 | 17 | 2040693581152257 | 0 | 1170 | 13
3 | 1528426860 | 1528426980 | 17 | 4582764464570369 | 21 | 411 | 5
4 | 1528426920 | 1528426980 | 17 | 4582764464570369 | 21 | 160 | 2
5 | 1528426860 | 1528426980 | 17 | 16026481486462977 | 0 | 184 | 2
6 | 1528426860 | 1528426980 | 6 | 4037406697193473 | 20 | 92 | 27
7 | 1528426560 | 1528426980 | 6 | 3087428650795009 | 8 | 9128 | 33
8 | 1528426920 | 1528426980 | 6 | 4037406697193473 | 20 | 414 | 83
9 | 1528426920 | 1528426980 | 6 | 3087428650795009 | 8 | 24654 | 78
10 | 1528426860 | 1528426980 | 6 | 3087428650795009 | 8 | 68500 | 122
11 | 1528426920 | 1528426980 | 17 | 6157265115545601 | 0 | 7888 | 102
12 | 1528426920 | 1528426980 | 6 | 27637324275777538 | 0 | 307 | 10
13 | 1528423020 | 1528426980 | 6 | 4037406697193473 | 20 | 46 | 8

The value is normal and the bug is found and resolved.

Jsonobject class Getinteger function Value exception

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.