【问题标题】:Streaming messages to multiple topics将消息流式传输到多个主题
【发布时间】:2017-07-12 07:51:25
【问题描述】:

我有一个主主题和多个谓词,每个谓词都有一个与之关联的输出主题。我想将每条记录发送到谓词解析为 true 的所有主题。我正在使用 Luwak 测试记录满足哪些谓词(要使用这个库,您可以使用谓词列表评估文档,它会告诉您哪些匹配 - 即我只调用一次以获得满足的谓词列表)。

我正在尝试为此使用 Kafka Streams,但 KStream 上似乎没有合适的方法(KStream#branch 仅将记录路由到单个主题)。

一种可能的方法如下:

Stream from master
Map the values into a format with the original content and the list of matching predicates
Stream to an intermediate with-matches topic

For each predicate/output topic
    Stream from intermediate with-matches topic
    Filter "does list of matches predicates contain predicate ID"
    Map the values to just the original content
    Stream to corresponding output topic

不过,这样一个中间话题似乎“笨拙”。有更好的建议吗?

我正在使用:

  • Kafka v0.10.1.1
  • Luwak v1.4.0

【问题讨论】:

    标签: apache-kafka apache-kafka-streams


    【解决方案1】:

    您可以简单地将多个过滤器并行应用到同一个KStream 实例:

    KStream stream = ...
    
    stream.filter(new MyPredicate1()).to("output-topic-1");
    stream.filter(new MyPredicate2()).to("output-topic-2");
    stream.filter(new MyPredicate3()).to("output-topic-3");
    // ... as as many as you need
    

    每条记录将被发送到每个谓词一次 - 从概念上讲,它是对所有过滤器的广播,但记录不会被物理复制,因此没有内存开销。

    【讨论】:

    • 也可以使用分支。
    • @Cyber​​Knight 不适合上面描述的用例:branch() 只允许您将每条记录放入至多一个输出流中(即,您可以将记录放入不同的主题中),而使用case describe 是将每条记录写入多个输出流(多个!=不同)。
    • 我明白了。那么提供的答案就足够了。你是对的!
    猜你喜欢
    • 2018-11-18
    • 2018-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-15
    • 2014-08-15
    • 2020-11-05
    相关资源
    最近更新 更多