Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does the aggregator assist Java in processing structured text to implement conditional filtering

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "aggregator how to help Java deal with structured text to achieve conditional filtering", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "aggregator how to help Java deal with structured text to achieve conditional filtering" bar!

Directly using Java to filter data in text files according to conditions will have the following troubles:

1. The file is not a database and cannot be accessed by SQL. The code needs to be rewritten when the filter conditions change. If you want conditional filtering as flexible as real phenomenon SQL, you need to implement dynamic expression parsing and evaluation on your own, and the programming workload is very heavy.

2. When the file is too large, it can not be loaded into memory at one time, while the step-by-step read-in method will involve complex programming such as file buffer management, line splitting calculation and so on.

Using aggregators to assist Java programming, these problems do not need to be solved by writing your own code. Let's take a look at the concrete practice through an example.

Employee data is saved in the text file employee.txt. We need to read the employee information to find out the female employees 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 rotated 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

* * shley Smith F Texas 1975-05-13 2004-07-30 rotated 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

10 Ryan Johnson M Pennsylvania 1976-03-12 2006-03-12 rotated 13000

11 Jacob Moore M Texas 1974-12-16 2004-12-16 Sales 12000

12 Jessica Davis F New York 1980-09-11 2008-09-11 Sales 7000

13 Daniel Davis M Florida 1982-05-14 2010-05-14 Finance 10000

...

The idea of implementation is to call the aggregator script with the Java program, read and calculate the data, and then return the result to the Java program in the form of ResultSet. Because the aggregator supports dynamic expression parsing and evaluation, Java programs can filter data in text files as flexibly as they do with sql.

For example, we need to query female employees born after January 1, 1981 (inclusive). The esProc program can obtain an external input parameter "where" as a condition, as shown in the following figure:

Where is a string with a value of: BIRTHDAY > = date & & GENDER== "F".

The esProc code is as follows:

A1: define a file object and read in the data. The * line is the title and the field delimiter is tab by default. EsProc's integrated development environment can visually display the imported data, as shown on the right side of the figure above.

A2: filter according to conditions. Macros are used here to implement dynamic parsing expressions, where where is the incoming parameter. The aggregator first calculates ${. } to replace ${as a macro string value with the result of the evaluation. Explain the execution later. The final execution in this example is: = A1.select (BIRTHDAY > = date) & & GENDER== "F").

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

You don't have to change the code when the filter conditions change, just change the where parameter. For example, the condition changes to: query female employees born after January 1, 1981 (inclusive), or employees whose NAME+SURNAME equals "RebeccaMoore". The parameter value of Where can be written as: BIRTHDAY > = date & & GENDER== "F" | NAME+SURNAME== "RebeccaMoore". After execution, the result set in A2 is shown below:

The code to get the result by calling this program with esProc JDBC in the Java program is as follows: (save the above esProc program as test.dfx):

/ / establish an esProc jdbc connection

Class.forName ("com.esproc.jdbc.InternalDriver")

Con= DriverManager.getConnection ("jdbc:esproc:local://")

/ / call the esProc program (stored procedure), where test is the file name of dfx

St = (com.esproc.jdbc.InternalCStatement) con.prepareCall ("call test")

/ / set parameters

St.setObject (1, "BIRTHDAY > = date) & & GENDER==\" F\ "| | NAME+SURNAME==\" RebeccaMoore\ "); / / parameters are dynamic filtering conditions

/ / execute esProc stored procedures

St.execute ()

/ / get the result set: a set of qualified employees

ResultSet set = st.getResultSet ()

For scripts with simpler code, you can also write the code directly in the Java program that calls the aggregator JDBC without having to write a script file (test.dfx):

St= (com. Esproc.jdbc.InternalCStatement) con.createStatement ()

ResultSet set= st.executeQuery ("= file (\" D:/employee.txt\ ") .import@t () .select (BIRTHDAY > = date) & & GENDER==\" F\ "| | NAME+SURNAME==\" RebeccaMoore\ "))

This Java code directly calls a script from the aggregator: get the data from the text file and filter it 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. But in fact, it is possible that the file is too large to read, and even if it can be read, it does not need to take up too much memory, so you can use file cursors to deal with it. The aggregator program is adjusted as follows:

A1: define a file object cursor with the * line as the title and the field delimiter tab by default.

A2: filter cursors according to conditions. Macros are used here to implement dynamic parsing expressions, where where is the incoming parameter. The aggregator will first calculate ${. The expression in}, replacing ${as a macro string value with the result of the evaluation. Explain the execution later. The final execution in this example is: = A1.select (BIRTHDAY > = date) & & GENDER== "F").

A3: returns the cursor.

Although the aggregator returns cursors to Java, the program called by Java does not need to be modified. The aggregator automatically fetches the contents of the cursor when Java uses ResultSet to traverse the data.

If you need to write the filtered data to another file instead of returning it to the main program, simply change the expression in the A3 grid to: = file ("D:/employee_group.txt") .export @ t (A2), and the aggregator will write the cursor data out as a file.

Thank you for your reading, the above is the content of "how the aggregator helps Java deal with structured text conditional filtering". After the study of this article, I believe you have a deeper understanding of how the aggregator helps Java deal with structured text conditional filtering, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report