June 8, 2015

esProc Finds Differences between CSV files

userName and date are the logical primary key of both old.csv file and new.csv file, in which we want to find rows that are new, deleted and updated.
The source data is as follows:




As can be seen from the above data, in new.csv the 2nd and the 3rd row are the new and the 4th row is the updated; in old.csv the 3rd row is the deleted.
esProc code: 

A1,B1Retrieve the comma-separated files.

A2,B2Sort data by the key, as this is required by the following merge function.

A3Find the new records by the key. merge function is used to merge data sets. @d means calculating the difference during the merge. Similar options include @u for union and @i for intersection. The computed result is as follows: 

A4Find the deleted records by the key. The computed result is as follows: 

A5Take the key fields as ordinary ones to find the updated records. The computed result is as follows: 

A6A5 is an intermediate result. We need to calculate the difference between A5 and the new records to get the updated records. The computed result is as follows: 
B6Return A6 to JAVA program or the reporting tool.

Now all data processing work has been finished. We’ll then integrate the esProc script into JAVA program via JDBC. The JAVA code is as follows:
         //establish a connection via esProc JDBC
         Class.forName("com.esproc.jdbc.InternalDriver");
         con= DriverManager.getConnection("jdbc:esproc:local://");
         //call esProc script, whose name is test and that can accept parameters
         st =(com.esproc.jdbc.InternalCStatement)con.prepareCall("call test()");
         st.execute();//execute esProc stored procedure
         ResultSet set = st.getResultSet();//get the result

If you want to return multiple data sets to the JAVA program, you can modify B6’s code into result new,delete,update.

No comments:

Post a Comment