我想要达到的目的是根据用户对我从Kafka主题接收到的一些消息进行分组,并打开它们的窗口,以便在(5分钟)窗口中聚合我接收到的消息。然后我想收集每个窗口中的所有聚合,以便立即处理它们,并将它们添加到我在5分钟间隔内收到的所有消息的报告中。
最后一点似乎是困难的部分,因为Kafka流似乎没有提供(至少我找不到它!)任何可以在一个“有限”流中收集所有与窗口相关的内容并在一个地方进行处理的东西。
这是我实现的代码
StreamsBuilder builder = new StreamsBuilder();
KStream<UserId, Message> messages = builder.stream("KAFKA_TOPIC");
TimeWindowedKStream<UserId, Message> windowedMessages =
messages.
groupByKey().windowedBy(TimeWindows.of(SIZE_MS));
KTable<Windowed<UserId>, List<Message>> messagesAggregatedByWindow =
windowedMessages.
aggregate(
() -> new LinkedList<>(), new MyAggregator<>(),
Materialized.with(new MessageKeySerde(), new MessageListSerde())
);
messagesAggregatedByWindow.toStream().foreach((key, value) -> log.info("({}), KEY {} MESSAGE {}", value.size(), key, value.toString()));
KafkaStreams streams = new KafkaStreams(builder.build(), config);
streams.start();
结果是
KEY [UserId(82770583)@1531502760000/1531502770000] Message [Message(userId=UserId(82770583),message="a"),Message(userId=UserId(82770583),message="b"),Message(userId=UserId(82770583),message="d")]
KEY [UserId(77082590)@1531502760000/1531502770000] Message [Message(userId=UserId(77082590),message="g")]
KEY [UserId(85077691)@1531502750000/1531502760000] Message [Message(userId=UserId(85077691),message="h")]
KEY [UserId(79117307)@1531502780000/1531502790000] Message [Message(userId=UserId(79117307),message="e")]
KEY [UserId(73176289)@1531502760000/1531502770000] Message [Message(userId=UserId(73176289),message="r"),Message(userId=UserId(73176289),message="q")]
KEY [UserId(92077080)@1531502760000/1531502770000] Message [Message(userId=UserId(92077080),message="w")]
KEY [UserId(78530050)@1531502760000/1531502770000] Message [Message(userId=UserId(78530050),message="t")]
KEY [UserId(64640536)@1531502760000/1531502770000] Message [Message(userId=UserId(64640536),message="y")]
每个窗口都有许多日志行,它们与其他窗口混合在一起。
我想要的是:
// Hypothetical implementation
windowedMessages.streamWindows((interval, window) -> process(interval, window));
方法过程类似于:
// Hypothetical implementation
void process(Interval interval, WindowStream<UserId, List<Message>> windowStream) {
// Create report for the whole window
Report report = new Report(nameFromInterval());
// Loop on the finite iterable that represents the window content
for (WindowStreamEntry<UserId, List<Message>> entry: windowStream) {
report.addLine(entry.getKey(), entry.getValue());
}
report.close();
}
结果将按如下方式分组(每个报告都是对my callback:void process(…)的调用),每个窗口的提交将在处理整个窗口时提交:
Report 1:
KEY [UserId(85077691)@1531502750000/1531502760000] Message [Message(userId=UserId(85077691),message="h")]
Report 2:
KEY [UserId(82770583)@1531502760000/1531502770000] Message [Message(userId=UserId(82770583),message="a"),Message(userId=UserId(82770583),message="b"),Message(userId=UserId(82770583),message="d")]
KEY [UserId(77082590)@1531502760000/1531502770000] Message [Message(userId=UserId(77082590),message="g")]
KEY [UserId(73176289)@1531502760000/1531502770000] Message [Message(userId=UserId(73176289),message="r"),Message(userId=UserId(73176289),message="q")]
KEY [UserId(92077080)@1531502760000/1531502770000] Message [Message(userId=UserId(92077080),message="w")]
KEY [UserId(78530050)@1531502760000/1531502770000] Message [Message(userId=UserId(78530050),message="t")]
KEY [UserId(64640536)@1531502760000/1531502770000] Message [Message(userId=UserId(64640536),message="y")]
Report 3
KEY [UserId(79117307)@1531502780000/1531502790000] Message [Message(userId=UserId(79117307),message="e")]
1条答案
按热度按时间o4tp2gmn1#
我也有同样的疑问。我和库的开发人员谈过,他们说这是一个非常常见的请求,但还没有实现。很快就会发布。
您可以在此处找到更多信息:https://cwiki.apache.org/confluence/display/kafka/kip-328%3a+ability+to+suppress+updates+for+ktables