如何使用KStreams来应用窗口{使用Java的用例}

时间:2018-11-22 07:48:42

标签: apache-kafka apache-kafka-streams

KStream<String, dummy> demoZone = builder.stream(fromTopic, Consumed.with(Serializer.getSerde(String.class), Serializer.getSerde(dummy.class)));

我想在1分钟的demoZone上应用窗口,并找到该1分钟数据的平均值

1 个答案:

答案 0 :(得分:1)

您可以使用如下所示的TimeWindow:

KTable<Windowed<String>, Long> averageCount = demoZone
        .groupByKey(Serialized.with(Serdes.String(), dummySerde()))
        .windowedBy(TimeWindows.of(TimeUnit.MINUTES.toMillis(5).advanceBy(TimeUnit.MINUTES.toMillis(1))))
        .reduce(// Your aggregate logic here);

您可以在此处找到有关Windowing的更多详细信息:
https://docs.confluent.io/current/streams/developer-guide/dsl-api.html#windowing