use of org.acme.kafka.streams.aggregator.model.Aggregation in project quarkus-quickstarts by quarkusio.
the class TopologyProducer method buildTopology.
@Produces
public Topology buildTopology() {
StreamsBuilder builder = new StreamsBuilder();
ObjectMapperSerde<WeatherStation> weatherStationSerde = new ObjectMapperSerde<>(WeatherStation.class);
ObjectMapperSerde<Aggregation> aggregationSerde = new ObjectMapperSerde<>(Aggregation.class);
KeyValueBytesStoreSupplier storeSupplier = Stores.persistentKeyValueStore(WEATHER_STATIONS_STORE);
GlobalKTable<Integer, WeatherStation> stations = builder.globalTable(WEATHER_STATIONS_TOPIC, Consumed.with(Serdes.Integer(), weatherStationSerde));
builder.stream(TEMPERATURE_VALUES_TOPIC, Consumed.with(Serdes.Integer(), Serdes.String())).join(stations, (stationId, timestampAndValue) -> stationId, (timestampAndValue, station) -> {
String[] parts = timestampAndValue.split(";");
return new TemperatureMeasurement(station.id, station.name, Instant.parse(parts[0]), Double.valueOf(parts[1]));
}).groupByKey().aggregate(Aggregation::new, (stationId, value, aggregation) -> aggregation.updateFrom(value), Materialized.<Integer, Aggregation>as(storeSupplier).withKeySerde(Serdes.Integer()).withValueSerde(aggregationSerde)).toStream().to(TEMPERATURES_AGGREGATED_TOPIC, Produced.with(Serdes.Integer(), aggregationSerde));
return builder.build();
}
use of org.acme.kafka.streams.aggregator.model.Aggregation in project quarkus-quickstarts by quarkusio.
the class TopologyProducerTest method test.
@Test
public void test() {
WeatherStation station1 = new WeatherStation(1, "Station 1");
weatherStations.pipeInput(station1.id, station1);
temperatures.pipeInput(station1.id, Instant.now() + ";" + "15");
temperatures.pipeInput(station1.id, Instant.now() + ";" + "25");
temperaturesAggregated.readRecord();
TestRecord<Integer, Aggregation> result = temperaturesAggregated.readRecord();
Assertions.assertEquals(2, result.getValue().count);
Assertions.assertEquals(1, result.getValue().stationId);
Assertions.assertEquals("Station 1", result.getValue().stationName);
Assertions.assertEquals(20, result.getValue().avg);
}
Aggregations