【发布时间】:2017-10-11 21:07:11
【问题描述】:
我正在尝试找到一种方法来重新排序主题分区中的消息并将已排序的消息发送到新主题。
我有发送以下格式的字符串消息的 Kafka 发布者:
{system_timestamp}-{event_name}?{parameters}
例如:
1494002667893-client.message?chatName=1c&messageBody=hello
1494002656558-chat.started?chatName=1c&chatPatricipants=3
此外,我们为每条消息添加一些消息键,将它们发送到相应的分区。
我想做的是根据消息的 {system-timestamp} 部分并在 1 分钟的窗口内重新排序事件,因为我们的发布者不保证消息会在符合 {system-timestamp} 值。
例如,我们可以先向主题传递 {system-timestamp} 值较大的消息。
我研究了 Kafka Stream API 并找到了一些关于消息窗口和聚合的示例:
Properties streamsConfiguration = new Properties();
streamsConfiguration.put(StreamsConfig.APPLICATION_ID_CONFIG, "stream-sorter");
streamsConfiguration.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
streamsConfiguration.put(StreamsConfig.ZOOKEEPER_CONNECT_CONFIG, "localhost:2181");
streamsConfiguration.put(StreamsConfig.KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
streamsConfiguration.put(StreamsConfig.VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
KStreamBuilder builder = new KStreamBuilder();
KStream<String, String> stream = builder.stream("events");
KGroupedStream<String>, String> groupedStream = stream.groupByKey();//grouped events within partion.
/* commented since I think that I don't need any aggregation, but I guess without aggregation I can't use time windowing.
KTable<Windowed<String>, String> windowedEvents = stream.groupByKey().aggregate(
() -> "", // initial value
(aggKey, value, aggregate) -> aggregate + "", // aggregating value
TimeWindows.of(1000), // intervals in milliseconds
Serdes.String(), // serde for aggregated value
"test-store"
);*/
但是接下来我应该如何处理这个分组流?我没有看到任何 'sort() (e1,e2) -> e1.compareTo(e2)' 方法可用,windows 也可以应用于像 aggregation() 这样的方法em>, reduce() ,count() ,但我认为我不需要任何消息数据操作。
如何在 1 分钟窗口中重新排序消息并将它们发送到另一个主题?
【问题讨论】:
标签: java stream apache-kafka messaging