The cube helps java Process JSON and javajson of diverse data sources.

Source: Internet
Author: User

The cube helps java Process JSON and javajson of diverse data sources.

Java json open-source packages can only parse json data and have no operation functions. It is quite troublesome for programmers to write General programs to implement grouping, sorting, filtering, and connection. For example, when you use java to write a json file conditional filter program, you must rewrite the code when the conditional expression changes. If you want to implement SQL-like flexible conditional filtering, You need to implement dynamic expression parsing and Evaluation on your own. The programming workload is very large.

The Set calculator supports dynamic expressions and can be embedded into Java to write common json computing programs. The following is an example of a specific practice. The string to be processed is the employee information in json format, including EID, NAME, SURNAME, GENDER, STATE, BIRTHDAY, HIREDATE, and DEPT. In this example, data is parsed, find out the female employees born after January 1, January 1, 1981. The Json string content is as follows:

[{EID: 1, NAME: "Rebecca", SURNAME: "Moore", GENDER: "F", STATE: "California", BIRTHDAY: 1974-11-20, HIREDATE: 2005-03-11, DEPT: "R & D", SALARY: 7000 },
{EID: 2, NAME: "Ashley", SURNAME: "Wilson", GENDER: "F", STATE: "New York", BIRTHDAY: 1980-07-19, HIREDATE: 2008-03-16, DEPT: "Finance", SALARY: 11000 },
{EID: 3, NAME: "Rachel", SURNAME: "Johnson", GENDER: "F", STATE: "New Mexico", BIRTHDAY:, HIREDATE:, DEPT: "Sales", SALARY: 9000},…]

The implementation idea is: the Java program calls the assembler program and passes in a json string. The Collector parses json format data to complete conditional filtering, then return the result to the Java program as a json string. Because the cube Reader supports dynamic expression parsing and evaluation, Java programs can filter json data flexibly as they use SQL.

For example, if we need to query the female employees born after January 1, January 1, 1981 (inclusive), the esProc program can obtain two external input parameters "jsonstr" and "where", such:

Where is a string whose value is: BIRTHDAY> = date (1981,1, 1) & GENDER = "F ".
The esProc code is as follows:

A1: parses data in json format into a forward table. EsProc's integrated development environment intuitively displays the computing results, such as the right part.

A2: Filter by conditions. Here, a macro is used to implement a dynamic parsing expression, where is the input parameter. The set operator calculates $ {…} first {...} Replace the calculation result with the macro string value $ {...} Then explain and execute. In this example, the final execution is: = A1.select (BIRTHDAY> = date (1981,1, 1) & GENDER = "F ").

A3: generate a json string for the filtered order table.

A4: returns a qualified result set to an external program.

When the filter condition changes, you do not need to change the code. You only need to change the where parameter. For example, if the condition is changed to: Query an employee born after January 1, January 1, 1981, or an employee whose NAME + SURNAME is "RebeccaMoore. The Where parameter value can be written as: BIRTHDAY> = date (1981,1, 1) & GENDER = "F" | NAME + SURNAME = "RebeccaMoore ". After execution, the result set in A2 is as follows:

Because the esProc script is called in a Java program through the jdbc interface, the returned result is a ResultSet object set. The first string field of the set is the filtered json string. The Code is as follows: (Save the esProc program as test. dfx ):

& Nbsp; // establish a connection
& Nbsp; Class. forName ("com. esproc. jdbc. InternalDriver ");
Con = DriverManager. getConnection ("jdbc: esproc: local ://");
// Call the stored procedure, where test is the dfx file name
Com. esproc. jdbc. InternalCStatement st;
St = (com. esproc. jdbc. InternalCStatement) con. prepareCall ("call json (?,?)");
// Set parameters. The json string is long and omitted.
// In actual application, json strings may be generated in different ways. For details, refer to the following section.
String jsonstr =
"[{EID: 1, NAME: \" Rebecca \ ", SURNAME: \" Moore \ ", GENDER: \" F \ ", STATE: \ "California \...}] ";
St. setObject (1, jsonstr );
St. setObject (2, "BIRTHDAY> = date (1981,1, 1) & GENDER = \" F \"");
// Execution set calculator Stored Procedure
ResultSet set=st.exe cuteQuery ();
// Obtain the filtered json string
String jsonstrResult;
If (set. next () jsonstrResult = set. getString (1 );

Json is a common format for data exchange in Internet applications. In actual applications, json strings may come from local files or remote HTTP servers, the collector can directly read json strings from a file or an HTTP server. Take the HTTP server as an example. Assume that a testServlet can return a json-format employee information string, you can use the following code to obtain and calculate it:

A1: defines the httpfile object. The url is http: // localhost: 6080/myweb/servlet/testServlet? Table = employee & type = json.

A2: Read the results returned by the httpfile object.

A3: parses json strings to generate a sequence table.

A4: filter data based on conditions.

A5: Convert the filtered order table to a json string.

A6: return the result in A4 to the Java code that calls this set calculator program.

Related Article

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.