0基础学习PyFlink——时间滑动窗口(Sliding Time Windows)

《0基础学习PyFlink------时间滚动窗口(Tumbling Time Windows)》我们介绍了不会有重复数据的时间滚动窗口。本节我们将介绍存在重复计算数据的时间滑动窗口。

关于滑动窗口,可以先看下《0基础学习PyFlink------个数滑动窗口(Sliding Count Windows)》。下图就是个数滑动窗口示意图。

我们看到个数滑动窗口也会因为窗口内数据不够而不被触发。但是时间滑动窗口则可以解决这个问题,我们只要把窗口改成时间类型即可。

相应的代码我们参考《0基础学习PyFlink------时间滚动窗口(Tumbling Time Windows)》,只要把TumblingProcessingTimeWindows改成SlidingProcessingTimeWindows,并增加一个偏移参数(Time.milliseconds(1))即可。这意味着我们将运行一个时间长度为2毫秒,每次递进1毫秒的窗口。

完整代码

python 复制代码
from typing import Iterable
import time
from pyflink.common import Types, Time
from pyflink.datastream import StreamExecutionEnvironment, RuntimeExecutionMode, WindowFunction
from pyflink.datastream.window import  TimeWindow, SlidingProcessingTimeWindows
   
class SumWindowFunction(WindowFunction[tuple, tuple, str, TimeWindow]):
    def apply(self, key: str, window: TimeWindow, inputs: Iterable[tuple]):
        print(*inputs, window)
        return [(key,  len([e for e in inputs]))]


word_count_data = [("A",2),("A",1),("A",4),("A",3),("A",6),("A",5),("A",7),("A",8),("A",9),("A",10),
                   ("A",11),("A",12),("A",13),("A",14),("A",15),("A",16),("A",17),("A",18),("A",19),("A",20)]

def word_count():
    env = StreamExecutionEnvironment.get_execution_environment()
    env.set_runtime_mode(RuntimeExecutionMode.STREAMING)
    # write all the data to one file
    env.set_parallelism(1)

    source_type_info = Types.TUPLE([Types.STRING(), Types.INT()])
    # define the source
    # mappging
    source = env.from_collection(word_count_data, source_type_info)
    # source.print()

    # keying
    keyed=source.key_by(lambda i: i[0]) 
    
    # reducing
    reduced=keyed.window(SlidingProcessingTimeWindows.of(Time.milliseconds(2), Time.milliseconds(1))) \
                    .apply(SumWindowFunction(),
                        Types.TUPLE([Types.STRING(), Types.INT()]))
        
    # # define the sink
    reduced.print()

    # submit for execution
    env.execute()

if __name__ == '__main__':
    word_count()

运行结果

运行两次上述代码,我们发现每次都不同,而且有重叠计算的元素。

('A', 2) ('A', 1) ('A', 4) TimeWindow(start=1698773292650, end=1698773292652)

('A', 2) ('A', 1) ('A', 4) ('A', 3) ('A', 6) ('A', 5) ('A', 7) ('A', 8) ('A', 9) ('A', 10) ('A', 11) TimeWindow(start=1698773292651, end=1698773292653)

(A,3)

(A,11)

('A', 3) ('A', 6) ('A', 5) ('A', 7) ('A', 8) ('A', 9) ('A', 10) ('A', 11) ('A', 12) ('A', 13) ('A', 14) ('A', 15) ('A', 16) ('A', 17) ('A', 18) ('A', 19) ('A', 20) TimeWindow(start=1698773292652, end=1698773292654)

(A,17)

('A', 2) ('A', 1) ('A', 4) TimeWindow(start=1698773319933, end=1698773319935)

('A', 2) ('A', 1) ('A', 4) ('A', 3) ('A', 6) ('A', 5) ('A', 7) ('A', 8) ('A', 9) ('A', 10) ('A', 11) ('A', 12) TimeWindow(start=1698773319934, end=1698773319936)

(A,3)

(A,12)

('A', 3) ('A', 6) ('A', 5) ('A', 7) ('A', 8) ('A', 9) ('A', 10) ('A', 11) ('A', 12) ('A', 13) ('A', 14) ('A', 15) ('A', 16) ('A', 17) ('A', 18) ('A', 19) ('A', 20) TimeWindow(start=1698773319935, end=1698773319937)

(A,17)

参考资料

相关推荐
旷世奇才李先生5 分钟前
PyCharm 安装使用教程
ide·python·pycharm
这里有鱼汤26 分钟前
“对象”?对象你个头!——Python世界观彻底崩塌的一天
后端·python
尘浮72836 分钟前
60天python训练计划----day59
开发语言·python
wh393339 分钟前
使用Python将PDF转换成word、PPT
python·pdf·word
船长@Quant1 小时前
数学视频动画引擎Python库 -- Manim Voiceover 语音服务 Speech Services
python·数学·manim·动画引擎·语音旁白
好开心啊没烦恼2 小时前
Python 数据分析:计算,分组统计1,df.groupby()。听故事学知识点怎么这么容易?
开发语言·python·数据挖掘·数据分析·pandas
lljss20203 小时前
Python11中创建虚拟环境、安装 TensorFlow
开发语言·python·tensorflow
空中湖3 小时前
tensorflow武林志第二卷第九章:玄功九转
人工智能·python·tensorflow
CodeCraft Studio4 小时前
CAD文件处理控件Aspose.CAD教程:使用 Python 将绘图转换为 Photoshop
python·photoshop·cad·aspose·aspose.cad
Python×CATIA工业智造6 小时前
Frida RPC高级应用:动态模拟执行Android so文件实战指南
开发语言·python·pycharm