The collector assists Java in processing the JSON of a diverse data source

Source: Internet
Author: User
Tags first string

Java's JSON open-source package can only parse JSON data, without arithmetic functions. Programmers write their own common programs for grouping, sorting, filtering, and connecting these computations, which is rather cumbersome. For example, when you write a JSON file condition filter in Java, you need to rewrite the code when the conditional expression changes. If you want to implement a flexible conditional filtering like SQL, you need to implement dynamic expression parsing and evaluation, the programming work is very large.

The collector supports dynamic expressions and can be embedded in Java to write out a common JSON calculation program. Let's take a look at the concrete examples below. The pending string is a JSON-formatted employee information that contains the Eid, NAME, SURNAME, GENDER, State, BIRTHDAY, HireDate, and Dept fields, and this example parses the data Women who were born after January 1, 1981 (inclusive) were identified by conditions. The JSON string contents are as follows:

[{eid:1,name: "Rebecca", SURNAME: "Moore", GENDER: "F", State: "California", birthday:1974-11-20,hiredate:2005-03-11, DEPT: "R & R", 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:1970-12-17,hiredate:2010-12-01, DEPT: "Sales", salary:9000},...]

The idea is that the Java program calls the collector program and passes in the JSON string, the collector parses the JSON-formatted data to complete the conditional filtering, and then returns the result as a JSON string to the Java program. Because the collector supports dynamic expression parsing and evaluation, Java programs can filter JSON data flexibly, as is the case with SQL.

For example, we need to inquire about female employees born after January 1, 1981 (inclusive), ESPROC program can obtain two input parameters "Jsonstr" and "where" conditions externally, such as:

Where is a string whose value is: Birthday>=date (1981,1,1) && gender== "F".
The Esproc code is as follows:

A1: Parse the JSON format data into a sequential table. The integrated development environment of ESPROC can visually display the results of calculations, such as the right part.

A2: Filter by condition. In this case, a macro is used to implement a dynamic parse expression, where it is the passed-in parameter. The collector calculates ${first ...} Expression in the ${, substituting the result of the calculation as a macro string value ...} Then explain the execution. In this example, the final execution is: =a1.select (birthday>=date (1981,1,1) && gender== "F").

A3: Generates a JSON format string after the filtered order table.

A4: Returns a qualifying result set to an external program.

Change the filter condition without changing the code, just change the where parameter. For example, the condition becomes: query for female employees born after January 1, 1981 (inclusive), or name+surname equals "rebeccamoore" employees. Where parameter values 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 calling the Esproc script in a Java program is done through the JDBC interface, the returned result is a ResultSet object set. Gets the first string field of set, which is the JSON string after filtering. The specific code is as follows: (Save the above Esproc program as TEST.DFX):

    //Establishing a connection
     Class.forName ("Com.esproc.jdbc.InternalDriver");
con= drivermanager.getconnection ("jdbc:esproc:local://");
Call the stored procedure, where test is the file name of DFX
Com.esproc.jdbc.InternalCStatement St;
St = (com. esproc.jdbc.InternalCStatement) con.preparecall ("Call JSON (?,?)");
Set parameters, JSON string longer, omit part.
In practice, JSON strings may be generated in a variety of different ways, as described later.
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\ ");
Execute the Collector stored procedure
ResultSet Set=st.executequery ();
Gets the JSON format string after filtering
String Jsonstrresult;
if (Set.next ()) Jsonstrresult = set.getstring (1);

JSON is a common format for exchanging data in Internet applications where the JSON string may be from a local file or a remote HTTP server, and the collector can read the JSON string directly from a file or HTTP server. As an example of an HTTP server, suppose a testservlet can return a string of employee information in JSON format, which can be obtained and evaluated using the following code:

A1: The Httpfile object is defined, and the URL is http://localhost:6080/myweb/servlet/testServlet?table=employee&type=json.

A2: Reads the result returned by the Httpfile object.

A3: Parses a JSON-formatted string to generate a sequential table.

A4: Filter data by condition.

A5: Converts the filtered order table to a JSON-formatted string.

A6: Returns the results from the A4 to the Java code that called the Collector program.

The collector assists Java in processing the JSON of a diverse data source

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.