DataStream<String> data = ...
// append a Long 1 to each record to count it.
DataStream<Tuple2<String, Long>> withOnes = data.map(new AppendOne);
DataStream<Tuple2<String, Long>> 1minCnts = withOnes
// key by String field
.keyBy(0)
// define time window
.timeWindow(Time.of(1, MINUTES))
// sum ones of the Long field
// in practice you want to use an incrementally aggregating ReduceFunction and
// a WindowFunction to extract the start/end timestamp of the window
.sum(1);
// emit 1-min counts to wherever you need it
1minCnts.addSink(new YourSink());
// compute 5-min counts based on 1-min counts
DataStream<Tuple2<String, Long>> 5minCnts = 1minCnts
// key by String field
.keyBy(0)
// define time window of 5 minutes
.timeWindow(Time.of(5, MINUTES))
// sum the 1-minute counts in the Long field
.sum(1);
// emit 5-min counts to wherever you need it
5minCnts.addSink(new YourSink());
// continue with 1 day window and 1 week window
1条答案
按热度按时间4sup72z81#
是的,那是可能的。
如果您使用的是事件时间,那么您可以简单地以增加的时间间隔层叠窗口。所以你要:
请注意,这是可能的,因为:
sum是一个关联函数(可以通过求部分和来计算和)。
翻转窗口对齐,不重叠。
关于增量聚合
ReduceFunction
:通常,您希望在窗口操作的输出中具有窗口的开始和/或结束时间戳(否则,相同键的所有结果看起来相同)。窗口的开始和结束时间可以从
window
的参数apply()
a方法WindowFunction
. 然而,一个WindowFunction
不以增量方式聚合记录,而是收集记录并在窗口末尾聚合记录。因此,使用ReduceFunction
对于增量聚合和WindowFunction
将窗口的开始和/或结束时间附加到结果。文档讨论了细节。如果要使用处理时间来计算这个值,就不能级联窗口,而必须从输入数据流扇出到四个窗口函数。