默认情况下,.windowedBy(SessionWindows.with(...))
将返回每个新的传入记录。那么,在返回当前会话窗口的最后结果之前,我应该至少等待1秒钟吗?
我正在尝试单词计数示例:
final KStream<String, String> source = builder.stream("streams-plaintext-input");
final KStream<String, Long> wordCounts = source
// Split each text line, by whitespace, into words.
.flatMapValues(value -> Arrays.asList(value.toLowerCase(Locale.getDefault()).split(" ")))
// Group the stream by word to ensure the key of the record is the word.
.groupBy((key, word) -> word)
.windowedBy(SessionWindows.with(Duration.ofSeconds(10)))
// Count the occurrences of each word (message key).
.count(Materialized.with(Serdes.String(), Serdes.Long()))
.suppress(Suppressed.untilTimeLimit(Duration.ofSeconds(1), Suppressed.BufferConfig.unbounded()))
// Convert to KStream<String, Long>
.toStream((windowedId, count) -> windowedId.key());
wordCounts.foreach((word, count) -> {
System.out.println(word + " : " + count);
});
这是生产者的输入和客户的结果,这实际上是错误的:
$ bin/kafka-console-producer.sh --broker-list localhost:9092 --topic streams-plaintext-input
>hello kafka stream
(nothing)
>hello kafka stream
hello : 1
kafka : 1
stream : 1
>hello kafka stream
hello : null
kafka : 1
stream : 1
我该如何解决?非常感谢您阅读我的问题:)
答案 0 :(得分:0)
KTable#suppress()
运算符是您这种情况下的朋友。
查看文档和有关它的详细博客文章: