Kafka Streams SessionWindows中的油门

时间:2019-06-20 06:41:21

标签: java apache-kafka apache-kafka-streams

默认情况下,.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

我该如何解决?非常感谢您阅读我的问题:)

1 个答案:

答案 0 :(得分:0)