Java Query Large text

Source: Internet
Author: User

However, Java itself lacks the corresponding class library, which requires hard coding to achieve structured file computation, complex code and poor readability, which makes it difficult to achieve efficient parallel processing.

Use the free collector to compensate for this deficiency. The collector encapsulates a rich set of structured file read-write and cursor calculation functions, writes simple code to achieve parallel computing, and provides an easy-to-use JDBC interface. Java applications can execute the collector script file as a database stored procedure, pass in parameters and get the returned results with JDBC

The following examples illustrate the process by which the collector assists Java in querying large text.

The source data is sOrder.txt as follows:

To query between StartDate and EndDate, the amount is greater than argamount order, just use the following code:

Use the function cursor to open the file as a cursor, @t to read the 1th row as a column name. After the structured query, the query results can be read into memory with a fetch, as follows:

If the query results memory does not fit, you can directly return the cursor in the collector (that is, remove the A3 code), in Java only need to use JDBC streaming read to obtain the results of the calculation.

The collector can also implement multithreaded parallel computations , the simplest way is to use @m in the cursor function of the preceding code, which means that multithreaded reads the file.

can also be manually segmented, both in the Read and compute sections using multi-threaded parallel computing, the code is as follows:

The code above uses 8 cursors to open the file, one at a time to read the specified portion of the file. @z means that the file is roughly divided into sections in bytes, and only part of it is read, and the collector automatically goes through the header to ensure that the data being fetched is the entire line.

The function Conj merges the results of the calculation, @x indicates that the merged object is a cursor, and @m represents a parallel calculation. It is important to note that the function CONJ cannot guarantee that the result order is consistent with the source data.

The code above uses a parallel calculation function built into the collector, and if the computational process is more complex and the memory can load the results, it is appropriate to use explicit parallel computation of the statement. The code is as follows:

The above code reads a large file with 8 sub-threads, executes the query, and then returns the result to the main thread. The function fork executes a sub-thread, scoped to B2-B3. A2 can be used internally by the thread to get the ingress parameters, and the thread can use A2 to get the computed results for all threads.

For ordered data, the binary method can be used to improve the performance of the query. For example, the data is sorted by client and OrderID, now to find the corresponding records according to the parameters argclient and Argorder, you can use the following code:

Begin,end is the starting and ending position of the dichotomy, and M is the middle position.

B4: To the middle position by the number of bytes, open the cursor read into a record, the collector will automatically implement the end of the head, take out the full record. If the location is successful, the current record is stored in C5. If the location is unsuccessful, continue to compare the collection size and reset the begin,end.

?
get ""

Java Query Large 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.