在flink中定义一个时间窗口后如下:
val lines = socket.timeWindowAll(Time.seconds(5))
如何计算该特定 5 秒窗口内的记录数?
执行计数聚合的最有效方法是ReduceFunction
。然而,reduce
有输入和输出类型必须相同的限制。所以你必须将输入转换为Int
在应用窗口之前:
val socket: DataStream[(String)] = ???
val cnts: DataStream[Int] = socket
.map(_ => 1) // convert to 1
.timeWindowAll(Time.seconds(5)) // group into 5 second windows
.reduce( (x, y) => x + y) // sum 1s to count
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)