Custom sort 2 csv files with 2 columns and print differences in Java
问题
I have 2 csv files as below:
csv1:
101,101-1,400.01,500.01
101,101-2,400.02,500.01
102,102-1,600.01,700.01
102,102-2,600.02,700.02
csv2:
101,101-1,400.02,500.01
101,101-2,400.02,500.01
102,102-1,600.01,700.02
102,102-2,600.02,700.07
I want to store the data in Java collection in such a way I can compare column c and d both csv and print the differences of each id.
desired output:
difference of column c of id 101 and sub id 101-1 is : 0.01
difference of column d of id 101 and sub id 101-1 is : 0.00
difference of column c of id 102 and sub id 102-1 is : 0.00
difference of column d of id 102 and sub id 102-1 is : 0.01
and so on.
I have tried using Map<Integer,List<Map<String,List>>> but its getting too complex and time consuming. Please suggest better way to get the result using Java.
解答
这个问题需要对两个csv 文件的某两列同序排序,然后计算并输出其他对应列相减后的绝对值,Java 实现则代码较长。
用Java 下的开源包 SPL 很容易写,只要几句:
A |
|
1 |
=file("1.csv").import@wc().sort(~(1),~(2)) |
2 |
=file("2.csv").import@wc().sort(~(1),~(2)) |
3 |
=A1.("difference of column c of id"/~(1)/"and sub id"/~(2)/"is :"/abs(~(3)-A2(#)(3))/"\ndifference of column d of id"/~(1)/"and sub id"/~(2)/"is :"/abs(~(4)-A2(#)(4))).export() |
SPL提供了JDBC 供 Java 调用,把上面的脚本存为 diff.splx,在 Java 中以存储过程的方式调用脚本文件:
…
Class.forName("com.esproc.jdbc.InternalDriver");
con= DriverManager.getConnection("jdbc:esproc:local://");
st=con.prepareCall("call diff()");
st.execute();
…
English version