I have a series of events, each event contains a timestamp, device, serialnumber, and measurement.
class Event {
private String device;
private String description;
private String serialnumber;
private Measurement measurement;
}
class Measurement {
private LocalDateTime timestamp;
private int value;
}
I have a stream of these events, and I would like to aggregate them into a simpler structure, dropping the serial number, then group them by device, and then sorting the measurements by timestamp and value.
{device: "device_1", description: "first device", serialnumber: "1", measurement: { timestamp: 2022-04-23T18:20:22Z, value: 180}}
{device: "device_2", description: "second device", serialnumber: "2", measurement: { timestamp: 2022-04-23T18:20:28Z, value: 120}}
{device: "device_2", description: "second device", serialnumber: "2", measurement: { timestamp: 2022-04-23T18:20:20Z, value: 160}}
{device: "device_1", description: "first device", serialnumber: "1", measurement: { timestamp: 2022-04-23T18:20:22Z, value: 170}}
[
{
device: "device_1",
description: "first device",
measurements: [
{ timestamp: 2022-04-23T18:20:22Z, value: 170},
{ timestamp: 2022-04-23T18:20:22Z, value: 180}
]
},
{
device: "device_2",
description: "second device",
measurements: [
{ timestamp: 2022-04-23T18:20:20Z, value: 160},
{ timestamp: 2022-04-23T18:20:28Z, value: 120}
]
}
]
I've managed to get the required format by creating a "builder" class in which you can insert events that get then processed and added to the data members in the correct format/order.
However, I think it would be nicer to somehow achieve this on the fly without another extra class, but using the groupingBy and toMap (and other?) stream methods.