0基础学习PyFlink——事件时间和运行时间的窗口

大纲

《0基础学习PyFlink------时间滚动窗口(Tumbling Time Windows)》一文中,我们使用的是运行时间(Tumbling ProcessingTime Windows)作为窗口的参考时间:

python 复制代码
    reduced=keyed.window(TumblingProcessingTimeWindows.of(Time.milliseconds(2))) \
                    .apply(SumWindowFunction(),
                        Types.TUPLE([Types.STRING(), Types.INT()]))

而得到的结果也是不稳定的。

这是因为每次运行时,CPU等系统资源的繁忙程度是不一样的,这就影响了最后的运行结果。

为了让结果稳定,我们可以不依赖运行时间(ProcessingTime),而使用不依赖于运行环境,只依赖于数据的事件时间(EventTime)。

一般,我们需要大数据处理的数据,往往存在一个字段用于标志该条数据的"顺序"。这个信息可以是单调递增的ID,也可以是不唯一的时间戳。我们可以将这类信息看做事件发生的时间。

那如何让输入的数据中的"事件时间"参与到窗口时长的计算中呢?这儿就要引入Watermark(水印)的概念。

假如我们把数据看成一张纸上的内容,水印则是这张纸的背景。它并不影响纸上内容的表达,只是系统要用它来做更多的事情。

将数据中表达"顺序"的数据转换成"时间",我们可以使用水印单调递增时间戳分配器

定制策略

python 复制代码
class ElementTimestampAssigner(TimestampAssigner):
    def extract_timestamp(self, value, record_timestamp)-> int:
        return int(value[1])
 ......       
    # define the watermark strategy
    watermark_strategy = WatermarkStrategy.for_monotonous_timestamps() \
        .with_timestamp_assigner(ElementTimestampAssigner())

for_monotonous_timestamps会分配一个水印单调递增时间戳分配器,然后使用with_timestamp_assigner告知输入数据中"顺序"字段的值。这样系统就会根据这个字段的值生成一个单调递增的时间戳。这个时间戳相对顺序就和输入数据一样,是稳定的。

比如上图中,会分别用2,1,4,3......来计算时间戳。

运行策略

然后对原始数据使用该策略,这样source_with_wartermarks中的数据就包含了时间戳。

python 复制代码
source_with_wartermarks=source.assign_timestamps_and_watermarks(watermark_strategy)

Reduce

这次我们使用TumblingEventTimeWindows,即事件时间(EventTime)窗口,而不是运行时间(ProcessingTime)窗口。

python 复制代码
     # keying
    keyed=source_with_wartermarks.key_by(lambda i: i[0]) 
    
    # reducing
    reduced=keyed.window(TumblingEventTimeWindows.of(Time.milliseconds(2))) \
                    .apply(SumWindowFunction(),
                        Types.TUPLE([Types.STRING(), Types.INT()]))

('E', 1) TimeWindow(start=0, end=2)

('E', 3) ('E', 2) TimeWindow(start=2, end=4)

('E', 4) ('E', 5) TimeWindow(start=4, end=6)

('E', 6) ('E', 7) TimeWindow(start=6, end=8)

('E', 8) ('E', 9) TimeWindow(start=8, end=10)

('E', 10) TimeWindow(start=10, end=12)

(E,1)

(E,2)

(E,2)

(E,2)

(E,2)

(E,1)

多运行几次,结果是稳定输出的。

我们再多关注下TimeWindow中的start和end,它们是不重叠的、步长为2、左闭右开的区间。这个符合滚动窗口特性。

完整代码

python 复制代码
from typing import Iterable

from pyflink.common import Types, Time, WatermarkStrategy
from pyflink.datastream import StreamExecutionEnvironment, RuntimeExecutionMode, WindowFunction
from pyflink.datastream.window import TumblingEventTimeWindows, TimeWindow, TumblingProcessingTimeWindows, SlidingProcessingTimeWindows
from pyflink.common.watermark_strategy import TimestampAssigner

