A library that is used as a Java structured file computing class

Source: Internet
Author: User

Sometimes we can not use the database and the file system to store data, it is necessary to complete the file-based data calculation. However, Java itself lacks the corresponding class library, which requires hard coding to achieve structured file calculations with complex code and poor readability. There are many issues on the web that look for Java class libraries for file calculations, such as:

Http://www.coderanch.com/t/561180/java/java/read-text-file-perform-operation

http://stackoverflow.com/questions/9614112/ How-to-sort-and-arrange-a-text-file-in-java-to-group-lines

Http://stackoverflow.com/questions/26418282/get-text-file-through-sql-java

Http://www.linkedin.com/groups/I-would-like-desing-DAO-70526.S.5927393719350235139?trk=groups_ search_item_list-0-b-ttl&goback=%2egna_70526

HTTPS://PLUS.GOOGLE.COM/112275790033275716955/POSTS/4P75NJYZMCV

Http://stackoverflow.com/questions/28969897/file-based-datastructure-for-java

Use the free collector to compensate for this deficiency. The collector encapsulates a rich set of structured file calculation functions and provides a JDBC interface. Java applications can execute the collector script file as a database stored procedure, passing in parameters and getting the returned results using JDBC.

The integration structure of the collector and the Java application is as follows:


The following is an example of a text file's conditional query that illustrates the process by which the Java call Collector completes a structured file calculation. The source data is as follows:

Conditional query: Select an order within a certain time period.

Code:

Explanation:

A1: Read in file. The default delimiter is tab,@t to read the first line as a column header.

A2: Performs a conditional query. StartDate and EndDate are input parameters, such as 2010-01-01 to 2010-12-31. Results:


The Java main program can invoke the scheduler script in a JDBC way, with the following code:

class.forname ("Com.esproc.jdbc.InternalDriver");

Con=drivermanager.getconnection ("jdbc:esproc:local://");

                    // Call the Collector script (similar to a stored procedure) where Orderquery is a DFX the file name

St= (com. esproc.jdbc.InternalCStatement) con.preparecall ("Call Orderquery (?,?)");

St.setobject (1, "2010-01-01");

St.setobject (2, "2010-12-31");

                    // Execute Script

St.execute ();

                    // Get result set

Resultsetrs = St.getresultset ();

......

The return value is the ResultSet object that conforms to the JDBC standard, and the method of invoking the collector script is exactly the same as accessing the database, and programmers familiar with JDBC can quickly master it.

for the simpler code above, you can also write the script directly in the JDBC call, separated by \ n between the multiline statements, similar to executing a more complex SQL, so that you do not have to save a script file.

st = (com.esproc.jdbc.InternalCStatement) con.createstatement ();

ResultSet rs1 =st.executequery ("=file (\" d:\\sorder.txt\ ")[email protected]() \ n" + "=a1. Select (Orderdate>=date (\ "2010-01-01\") && orderdate<=date (\ "2010-12-31\" ));

The Collector returns the value of the last expression.

More detailed information on the deployment and invocation of the collector JDBC can be found in the Java call to the "Integrator Integration Application"

as a professional structured computing class library, the collector can also implement more operations, the following examples.

Sort: Sorted by customer code in descending order, sorted by year, month ascending.

Code: =a1.sort (-client,year (OrderDate), month (OrderDate))

Explanation: The descending arrangement uses the "-", the year, the month needs to be calculated to obtain.

Extension: If you want to sort on the basis of the query, you can write =a2.sort (...), or =a1.select (...). Sort (...)

Results:


Grouped Totals: Calculates sales and orders per sales member per year

Code:=a1.groups (Sellerid,year (OrderDate); sum (Amount), COUNT (~))

Explanation: The function group can be aggregated at the same time as the grouping, ~ represents each group or current group, and COUNT (~) equals count (OrderID).

Results:


Get unique values: List customer lists

Code:=a1.id (Client)

Results:


Remove Duplicates: Keep the first record per customer for each sale

Code:[email protected] (Client,sellerid)

Explanation: The function group is used for grouping (can not be summarized), @1 indicates that each group takes the 1th record.

Results:


TopN: Find 3 orders with the largest sales per salesperson.

Code:=a1.group (Sellerid;~.top (3,-amount): t). Conj (t)

Explanation: The function top filters out TOPN, "-" denotes reverse order, and the function Conj is used for merging.

Extension: If you only take the largest one order, you can use the MAXP function.

Calculation result:


Associative calculations: Aligns the three fields of the Emp.txt name, Dept, gender to SOrder.txt.

Code:


Explanation:

A3: The function join performs the join operation and renames the two tables to S and e,@1 for the left connection. The results are as follows:


Extension: @1 for left link, @f for full connection , no option for inner connection .

A4: Make a new structured two-dimensional table by getting the required fields from the connected tables.

Results:


The previous example assumes that the file is small, and if the file is large, it cannot be put into memory, and the calculator cursor can be used to calculate it, refer to the relevant documentation.

It is worth mentioning that the functions of the above-mentioned collector are free, and programmers can embed the collector engine into their own applications without cost.

A library that is used as a Java structured file computing class

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.