flink的WindowFucntion按照计算原理的不同可以分为两大类:
一类是增量聚合函数:对应有ReduceFunction、AggregateFunction;
另一类是全量窗口函数,对应有ProcessWindowFunction(还有WindowFunction)。
代码如下:
//每隔5秒统计每个基站的日志数量
data.map(stationLog => ((stationLog.sid, 1)))
.keyBy(_._1)
.window(TumblingEventTimeWindows.of(Time.seconds(5)))
.reduce((v1, v2) => (v1._1, v1._2 + v2._2))
//每隔3秒计算最近5秒内,每个基站的日志数量
data.map(stationLog => ((stationLog.sid, 1)))
.keyBy(_._1)
.timeWindow(Time.seconds(5), Time.seconds(3))
.aggregate(new AggregateFunction[(String, Int), (String, Long), (String, Long)] {
override def createAccumulator() = ("", 0)
override def add(in: (String, Int), acc: (String, Long)) = {
(in._1, acc._2 + in._2)
}
override def getResult(acc: (String, Long)) = acc
override def merge(acc: (String, Long), acc1: (String, Long)) = {
(acc._1, acc1._2 + acc._2)
}
})
//每隔5秒统计每个基站的日志数量
data.map(stationLog => ((stationLog.sid, 1)))
.keyBy(_._1)
.timeWindow(Time.seconds(5))
.process(new
ProcessWindowFunction[(String, Int), (String, Int), String, TimeWindow] {
override def process(key: String, context: Context, elements: Iterable[(String,
Int)], out: Collector[(String, Int)]): Unit = {
println("-------")
out.collect((key, elements.size))
}
})