Flink_06_ProcessAPI(个人总结)

    声明: 1. 本文为我的个人复习总结, 并那种从零基础开始普及知识 内容详细全面, 言辞官方的文章
              2. 由于是个人总结, 所以用最精简的话语来写文章
              3. 若有错误不当之处, 请指出

侧输出流(SideOutput)

即分支流, 可以用来接收迟到数据, 也可以用来将数据分类成多个支流

对于滑动窗口, 有很多窗口重叠, 当迟到数据被所有窗口都不接收时, 它才会进入侧输出流

只有Process这种最底层的API, 才能通过环境上下文去使用侧输出流

案例: 将温度值低于30度的数据输出到 SideOutput

// 定义侧输出流标签, 注意得是其匿名实现类

final OutputTag<SensorReading> lowTempTag = new OutputTag<SensorReading>("lowTemp") { };

SingleOutputStreamOperator<SensorReading> highTempStream = dataStream.process(new ProcessFunction<SensorReading, SensorReading>( ) {
    @Override
    public void processElement(SensorReading value, Context ctx, Collector<SensorReading> out) {
        if (value.getTemperature( ) < 30) {
            ctx.output(lowTempTag, value);
        } else {
            out.collect(value);
        }
    }
});
DataStream<SensorReading> lowTempStream = highTempStream.getSideOutput(lowTempTag);
highTempStream.print("high");
lowTempStream.print("low");

8种ProcessAPI:

  1. ProcessFunction

  2. KeyedProcessFunction

    得先keyBy,

    会处理流的每一个元素, 以out.collect(xxx)的方式输出任意多个元素

    • .processElement(SensorReading value, Context ctx, Collector<O> out)

      ctx 可以

      1. 访问元素的时间戳

      2. 访问元素的key

      3. 将数据输出到侧输出流

      4. 访问TimerService(ctx.timerService( ))

        TimerService:

        方法:

        1. EventTime相关
          • long currentWatermark( ) 返回当前数据的事件时间
          • void registerEventTimeTimer(long timestamp) 注册当前key的定时器
          • void deleteEventTimeTimer(long timestamp) 删除定时器, 如果没有则不执行
        2. ProcessingTime相关
          • long currentProcessingTime( ) 返回当前数据的处理时间
          • void registerProcessingTimeTimer(long timestamp) 注册当前key的定时器
          • void deleteProcessingTimeTimer(long timestamp) 删除定时器, 如果没有则不执行
        • 当定时器Timer触发后, 会执行回调函数onTimer( )

        • timestamp 是定时器所设定的触发运行的时间戳, 如果注册一个已经过期的时间, 那么当再次输入数据时 它才会触发定时器

        • 若注册窗口关闭时启动的定时器, 最好在WindowEndTime的基础上延迟1s;

          因为到了临界点, 既要触发窗口计算, 又要触发定时器;

          定时器任务又依赖于先窗口计算完毕, 所以给个1s的延迟较好

        案例需求: 如果温度值在10秒钟之内(ProcessingTime)连续上升, 则报警

        public class TempIncreaseWarning extends KeyedProcessFunction<String, SensorReading, String> {
            private Integer interval;
        
            public TempIncreaseWarning(Integer interval) {
                this.interval = interval;
            }
        
            // 记录 上一次温度
            private ValueState<Double> lastTempState;
            // 记录 定时器触发时间
            private ValueState<Long> timerTsState;
        
            @Override
            public void open(Configuration parameters) throws Exception {
                lastTempState = getRuntimeContext( ).getState(new ValueStateDescriptor<Double>("last-temp", Double.class, Double.MIN_VALUE));
                timerTsState = getRuntimeContext( ).getState(new ValueStateDescriptor<Long>("timer-ts", Long.class));
            }
        
        
            @Override
            public void processElement(SensorReading value, Context ctx, Collector<String> out) throws Exception {
                // 取出状态
                Double lastTemp = lastTempState.value( );
                Long timerTs = timerTsState.value( );
        
                // 每当温度上升时 && 暂无定时器
                if (value.getTemperature( ) > lastTemp && timerTs == null) {
                    long ts = ctx.timerService( ).currentProcessingTime( ) + interval * 1000L;
                    // 注册定时器
                    ctx.timerService( ).registerProcessingTimeTimer(ts);
                    // 为了后续删除定时器能找到注册时间戳
                    timerTsState.update(ts);
                }
                // 每当温度下降时 && 定时器不为空
                else if (value.getTemperature( ) <= lastTemp && timerTs != null) {
                    // 清除定时器,注意不能用ts,我们要找的是注册定时器的那个时间戳才对
                    ctx.timerService( ).deleteProcessingTimeTimer(timerTs);
                    timerTsState.clear( );
                }
                // 更新温度状态
                lastTempState.update(value.getTemperature( ));
            }
        
            @Override
            public void onTimer(long timestamp, OnTimerContext ctx, Collector<String> out) {
                out.collect("传感器" + ctx.getCurrentKey( ) + "的温度连续" + interval + "秒上升");
                timerTsState.clear( );
            }
        }
        
  3. CoProcessFunction

    connect后的流再.process

    有processElement1( ) 和 processElement2( )

  4. ProcessJoinFunction

  5. BroadcastProcessFunction

    A流有1个分区, B流有4个分区, B流要用到A流的数据, 所以需要将A流1个分区的数据广播到B流的4个分区

    广播后再进行process处理

  6. KeyedBroadcastProcessFunction

  7. ProcessWindowFunction

    如 .aggregate(AggregateFunction<IN, ACC, OUT>aggFunction,ProcessWindowFunction<IN, OUT, KEY, W> windowFunction)

  8. ProcessAllWindowFunction

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值