Search in sources :

Example 1 with Aggregation

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();
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) Aggregation(org.acme.kafka.streams.aggregator.model.Aggregation) WeatherStation(org.acme.kafka.streams.aggregator.model.WeatherStation) TemperatureMeasurement(org.acme.kafka.streams.aggregator.model.TemperatureMeasurement) KeyValueBytesStoreSupplier(org.apache.kafka.streams.state.KeyValueBytesStoreSupplier) ObjectMapperSerde(io.quarkus.kafka.client.serialization.ObjectMapperSerde) Produces(javax.enterprise.inject.Produces)

Example 2 with Aggregation

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);
}
Also used : Aggregation(org.acme.kafka.streams.aggregator.model.Aggregation) WeatherStation(org.acme.kafka.streams.aggregator.model.WeatherStation) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Aggregations

Aggregation (org.acme.kafka.streams.aggregator.model.Aggregation)2 WeatherStation (org.acme.kafka.streams.aggregator.model.WeatherStation)2 ObjectMapperSerde (io.quarkus.kafka.client.serialization.ObjectMapperSerde)1 QuarkusTest (io.quarkus.test.junit.QuarkusTest)1 Produces (javax.enterprise.inject.Produces)1 TemperatureMeasurement (org.acme.kafka.streams.aggregator.model.TemperatureMeasurement)1 StreamsBuilder (org.apache.kafka.streams.StreamsBuilder)1 KeyValueBytesStoreSupplier (org.apache.kafka.streams.state.KeyValueBytesStoreSupplier)1 Test (org.junit.jupiter.api.Test)1