DEV Community

Judy
Judy

Posted on

Compare two csv files #eg34

Here are two large, same-structure csv files A and B. Their primary keys are Name & Dept fields. The two have some different records.

Image description
Use Java to compare primary keys of the two files to find records that exist in A but that does not exist in B according to the key values.

Image description
Write the following SPL code:

Image description
T()function parses a csv file; @c option enables retrieving data from a file that does not fit into the memory. sortx() function sorts data in a cursor. merge() function merges two cursors; @d enables calculating the difference.

The logic of the above code can be also expressed in a single SPL statement:

=[T@c(""A.csv"").sortx(Name,Dept),T@c(""B.csv"").sortx(Name,Dept)].merge@d(Name,Dept).fetch()

Read How to Call a SPL Script in Java to find how to integrate SPL into a Java application.

Open source SPL source address

Top comments (1)

Collapse
 
hanzla-baig profile image
Hanzla Baig

👍