Flink入门系列06-window

窗口(window)概念

窗口,就是把无界的数据流,依据一定的规则划分成一段一段的有界数据流。既然划分为有界数据流,通常都是为了”聚合“。
在这里插入图片描述
Keyedwindow 重要特性:任何一个窗口,都绑定在自己所属的 key 上,不同 key 的数据肯定不会划分到相同的窗口中去。

窗口类型

  • 滚动窗口
  • 滑动窗口
  • 会话窗口:没有固定的窗口长度,也没有固定的滑动步长,而是根据数据流中前后两个事件的时间间隔是否超出阈值来划分。

窗口计算 API 模板

  • KeyedWindows

    stream
    	.keyBy(...) 
    	.window(...)    // required
    	[.trigger(...)] // optional
    	[.evictor(...)] // optional
    	[.allowedLateness(...)]    // optional
    	[.sideOutputLateData(...)] // optional
    	.reduce/aggregate/apply()  // required
    	[.getSideOutput(...)] // optional
    
  • NonKeyedWindows

    stream
    	.windowAll(...)    // required
    	[.trigger(...)]    // optional
    	[.evictor(...)]    // optional
    	[.allowedLateness(...)]    // optional
    	[.sideOutputLateData(...)] // optional
    	.reduce/aggregate/apply()  // required
    	[.getSideOutput(...)] // optional
    

窗口计算 API 示例

  • 全局窗口

    // 全局 计数滚动窗口
    beanStream.countWindowAll(10)  // 10条数据一个窗口
            .apply(new AllWindowFunction<EventBean2, String, GlobalWindow>() {
                @Override
                public void apply(GlobalWindow window, Iterable<EventBean2> values, Collector<String> out) throws Exception {
    
                }
            });
    
    
    // 全局 计数滑动窗口
    beanStream.countWindowAll(10, 2); // 窗口长度为10条数据,滑动步长为2条数据
    /*.apply()*/
    
    
    // 全局 事件时间滚动窗口
    beanStream.windowAll(TumblingEventTimeWindows.of(Time.seconds(30))) // 窗口长度为30s的滚动窗口
            .apply(new AllWindowFunction<EventBean2, String, TimeWindow>() {
                @Override
                public void apply(TimeWindow window, Iterable<EventBean2> values, Collector<String> out) throws Exception {
    
                }
            });
    
    
    // 全局 事件时间滑动窗口
    beanStream.windowAll(SlidingEventTimeWindows.of(Time.seconds(30), Time.seconds(10))); // 窗口长度:30s,滑动步长:10s
    /*.apply()*/
    
    // 全局  事件时间会话窗口
    beanStream.windowAll(EventTimeSessionWindows.withGap(Time.seconds(30)));  // 前后两个事件的间隙超过30s就划分窗口
    /*.apply()*/
    
    // 全局  处理时间滚动窗口
    beanStream.windowAll(TumblingProcessingTimeWindows.of(Time.seconds(30)));
    
    
    // 全局  处理时间滑动窗口
    beanStream.windowAll(SlidingProcessingTimeWindows.of(Time.seconds(30), Time.seconds(10)));
    
    // 全局  处理间会话窗口
    beanStream.windowAll(ProcessingTimeSessionWindows.withGap(Time.seconds(30)));
    
  • Keyed 窗口

    KeyedStream<EventBean2, Long> keyedStream = beanStream.keyBy(EventBean2::getGuid);
    
    // Keyed 计数滚动窗口
    keyedStream.countWindow(10);
    
    
    // Keyed 计数滑动窗口
    keyedStream.countWindow(10, 2);
    
    
    // Keyed 事件时间滚动窗口
    keyedStream.window(TumblingEventTimeWindows.of(Time.seconds(30)));
    
    
    // Keyed 事件时间滑动窗口
    keyedStream.window(SlidingEventTimeWindows.of(Time.seconds(30), Time.seconds(10)));
    
    
    // Keyed  事件时间会话窗口
    keyedStream.window(EventTimeSessionWindows.withGap(Time.seconds(30)));
    
    
    // Keyed  处理时间滚动窗口
    keyedStream.window(TumblingProcessingTimeWindows.of(Time.seconds(30)));
    
    
    // Keyed  处理时间滑动窗口
    keyedStream.window(SlidingProcessingTimeWindows.of(Time.seconds(30), Time.seconds(10)));
    
    
    // Keyed  处理时间会话窗口
    keyedStream.window(ProcessingTimeSessionWindows.withGap(Time.seconds(30)));
    

窗口聚合算子

窗口聚合算子整体分为两类

  • 增量聚合算子,一次取一条数据,用聚合函数对中间累加器更新;窗口触发时,取累加器输出结果。
    如 min、max、minBy、maxBy、sum、reduce、aggregate
  • 全量聚合算子,数据”攒“在状态容器中,窗口触发时,把整个窗口的数据交给聚合函数。
    如 apply、process

注意点

  1. window 和 windowAll 区别
    在 keyby 后数据分流,window是把不同的key分开聚合成窗口,而 windowall 则把所有的 key 都聚合起来,所以 windowall 的并行度只能为1,而 window 可以有多个并行度。

  2. 全量聚合算子 process 和 apply 区别
    apply 只能用于 window 之后

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值