Kafka Streams字数统计如何工作?

时间:2019-05-13 00:41:12

标签: apache-kafka apache-kafka-streams

我很难理解基本的kafka流示例:

LIVE

// Construct a `KStream` from the input topic "streams-plaintext-input", where message values
// represent lines of text (for the sake of this example, we ignore whatever may be stored
// in the message keys).  The default key and value serdes will be used.
final KStream<String, String> textLines = builder.stream(inputTopic);

final Pattern pattern = Pattern.compile("\\W+", Pattern.UNICODE_CHARACTER_CLASS);

final KTable<String, Long> wordCounts = textLines
  // Split each text line, by whitespace, into words.  The text lines are the record
  // values, i.e. we can ignore whatever data is in the record keys and thus invoke
  // `flatMapValues()` instead of the more generic `flatMap()`.
  .flatMapValues(value -> Arrays.asList(pattern.split(value.toLowerCase())))
  // Group the split data by word so that we can subsequently count the occurrences per word.
  // This step re-keys (re-partitions) the input data, with the new record key being the words.
  // Note: No need to specify explicit serdes because the resulting key and value types
  // (String and String) match the application's default serdes.
  .groupBy((keyIgnored, word) -> word)
  // Count the occurrences of each word (record key).
  .count();

// Write the `KTable<String, Long>` to the output topic.
wordCounts.toStream().to(outputTopic, Produced.with(Serdes.String(), Serdes.Long()));

有人可以解释.flatMapValues部分吗?

据我所知,flatMapValues将KStream<String, String>转到 KStream<String, List<String>>,那么随后的.groupBy如何链接才能以某种方式拥有String, String输入参数?

1 个答案:

答案 0 :(得分:0)

.flatMap是一个运算符,当它返回一个集合时,会将其“扁平化”的单个元素返回到单个项目中的下一个运算符