万字细说Flink中Window原理与使用
一、时间语义
学习flink的Window窗口,需要先了解下flink的三种时间语义。不同的时间语义对应不同的窗口函数。
1.Flink中的时间语义
-
在Flink的流式处理中,会涉及到时间的不同概念,如下图所示:
-
1. Event Time:是事件创建的时间。它通常由事件中的时间戳描述,例如采集的日志数据中,每一条日志都会记录自己的生成时间,Flink通过时间戳分配器访问事件时间戳。
-
2. Ingestion Time:是数据进入Flink的时间。
-
3. Processing Time:是每一个执行基于时间操作的算子的本地系统时间,与机器相关,默认的时间属性就是Processing Time。
2. 设置时间语义
-
在flink的流式处理中,绝大部分的业务都会使用eventTime,一般只在eventTime无法使用时,才会被迫使用ProcessingTime或者IngestionTime。
-
如果要使用EventTime,那么需要引入EventTime的时间属性,引入方式如下所示:
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment // 从调用时刻开始给env创建的每一个stream追加时间特征 env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime)
TimeCharacteristic
源码:public enum TimeCharacteristic { /** * Processing time for operators means that the operator uses the system clock of the machine * to determine the current time of the data stream. Processing-time windows trigger based * on wall-clock time and include whatever elements happen to have arrived at the operator at * that point in time. */ ProcessingTime, /** * Ingestion time means that the time of each individual element in the stream is determined * when the element enters the Flink streaming data flow. Operations like windows group the * elements based on that time, meaning that processing speed within the streaming dataflow * does not affect windowing, but only the speed at which sources receive elements. */ IngestionTime, /** * Event time means that the time of each individual element in the stream (also called event) * is determined by the event's individual custom timestamp. These timestamps either exist in * the elements from before they entered the Flink streaming dataflow, or are user-assigned at * the sources. The big implication of this is that it allows for elements to arrive in the * sources and in all operators out of order, meaning that elements with earlier timestamps may * arrive after elements with later timestamps. */ EventTime }
二、Window窗口
1.Window概述
-
streaming流式计算是一种被设计用于处理无限数据集的数据处理引擎,而无限数据集是指一种不断增长的本质上无限的数据集,而window是一种切割无限数据为有限块进行处理的手段。
-
Window是无限数据流处理的核心,Window将一个无限的stream拆分成有限大小的
"buckets"
桶,我们可以在这些桶上做计算操作。
2.Window简介
Window可以分成两类:
-
① CountWindow(计数窗口):按照指定的数据条数生成一个Window,与时间无关。
-
② TimeWindow(时间窗口):按照时间生成Window。
- 对于
TimeWindow
,可以根据窗口实现原理的不同分成三类:滚动窗口(Tumbling Window)、滑动窗口(Sliding Window)和会话窗口(Session Window)。
- 对于
2.1 滚动窗口(Tumbling Windows)
-
将数据依据固定的窗口长度对数据进行切片。
-
特点:时间对齐,窗口长度固定,没有重叠。
-
滚动窗口分配器将每个元素分配到一个指定窗口大小的窗口中,滚动窗口有一个固定的大小,并且不会出现重叠。例如:如果你指定了一个5分钟大小的滚动窗口,窗口的创建如下图所示
- 适用场景:适合做BI统计等(做每个时间段的聚合计算)。
2.2 滑动窗口(Sliding Windows)
-
滑动窗口是固定窗口的更广义的一种形式,滑动窗口由固定的窗口长度和滑动间隔组成。
-
特点:时间对齐,窗口长度固定,可以有重叠。
-
滑动窗口分配器将元素分配到固定长度的窗口中,与滚动窗口类似,窗口的大小由窗口大小参数来配置,另一个窗口滑动参数控制滑动窗口开始的频率。因此,滑动窗口如果滑动参数小于窗口大小的话,窗口是可以重叠的,在这种情况下元素会被分配到多个窗口中。
-
例如,你有10分钟的窗口和5分钟的滑动,那么每个窗口中5分钟的窗口里包含着上个10分钟产生的数据,如下图所示:
-
适用场景:对最近一个时间段内的统计(求某接口最近5min的失败率来决定是否要报警)。
2.3 会话窗口(Session Windows)
-
由一系列事件组合一个指定时间长度的timeout间隙组成,类似于web应用的session,也就是一段时间没有接收到新数据就会生成新的窗口。
-
特点:时间无对齐。
-
session窗口分配器通过session活动来对元素进行分组,session窗口跟滚动窗口和滑动窗口相比,不会有重叠和固定的开始时间和结束时间的情况,相反,当它在一个固定的时间周期内不再收到元素,即非活动间隔产生,那个这个窗口就会关闭。一个session窗口通过一个session间隔来配置,这个session间隔定义了非活跃周期的长度,当这个非活跃周期产生,那么当前的session将关闭并且后续的元素将被分配到新的session窗口中去。
3.Window API
3.1 TimeWindow
- TimeWindow是将指定时间范围内的所有数据组成一个window,一次对一个window里面的所有数据进行计算。
- flink有两种开窗的方式:
-
1.直接调用
timeWindow
。传入一个参数就是滚动窗口,传入两个参数就是滑动窗口。timeWindow
源码如下:如果传入一个参数(窗口大小),就是滚动窗口,本质是根据时间语义分别调用了
TumblingProcessingTimeWindows
和TumblingEventTimeWindows
/** * Windows this {@code KeyedStream} into tumbling time windows. * * <p>This is a shortcut for either {@code .window(TumblingEventTimeWindows.of(size))} or * {@code .window(TumblingProcessingTimeWindows.of(size))} depending on the time characteristic * set using * {@link org.apache.flink.streaming.api.environment.StreamExecutionEnvironment#setStreamTimeCharacteristic(org.apache.flink.streaming.api.TimeCharacteristic)} * * @param size The size of the window. */ public WindowedStream<T, KEY, TimeWindow> timeWindow(Time size) { if (environment.getStreamTimeCharacteristic() == TimeCharacteristic.ProcessingTime) { return window(TumblingProcessingTimeWindows.of(size)); } else { return window(TumblingEventTimeWindows.of(size)); } }
如果传入两个参数(窗口大小,和滑动步长),就是滑动窗口,也是根据时间语义实现了
SlidingProcessingTimeWindows
和SlidingEventTimeWindows
/** * Windows this {@code KeyedStream} into sliding time windows. * * <p>This is a shortcut for either {@code .window(SlidingEventTimeWindows.of(size, slide))} or * {@code .window(SlidingProcessingTimeWindows.of(size, slide))} depending on the time * characteristic set using * {@link org.apache.flink.streaming.api.environment.StreamExecutionEnvironment#setStreamTimeCharacteristic(org.apache.flink.streaming.api.TimeCharacteristic)} * * @param size The size of the window. */ public WindowedStream<T, KEY, TimeW
-