Kafka Streams 2.1.1 class cast while flushing timed aggregation to store

后端 未结 2 1998
独厮守ぢ
独厮守ぢ 2021-01-16 10:21

I\'m trying to use kafka streams to perform a windowed aggregation and emit the result only after a certain session window is closed. To achieve this I\'m using the suppress

2条回答
  •  礼貌的吻别
    2021-01-16 11:00

    There two option to solve that issue:

    1. use TimeWindowedKStream::aggregate(final Initializer initializer, final Aggregator aggregator, final Materialized> materialized);

    2. use KStream::groupByKey(final Grouped grouped)

    In you case it will be:

    Ad 1:

    input
        .groupByKey()
        .windowedBy(SessionWindows.with(Duration.ofSeconds(30)))
        .aggregate(() -> Long.valueOf(0), (key, v1, v2) -> v1 + v2, (key, agg1, agg2) -> agg1 + agg2, Materialized.with(Serdes.String(), Serdes.Long()))
        .suppress(Suppressed.untilWindowCloses(Suppressed.BufferConfig.unbounded()))
        .toStream()
        .map((k, v) -> new KeyValue<>(k.key(), v))
        .to("output");
    

    Ad 2:

    input
        .groupByKey(Grouped.with(Serdes.String(), Serdes.Long())
        .windowedBy(SessionWindows.with(Duration.ofSeconds(30)))
        .aggregate(() -> Long.valueOf(0), (key, v1, v2) -> v1 + v2, (key, agg1, agg2) -> agg1 + agg2)
        .suppress(Suppressed.untilWindowCloses(Suppressed.BufferConfig.unbounded()))
        .toStream()
        .map((k, v) -> new KeyValue<>(k.key(), v))
        .to("output");
    

提交回复
热议问题