Search in sources :

Example 1 with TemperatureMeasurement

use of org.acme.kafka.streams.aggregator.model.TemperatureMeasurement 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)

Aggregations

ObjectMapperSerde (io.quarkus.kafka.client.serialization.ObjectMapperSerde)1 Produces (javax.enterprise.inject.Produces)1 Aggregation (org.acme.kafka.streams.aggregator.model.Aggregation)1 TemperatureMeasurement (org.acme.kafka.streams.aggregator.model.TemperatureMeasurement)1 WeatherStation (org.acme.kafka.streams.aggregator.model.WeatherStation)1 StreamsBuilder (org.apache.kafka.streams.StreamsBuilder)1 KeyValueBytesStoreSupplier (org.apache.kafka.streams.state.KeyValueBytesStoreSupplier)1