将消息流式传输到多个主题

bm1*_*729 7 apache-kafka apache-kafka-streams

我有一个主要主题和多个谓词,每个谓词都有一个与之关联的输出主题.我想将每条记录发送到其谓词解析为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
Run Code Online (Sandbox Code Playgroud)

然而,这样一个中间话题似乎"笨拙".有更好的建议吗?

我在用:

  • 卡夫卡v0.10.1.1
  • Luwak v1.4.0

Mat*_*Sax 14

您可以简单地将多个过滤器并行应用于同一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
Run Code Online (Sandbox Code Playgroud)

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

  • @Cyber​​Knight 并不适合上面描述的用例:`branch()` 只允许您将每条记录放入至多一个输出流中(即,您可以将记录放入不同的主题中),而用例描述是编写每个记录记录到多个输出流(多个!=不同)。 (2认同)