Kafka Streams 2.1.1 class cast while flushing timed aggregation to store

与世无争的帅哥 提交于 2019-12-01 12:56:02

There two option to solve that issue:

  1. use TimeWindowedKStream::aggregate(final Initializer<VR> initializer, final Aggregator<? super K, ? super V, VR> aggregator, final Materialized<K, VR, WindowStore<Bytes, byte[]>> materialized);

  2. use KStream::groupByKey(final Grouped<K, V> 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");

To make this work with TopologyTestDriver, you would need to advance the clock time, which it seems has no effect on the Suppress step. A workaround is to allow your test to override the Suppress config with a setting like this:

Suppressed.untilTimeLimit(Duration.ZERO, BufferConfig.unbounded()) 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!