[英]Aggregation & grouping with Kafka streams
我正在研究卡夫卡流和国营商店。 我在下面有一个逻辑来根据键聚合消息组。
final Materialized<String, Sample, SessionStore<Bytes, byte[]>> abcStore =
Materialized.<String, Sample, SessionStore<Bytes,
byte[]>>as("topicforchangelog").withCachingDisabled();
KStream<String, Sample> abcStream = builder.stream("inprocess-topic");
SessionWindowedKStream<String, Sample> windowedKStream =
abcStream.groupByKey().windowedBy(SessionWindows.with(ofSeconds("36000")));
windowedKStream
.aggregate(
Aggregater::initialize,
Aggregater::merge,
Aggregater::merge,
abcStore)
.toStream()
.map((key, value) -> KeyValue.pair(key.key(), value))
.filter((k, v) -> v != null )
.selectKey((key, value) -> value.getId())
.peek(abcClass::logOut)
.to("outputtopic");
基于 Aggregate 类中的逻辑,我能够按预期合并以下消息。
Message 1 : Key : A , value : {"id" : "abc" , "names" : []}
Message 2 : Key : A , value : {"id" : null , "names" : [{"name" : "x"}]}
Message 3 : Key : A , value : {"id" : null , "names" : [{"name" : "xx"}]}
我得到的最后一条消息如下。
{"id" : "abc" , "names" : [{"name" : "x"}, {"name" : "xx"}]}
前三条消息在5分钟内被推送到主题。 _ _ _ 但是经过一段时间(一天或更长时间) ,如果我发送一条带有密钥“A”的消息-
Message 4 : Key : A , value : {"id" : null , "names" : [{"name" : "xxx"}}
我的期望是形成一个像
{"id" : "abc" , "names" : [{"name" : "x"}, {"name" : "xx"},{"name" : "xxx"}]}
但它没有发生。 我收到类似 {"id" : null , "names" : [{"name" : "xxx"}} 的输出
以“A”为键的“inprocess-topic”中存在的所有记录并未发生聚合。
这是因为
我们可以用 SessionWindow 概念来实现这个场景吗?
只是一个疯狂的猜测。 因为您使用的是窗口聚合,所以您可能需要考虑以下事项。
请参阅表中的第 6 行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.