class ElementTimestampAssigner(TimestampAssigner):
    def extract_timestamp(self, value, record_timestamp)-> int:
        return int(value[1])
   
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 = [("E",3),("E",1),("E",4),("E",2),("E",6),("E",5),("E",7),("E",8),("E",9),("E",10)]

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()

     # define the watermark strategy
    watermark_strategy = WatermarkStrategy.for_monotonous_timestamps() \
        .with_timestamp_assigner(ElementTimestampAssigner())
    
    source_with_wartermarks=source.assign_timestamps_and_watermarks(watermark_strategy)
        
     # keying
    keyed=source_with_wartermarks.key_by(lambda i: i[0]) 
    
    # reducing
    reduced=keyed.window(TumblingEventTimeWindows.of(Time.milliseconds(2))) \
                    .apply(SumWindowFunction(),
                        Types.TUPLE([Types.STRING(), Types.INT()]))
        
    # # define the sink
    reduced.print()

    # submit for execution
    env.execute()

if __name__ == '__main__':
    word_count()

滑动窗口案例

python 复制代码
from typing import Iterable

from pyflink.common import Types, Time, WatermarkStrategy
from pyflink.datastream import StreamExecutionEnvironment, RuntimeExecutionMode, WindowFunction
from pyflink.datastream.window import SlidingEventTimeWindows, TimeWindow
from pyflink.common.watermark_strategy import TimestampAssigner

class ElementTimestampAssigner(TimestampAssigner):
    def extract_timestamp(self, value, record_timestamp)-> int:
        return int(value[1])
   
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 = [("E",3),("E",1),("E",4),("E",2),("E",6),("E",5),("E",7),("E",8),("E",9),("E",10)]

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()
    
    # define the watermark strategy
    watermark_strategy = WatermarkStrategy.for_monotonous_timestamps() \
        .with_timestamp_assigner(ElementTimestampAssigner())
    
    source_with_wartermarks=source.assign_timestamps_and_watermarks(watermark_strategy)
        
     # keying
    keyed=source_with_wartermarks.key_by(lambda i: i[0]) 
    
    # reducing
    reduced=keyed.window(SlidingEventTimeWindows.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()

('E', 1) TimeWindow(start=0, end=2)

('E', 1) ('E', 2) TimeWindow(start=1, end=3)

('E', 3) ('E', 2) TimeWindow(start=2, end=4)

('E', 3) ('E', 4) TimeWindow(start=3, end=5)

('E', 4) ('E', 5) TimeWindow(start=4, end=6)

('E', 6) ('E', 5) TimeWindow(start=5, end=7)

('E', 6) ('E', 7) TimeWindow(start=6, end=8)

('E', 7) ('E', 8) TimeWindow(start=7, end=9)

('E', 8) ('E', 9) TimeWindow(start=8, end=10)

('E', 9) ('E', 10) TimeWindow(start=9, end=11)

('E', 10) TimeWindow(start=10, end=12)

(E,1)

(E,2)

(E,2)

(E,2)

(E,2)

(E,2)

(E,2)

(E,2)

(E,2)

(E,2)

(E,1)

通过TimeWindow的信息,我们看到这是一个步长为1、长度为2左闭右开的窗口。这个符合滑动窗口特点。

参考资料

相关推荐
呆呆敲代码的小Y19 分钟前
10 分钟搞懂 cua:开源 AI 操作电脑基础设施,附 Python 沙箱与 Agent 上手代码
人工智能·python·开源·ai agent·awesome·llm应用·cua
何升曜20 分钟前
Flink Slot 分配机制深度解析:从申请到部署的完整链路
flink
工具分享21 分钟前
带店托管必用爆单AI选品,一人公司轻松掌握
人工智能·python
l12586523 分钟前
# RAG低延迟架构设计:从5秒到500ms的优化全链路
数据库·人工智能·python·langchain
杨丰玮41827 分钟前
从零手写Java飞机躲障碍游戏|Swing绘图、鼠标跟随、计时器碰撞检测实战(五)
java·python·游戏·游戏引擎·图形渲染·动画·贴图
frjc29 分钟前
阿里云 ACK 环境 Arthas 在线调试指南
开发语言·python
Python私教40 分钟前
如意智影:如何让同一个人物在多个镜头里保持身份一致
人工智能·python·架构
Sayai1 小时前
Elasticsearch 日志索引设计:按小时切分 + 小时内 Rollover 兜底(ILM 实战,附完整脚本)
大数据·elasticsearch·搜索引擎
高洁011 小时前
智能体的“记忆”难题
python·深度学习·机器学习·transformer
三十岁老牛再出发1 小时前
8月21日总结
c++·python