Kafka流是否有可能将输出写入两个不同的主题? [英] Is it possible for a Kafka stream to write output to two different topics?
问题描述
我需要一个流来对主题A中的值进行分组,将分组后的值发送至主题B,并在翻滚时间窗口内将这些分组后的值的总和发送至主题C. 有可能在卡夫卡这样做吗?还是流只能从一个主题读取和写入?
I need a stream to group values from topic A, send the grouped values to topic B, and send the sum of those grouped values over a tumbling time window to topic C. Is it possible to do that in Kafka? Or can a stream both read and write from one topic only?
推荐答案
是.例如,您可以拆分输入流并写入不同的主题:
Yes. For example you can split a input stream and write to different topics:
KStream stream = ...
KStream[] splitStream = stream.branch(...);
splitStream[0].to("topic-1");
splitStream[1].to("topic-2");
您还可以通过to(TopicNameExtractor)
使用更动态的方法.
You can also use a more dynamic approach via to(TopicNameExtractor)
.
您还可以广播"流以应用不同的逻辑.重用相同的KStream
变量可确保通过多个并行下游操作在逻辑上复制和处理每个记录:
You can also "broadcast" a stream to apply different logic. Reusing the same KStream
variable ensures that each record is logically duplicated and processed through multiple parallel downstream operations:
KStream stream = ...
KStream filterdStream = stream.filter();
KTable count = stream.groupBy().count();
这篇关于Kafka流是否有可能将输出写入两个不同的主题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!