dav*_*lin 5 apache-flink flink-streaming
我有一组经过加密的数据流,需要计算不同时间段(1分钟,5分钟,1天,1周)的滚动计数。
是否可以在单个应用程序中计算所有四个窗口计数?
是的,那是可能的。
如果使用事件时间,则可以简单地以增加的时间间隔级联窗口。所以你也是:
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
Run Code Online (Sandbox Code Playgroud)
请注意,这是可能的,因为:
关于渐进聚合的评论ReduceFunction:
通常,您希望在窗口操作的输出中包含窗口的开始和/或结束时间戳(否则,同一键的所有结果看起来都相同)。窗口的开始和结束时间可以从window的apply()方法的参数访问WindowFunction。但是,a WindowFunction不会增量聚合记录,而是收集它们并在窗口末尾聚合记录。因此,使用a ReduceFunction进行增量聚合并使用a WindowFunction将窗口的开始和/或结束时间附加到结果上会更加有效。该文档讨论了详细信息。
如果要使用处理时间进行计算,则无法级联窗口,而必须将输入数据流扇出成四个窗口函数。
| 归档时间: |
|
| 查看次数: |
1389 次 |
| 最近记录: |