The Set calculator assists java in processing the alignment connection of structured text, and java structuring
The database has associated statements to facilitate alignment and join operations. However, sometimes data is stored in text files. To use JAVA for direct computation, you need to write a large number of loop statements, code is cumbersome and inefficient. Java programming is assisted by the set calculator to solve such problems conveniently and efficiently. The following is an example of a specific practice.
The employee information is stored in the employee file emp.txt, and the employee whose EId is equal to 1 is not in the file. The orders file sorder.txt stores the order information. The SellerId field corresponds to the EId field in emp, and the order with SellerId equal to 2 is not in this table. Some source data is as follows:
Emp.txt:
SOrder.txt:
Now we need to align the Name, Dept, and Gender fields of emp to sOrder and output the calculation results to the new file. The expected calculation result is as follows:
Code of the Set calculator:
A1 and A2 respectively read data from text files and store the data in two variables: emp and sOrder. The function import is used here. The default column Delimiter is tab. The function option @ t indicates that the first row is read as the field name. In this example, only some fields in emp.txt are used. Therefore, A1 must use the field name as a parameter. After calculation, the values of emp and sOrder are as follows:
A3: = join @ 1 (sOrder: s, SellerId; emp: e, EId ). Function join performs join operations and renames two tables s and e. The function option @ 1 indicates the left join. In this example, the emp is aligned to sOrder. The calculation result is as follows:
Click the blue link to view the specific record, as shown below:
Right joinOnly the positions of alignment can be exchanged, which can also be implemented by the set calculator. For example, to align sOrder with emp, you only need to swap the order of the two in the code, that is, = join @ 1 (emp: e, EId; sOrder: s, SellerId). The calculation result is as follows:
Full connectionIt is also easy to implement. You only need to use the function option @ f. The code is: join @ f (sOrder: s, SellerId; emp: e, EId). The calculation result is as follows:
There are four types of connection operations, except the left, right, and full connections mentioned above.Internal Connection. Function join executes the internal join by default. The code is = join (sOrder: s, SellerId; emp: e, EId). The calculation result is as follows:
Return to the example, A4: = A3.new (s. orderID, s. client, s. sellerId, s. amount, s. orderDate, e. name, e. dept, e. gender ). This code retrieves the required fields from the joined table to form a new structured two-dimensional table. The calculation result is as follows:
The alignment has been completed. The following code outputs data to a new file: = file ("E: \ result.txt"). export @ t (A4 ).
By default, the function export uses tab as the column delimiter. The function option @ t indicates that the field name is output to the first line. Open result.txt and you can see the following content:
The above script has completed all of its output work, and then you only need to call it in JAVA code.
// Establish an esProc jdbc connection
Class. forName ("com. esproc. jdbc. InternalDriver ");
Con = DriverManager. getConnection ("jdbc: esproc: local ://");
// Call esProc, where test is the script file name
St = (com. esproc. jdbc. InternalCStatement) con. prepareCall ("call test ()");
// Execute the esProc Stored Procedure
St.exe cute ();
To execute the Java code, empwill be synchronized to sorder, and the computing result is output to the result.txt file.
The following example is slightly changed: query the data in sOrder by dynamic time period, execute the same alignment operation, and then return the result directly to JAVA. To implement this example, the generator must define two parameters, begin and end, which indicate the start time and end time. The Set calculator code is as follows:
The red part is the modified Code.
A2: Use the select function to further filter sOrder. The filter condition is the start time and end time of JAVA, that is, @ begin and @ end.
A5: outputs the computing result in A4 to the JDBC interface.
JAVA code also needs to be modified to pass in parameters and obtain calculation results. The Code is as follows:
Class. forName ("com. esproc. jdbc. InternalDriver ");
Con = DriverManager. getConnection ("jdbc: esproc: local ://");
St = (com. esproc. jdbc. InternalCStatement) con. prepareCall ("call test (?,?)");
St. setObject (1, startTime );
St. setObject (2, endTime );
St.exe cute ();
ResultSet set = st. getResultSet ();