繁体   English   中英

Kafka Streams分组依据和串联

[英]Kafka Streams group by and concatenation

我有一个接收记录的Kafka流,并且我想根据特定字段连接消息。

流中的消息如下所示:

Key: 2099
Payload{
  email: tom@emample.com
  eventCode: 2099
}

预期产量:

key: 2099
Payload{
    emails: tom@example, bill@acme.com, jane@example.com
}

我可以使流正常运行,但是我不确定该包含什么。

到目前为止,这是我所做的。 我不确定是否应使用映射,聚合或缩小或这些操作的组合。

final StreamsBuilder builder = new StreamsBuilder();
KStream<String, Payload> inputStream = builder.stream(INPUT_TOPIC);

inputStream
        .groupByKey()
        .windowedBy(TimeWindows.of(TimeUnit.MINUTES.toMillis(300000)))

                                  // Not sure what to do here …..

}).to (OUTPUT_TOPIC );

可能是这样的

inputStream.groupByKey().windowedBy(TimeWindows.of(TimeUnit.MINUTES.toMillis(300000)))
.aggregate(PayloadAggr::new, new Aggregator<String, Payload, PayloadAggr>() {
        @Override
        public PayloadAggr apply(String key, Payload newValue, PayloadAggr result) {
            result.setKey(key);
            if(result.getEmails()==null){
                result.setEmails(newValue.getEmail());
            }else{
                result.setEmails(result.getEmails() + "," + newValue.getEmail());
            }
            return result;
        }
    }, .../* You serdes and store */}).toStream().to(OUTPUT_TOPIC);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM