String Split - For Structured Post-grouping Intra-group Calculations

Question

I have an array list created from an iteration:

 

ArrayList<String> ulpList = new ArrayList<String>();

String record = id + "," + lp.getTime()+ "," + lp.getLatitude() + "," + lp.getLongtitude()+ "," + lp.getPoint() + "," + lp.getDistance();

ulpList.add(record);

 

There will be many instances of same id with different times, latitudes, longitudes, points and distances. I want to break the array list ulpList into various sublists, and sort these sublists by time. After that I want to perform some calculations and join them back and output the result into a .csv file. I just need to know how to break up the array list into desired sublists (which will be temporary) and how to sort them by time, which is after the first delimiter.

Sample output:

[[04ae46c177169feac5f697eexxxx,1418601075,1.375579,103.960797,null, 1000.0]]

[[04ae46c177169feac5f697eexxxx,1418602016,1.381164,103.966164,null, 1000.0]]

[[04ae46c177169feac5f697eexxxx,1418603148,1.381164,103.966164,null, 1000.0]]

[[04ae46c177169feac5f697eexxxx,1418601994,1.381164,103.966164,null, 1000.0]]

[[055ee328d4d297500e9a7f4cffe6xxxx,1418602721,1.313564,103.878443,null, 1000.0]]

[[055ee328d4d297500e9a7f4cffe6xxxx,1418602119,1.313564,103.878443,null, 1000.0]]

[[055ee328d4d297500e9a7f4cffe6xxxx,1418601901,1.313564,103.878443,null, 1000.0]]

[[055ee328d4d297500e9a7f4cffexxxx,1418600991,1.313564,103.878443,null, 1000.0]]

[[055ee328d4d297500e9a7f4cffe6xxxx,1418600132,1.313564,103.878443,null, 1000.0]]

[[00cd34bad39d19f8e2a335b444bxxxx,1418600273,1.345569,103.696973,null, 1000.0]]

[[04036dd2f45253bc9c24810f8e3exxxx,1418603285,1.301047,103.853357,null, 1000.0]]

 

Answer

Here’s the algorithm: Remove the parentheses from the 1st and the 6th fields, group records by the 1st field and sort each group by the 2nd field, perform certain calculations over every group and concatenate all the records up. It will be very hard hardcode all these operations in Java. But SPL makes it simple and easy

A

1

=   file("d:\\source.csv").import@c()

2

=A1.run(#1=replace(#1,"[[",""),#6=replace(#6,"]]",""))

3

=A2.group(#1).(~.sort(#2))

4

=A3.conj(~.to(2,4))

5

=   file("d:\\result.csv").export(A4)

A1: Import data from the csv file.

undefined 

A2: Delete parentheses from field 1 and field 6.

undefined 

A3: Group record by field 1 and sort each group by field 2.

http://www.raqsoft.com.cn/raq/qiniu/themes/default/images/spacer.gif undefined

A4: Perform calculations over each group and concatenate the records. Since the question doesn’t mention which type of calculation to be performed over each group, we just take getting records from row 2 to row 4 as an example.

A5: Export A4’s result into a csv file.

An SPL script is easy to be embedded into a Java application. See How to Call an SPL Script in Java.