How to query large text files in Java
Sometimes we need to query large text instead of databases. In this case, we need to stream the file and implement the query algorithm, and perform parallel processing to improve performance. However, JAVA lacks the corresponding class libraries and requires hard coding to implement structured file computing. The code is complex and readable, making it difficult to implement efficient parallel processing.
You can make up for this deficiency by using a free calculator. The Set calculator encapsulates a wide range of structured file read/write and cursor computing functions. You can write simple code to implement parallel computing and provide easy-to-use JDBC interfaces. JAVA applications can execute the script file of the assembler as a database Stored Procedure, input parameters, and return results using JDBC.
The integration structure of the conset calculator and Java application is as follows:
The following is an example of the basic process in which the assembler assists JAVA in querying large text. The source data sorder.txt is as follows:
To query orders whose start time and end time is between startDate and endDate and whose amount is greater than argAmount, you only need to use the following code:
A1: open a file using a cursor. @ T indicates reading 1st rows as column names.
A2: performs a structured query and the result is a cursor.
A3: Execute the cursor and read the result into the memory as follows:
The JAVA main program can call the set calculator script in JDBC mode. The Code is as follows:
Class. forName ("com. esproc. jdbc. InternalDriver ");
Con = DriverManager. getConnection ("jdbc: esproc: local ://");
// Call the set calculator script similar to the stored procedure), where searchbig is the dfx file name.
St = (com. esproc. jdbc. InternalCStatement) con. prepareCall ("call searchbig ");
// Set parameters
St. setObject (1, "2010-01-01 ");
St. setObject (2, "2010-12-31 ");
St. setObject (3,2000 );
// Execute the script
St.exe cute;
// Obtain the result set
ResultSetrs = st. getResultSet;
......
The returned value is a JDBC-compliant ResultSet object. The method for calling the set calculator script is the same as that for accessing the database. programmers who are familiar with JDBC can quickly master the returned value.
For the above simple code, you can also directly write the script in the JDBC call, multiple lines of statements can be separated by \ n, similar to executing a more complex SQL statement, this eliminates the need to save a script file.
St = (com. esproc. jdbc. InternalCStatement) con. createStatement;
ResultSet rs1 cmdst.exe cuteQuery ("= file (\" D :\\ sOrder.txt \"). import @ t \ n "+" = A1.select (OrderDate> = date (\ "2010-01-01 \") & OrderDate <= date (\ "2010-12-31 \") & amp; Amount> 2000) \ n "+
"= A2.fetch ");
The set operator returns the value of the last expression.
If the memory of the query results is not enough, you can directly return the cursor in the Set compute to remove the A3 Code). In JAVA, you only need to set the number of records read in each batch to read normally, the Code is as follows:
St. setFetchSize (1000)
For more information about the deployment and call of the jcs jdbc, seeJAVA calls the integrated application of the Set Calculator.
The Set calculator can also be implementedMulti-thread parallel computingThe simplest way is to use @ m in the cursor function of the above Code, which indicates multi-threaded File Reading.
You can also perform manual segmentation and use multi-thread parallel computing in both the read and computation sections. The Code is as follows:
A1: open the file with 8 cursors and read the specified part of the file each time .~ Indicates the cyclic variables, which are 1, 2, and so on... 8, @ z indicates that the file is roughly divided into several parts by the number of bytes, and only one part is read. The set operator automatically removes the header and supplements the end to ensure that the retrieved data is a whole row.
A2: query each cursor.
A3: Execute the cursor in parallel and merge the results. @ X indicates that the merged object is a cursor, and @ m indicates parallel computing. Note that the conj function cannot ensure that the result sequence is consistent with the source data.
The preceding Code uses the built-in parallel computing functions of the cube. If the computing process is complex or the memory can hold the computing results, it is suitableExplicit Parallel ComputingStatement. The Code is as follows:
A1: set the number of parallel rows.
A2: parallel execution of code, the scope of action is the indentation of the B2-B3. To (A1) = [1, 2... 8] indicates the entry parameters of each thread. A2 can be used inside the thread to obtain entry parameters, and A2 can be used outside the thread to obtain the computing results of all threads.
B3: Query cursor, read the result into the memory, and return it to the main thread.
A4: Merge the computing results of each thread in sequence.
For ordered data, you can useImproves query performance using the binary method. For example, if the data has been sorted by Client and OrderID, you can use the following code to find the corresponding records based on the argClient and argOrder parameters:
Begin, end is the start and end position of the binary method, and m is the center position.
B4: locate the intermediate position based on the number of bytes. Open the cursor and read a record. The set operator automatically removes the header and end the record to retrieve the complete record. @ X indicates that the cursor is closed immediately after the record is retrieved.
B5-C6: If positioning is successful, the current record is stored in C5.
B7-C8: If positioning fails, continue to compare the collection size and reset begin, end.
A9: explicitly return the calculation results in C5 to JDBC.