【发布时间】:2020-05-04 06:04:11
【问题描述】:
我有一个 kafka 流,我需要一个执行以下操作的处理器:
使用 45 秒跳跃窗口和 5 秒提前计算基于域对象的一维的前 5 个计数。例如,如果流包含 Clickstream 数据,我需要按域名查看的前 5 个 url,但也需要在跳跃窗口中进行窗口化。
我见过一些做窗口计数的例子,例如:
KStream<String, GenericRecord> pageViews = ...;
// Count page views per window, per user, with hopping windows of size 5 minutes that advance every 1 minute
KTable<Windowed<String>, Long> windowedPageViewCounts = pageViews
.groupByKey(Grouped.with(Serdes.String(), genericAvroSerde))
.windowedBy(TimeWindows.of(Duration.ofMinutes(5).advanceBy(Duration.ofMinutes(1))))
.count()
MusicExample 上的 Top n 聚合,例如:
songPlayCounts.groupBy((song, plays) ->
KeyValue.pair(TOP_FIVE_KEY,
new SongPlayCount(song.getId(), plays)),
Grouped.with(Serdes.String(), songPlayCountSerde))
.aggregate(TopFiveSongs::new,
(aggKey, value, aggregate) -> {
aggregate.add(value);
return aggregate;
},
(aggKey, value, aggregate) -> {
aggregate.remove(value);
return aggregate;
},
Materialized.<String, TopFiveSongs, KeyValueStore<Bytes, byte[]>>as(TOP_FIVE_SONGS_STORE)
.withKeySerde(Serdes.String())
.withValueSerde(topFiveSerde)
);
我似乎无法将 2 结合起来 - 我同时获得窗口和前 n 个聚合。有什么想法吗?
【问题讨论】:
标签: apache-kafka apache-kafka-streams