Kafka流 - 根据消息组设置不同的时间窗口

时间:2018-03-05 22:57:35

标签: apache-kafka apache-kafka-streams

我想知道是否给出了KStream,可以根据消息组设置不同的时间窗口,例如,对于groupBy“A”5秒,对于groupBy“B”10秒......

KStream<String, Msg> stream = builder.stream(stringSerde, msgSerde, input);
stream.groupBy((key, msg) -> msg.getPool())
      .aggregate(init, agg, TimeWindows.of(wndLength).advanceBy(wndLength), msgSerde)
      ...

1 个答案:

答案 0 :(得分:4)

我们想到的最简单的方法是在.filter() / .branch()之前.groupBy().aggregate(),例如:

KStream<String, Msg> stream = builder.stream(stringSerde, msgSerde, input);
stream.filter((key, msg) -> msg.getPool().equals("A"))
      .groupBy((key, msg) -> msg.getPool())
      .aggregate(init, agg, TimeWindows.of(wndLength).advanceBy(wndLength), msgSerde)
      ...