2017-02-22 67 views
2

我有一個主題和多個謂詞,每個主題都有一個與之關聯的輸出主題。我想將每條記錄發送給謂詞解析爲真的所有主題。我使用Luwak來測試記錄滿足哪些謂詞(要使用這個庫來評估帶有謂詞列表的文檔,並告訴你哪些謂詞匹配 - 即我只調用一次來獲得滿足謂詞的列表)。將消息流式傳輸到多個主題

我想爲此使用卡夫卡流,但似乎沒有在KStream上適當的方法(KStream#分支只將記錄路由到單個主題)。

一個可能的方法如下:

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 

這種中間話題似乎「笨重」雖然。有更好的建議嗎?

我使用:

  • 卡夫卡v0.10.1.1
  • 魯瓦克V1.4.0

回答

5

你可以簡單的適用於平行多個過濾器以相同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 

每個記錄將被髮送到每個謂詞一次 - 它在概念上是一個broadc適用於所有過濾器,但記錄不會被物理複製,因此不存在內存開銷。