Group & Aggregate by Dates

Question

I have an ArrayList that is created from an input CSV file, in which some calculations will be performed to generate more columns which are to be afterwards printed into another CSV file. In the ArrayList, one of the attributes is a Unix time stamp. The record comprises of about 7 different days. What I want to do is to group the records by day, then if it's not in order, order the groups by time (the specifics, i.e. hours, minutes, seconds). So, from my input CSV file, I extracted the Unix timestamp using a delimiter, e.g.1442327884, then I used this code to retrieve the day.

java.util.Date time = new java.util.Date((long) timeStamp * 1000);

// gives a result of Tue Sep 15 22:38:04 SGT 2015

String date = String.valueOf(time.getDate());;

// gives the result of "15"

A method I used for another calculation for grouping by is as follows:

Map<String, List<String>> groups = data.stream().collect(Collectors.groupingBy(e -> e.split(",")[1]));

How do I set it to group by String date as mentioned above?

 

Answer

Group & aggregate is the typical structured computation. But as Java lacks the special class library for handling it, we have to resort to hardcoding, which is rather a hassle. It’s simple and easy to get it done in SPL (Structured Process Language). The script is integration-friendly with a Java program. (See How to Call an SPL Script in Java)

Since the asker doesn’t provide the source data, I’ll use the following data to illustrate my solution:

id             time_stamp

1              1442327884

2              1442337884

3              1442347884

4              1442338884

SPL script:

A

1

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

2

=A1.run(time_stamp=date(time_stamp))

3

=A2.group(time_stamp)

4

=A3.conj()

5

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

A1: Read in data from source.csv.

A2: Convert Unix time stamps into date data.

A3: Group records by dates and sort them.

A4: Concatenate records from all groups.