The collector assists in the conditional filtering of Java processing of structured text

Source: Internet
Author: User

It is troublesome to filter the data in a text file directly by using Java in the following conditions:

1. The file is not a database and cannot be accessed with SQL. The code needs to be rewritten when the filter condition 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.

2, the file is too large can not be loaded into memory processing at one time, and the use of progressive reading mode in consideration of performance will involve the file buffer management, split-line computing and other complex programming.

Using a collector to aid in Java programming, these problems do not require you to write code to solve. Let's take a look at the concrete examples below.

The employee data is saved in the text file Employee.txt. We want to read employee information and find out who was born after January 1, 1981 (inclusive).

The format of the text file Empolyee.txt is as follows:

EID NAME SURNAME GENDER State BIRTHDAY hiredate DEPT SALARY
1 Rebecca Moore F California 1974-11-20 2005-03-11 7000
2 Ashley Wilson F New York 1980-07-19 2008-03-16 Finance 11000
3 Rachel Johnson F New Mexico 1970-12-17 2010-12-01 Sales 9000
4 Emily Smith F Texas 1985-03-07 2006-08-15 HR 7000
5 Ashley Smith F Texas 1975-05-13 2004-07-30 16000
6 Matthew Johnson M California 1984-07-07 2005-07-07 Sales 11000
7 Alexis Smith F Illinois 1972-08-16 2002-08-16 Sales 9000
8 Megan Wilson F California 1979-04-19 1984-04-19 Marketing 11000
9 Victoria Davis F Texas 1983-12-07 2009-12-07 HR 3000
Ten Ryan Johnson M Pennsylvania 1976-03-12 2006-03-12 13000
Jacob Moore M Texas 1974-12-16 2004-12-16 Sales 12000
Jessica Davis F New York 1980-09-11 2008-09-11 Sales 7000
Daniel Davis M Florida 1982-05-14 2010-05-14 Finance 10000
...

The idea is to use a Java program to call the collector script, read and calculate the data, and then return the result to the Java program in a resultset way. Because the collector supports dynamic expression parsing and evaluation, Java programs can filter data in a text file flexibly, as with SQL.

For example, we need to inquire about female employees born after January 1, 1981 (including), ESPROC program can obtain an input parameter "where" as a condition externally, such as:

Where is a string with the value: Birthday>=date (1981,1,1) && gender== "F".

The Esproc code is as follows:

A1: Defines a file object, reads the data, the first row is the title, and the field delimiter is tab by default. The integrated development environment of ESPROC can visually display the imported data, 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: 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:

The code for using Esproc JDBC to invoke this program in a Java program is as follows: (Save the above Esproc program as TEST.DFX):

    //Establish esproc JDBC Connection
    class.forname (" Com.esproc.jdbc.InternalDriver ");
    con= drivermanager.getconnection ("jdbc:esproc:local://");
    //calls the ESPROC program (stored procedure), where test is the file name of DFX
    st = ( com.esproc.jdbc.InternalCStatement) Con.preparecall ("Call Test (?)");
    //Setting Parameters
    st.setobject (1, "Birthday>=date (1981,1,1) & & gender==\ "f\" | | Name+surname==\ "rebeccamoore\");//parameter is the dynamic filter condition
    //execute esproc stored procedure
     st.execute ();
    //Get result set: Qualified Employee collection
    resultset set = St.getresultset ();

For scripts with simpler code, you can also write code directly in the Java program that calls the test.dfx JDBC, without having to write a script file specifically:
St= (com. esproc.jdbc.InternalCStatement) con.createstatement ();
ResultSet set= st.executequery ("=file (\" d:/employee.txt\ ") [email protected] (). Select (Birthday>=date (1981,1,1) &&gender==\ "F\" | | Name+surname==\ "rebeccamoore\");

This Java code directly invokes a script from the collector: the data is obtained from the text file and filtered according to the specified criteria. The result set is returned to the ResultSet object set.

The above method assumes that the file is small and can be read into memory. However, it can actually occur when the file is large and unreadable, and it is not necessary to take up too much memory even if it can be read in, which is handled by using a file cursor. The Collector program is adjusted as follows:

A1: Defines a file object cursor, the first row is the caption, and the field delimiter is tab by default.

A2: Filters cursors 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 the ${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: Returns a cursor.

Although the collector returns a cursor to Java, the program that is called by Java does not have to be modified. When Java uses ResultSet to traverse data, the collector automatically takes the contents of the travel tag.

If you need to write the filtered data to another file instead of returning to the main program, simply change the A3 expression to: =file ("D:/employee_group.txt") [email protected] (A2), the collector will write the cursor data into a file.

The collector assists in the conditional filtering of Java processing of structured text

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.