背景
在流计算中,数据流是无限的,无法直接进行计算,因此Flink提出了window的概念(若干元素的集合)作为流计算的基本单元进行数据处理。
窗口机制
窗口机制实质上是Flink的算子operator对数据流的处理过程:数据流如何被拆分成window,何时触发计算逻辑等,如下图所示。
处理过程: 当数据流中的元素到达算子operator后,首先由WindowAssigner决定将该元素分配到哪个窗口(包括创建窗口);每个窗口对应一个Trigger,当有新的元素插入或者定时器超时后,如果存在Evictor则通过它对窗口中的元素进行过滤;否则,直接调用窗口函数进行逻辑计算输出结果;
关键组件
- WindowAssigner
作用:将流中的元素分配到对应窗口(零个或多个);
A WindowAssigner assigns zero or more Windows to an element;
- Window
作用:若干元素的集合;
创建:数据流的元素到达时由WindowAssigner分配和创建;
Generally speaking, a window defines a finite set of elements on an unbounded stream. This set can be based on time (as in our previous examples), element counts, a combination of counts and time, or some custom logic to assign elements to windows.
- Trigger
作用:决定窗口何时被计算或清除;
特点:每个窗口都绑定一个Trigger;
触发时机:有新的元素插入或者Trigger上的定时器超时;
执行动作:continue(不做任何操作),fire(处理窗口数据),purge(移除窗口和窗口中的数据),或者 fire + purge;
A Trigger determines when a pane of a window should be evaluated to emit the results for that part of the window.
- Evictor(可选)
作用:过滤窗口中的元素,相当于filter;
执行时机:trigger后,window function后;
An Evictor can remove elements from a pane before/after the evaluation of WindowFunction and after the window evaluation gets triggered by a Trigger;
- Window Functions
作用:具体的处理逻辑;
参考: