flink KeyedProcessFunction 源码分析

这篇文章深入解析了Flink中的KeyedProcessFunction,讲解了如何处理输入元素、使用定时器以及Context和OnTimerContext的作用。理解这些概念对于编写高效流处理任务至关重要。
摘要由CSDN通过智能技术生成

在这里插入图片描述
KeyedProcessFunction 继承 AbstractRichFunction 函数
Function为基础函数接口,为纯接口里面没有方法
RichFunction 继承基础接口Function
里面封装了到了运行环境函数,open和close 方法
还有得到迭代运行时间内容

在这里插入图片描述

public abstract class KeyedProcessFunction<K, I, O> extends AbstractRichFunction {

   private static final long serialVersionUID = 1L;

   /**
    * Process one element from the input stream.
    *
    * <p>This function can output zero or more elements using the {@link Collector} parameter
    * and also update internal state or set timers using the {@link Context} parameter.
    *
    * @param value The input value.
    * @param ctx A {@link Context} that allows querying the timestamp of the element and getting
    *            a {@link TimerService} for registering timers and querying the time. The
    *            context is only valid during the invocation of this method, do not store it.
    * @param out The collector for returning result values.
    *
    * @throws Exception This method may throw exceptions. Throwing an exception will cause the operation
    *                   to fail and may trigger recovery.
    */
/*
Value 为输入的数据
Context  可以查询元素的时间戳,并获得一个定时器服务用来注册定时器和查询时间。
Collector 为收集输出数据
*/
   public abstract void processElement(I value, Context ctx, Collector<O> out) throws Exception;

   /**
    * Called when a timer set using {@link TimerService} fires.
    *
    * @param timestamp The timestamp of the firing timer.
    * @param ctx An {@link OnTimerContext} that allows querying the timestamp, the {@link TimeDomain}, and the key
    *            of the firing timer and getting a {@link TimerService} for registering timers and querying the time.
    *            The context is only valid during the invocation of this method, do not store it.
    * @param out The collector for returning result values.
    *
    * @throws Exception This method may throw exceptions. Throwing an exception will cause the operation
    *                   to fail and may trigger recovery.
    */
   /*
当一个定时器被设置开启时候调用
timestamp 开启的定时器的时间戳
ctx 这个对象允许查询时间戳 和得到一个定时器服务来注册timers 来查询时间 和上面的processElement 的ctx一样
out 输出结果
*/

public void onTimer(long timestamp, OnTimerContext ctx, Collector<O> out) throws Exception {}

   /**
    * Information available in an invocation of {@link #processElement(Object, Context, Collector)}
    * or {@link #onTimer(long, OnTimerContext, Collector)}.
    */

/**
    * 
在调用processElement和onTimer时会用到
*/

   public abstract class Context {

      /**
       * Timestamp of the element currently being processed or timestamp of a firing timer.
       *
       * <p>This might be {@code null}, for example if the time characteristic of your program
       * is set to {@link org.apache.flink.streaming.api.TimeCharacteristic#ProcessingTime}.
       */
/*
当前被处理的元素元素的时间戳或者一个开启timer的时间戳
例如你的时间特性可以设置为TimeCharacteristic.ProcessingTime
*/
      public abstract Long timestamp();

      /**
       * A {@link TimerService} for querying time and registering timers.
       */

/*定时服务器用来查询时间和注册timers
*/
      public abstract TimerService timerService();

 
/*定时服务器用来查询时间和注册timers
*/
      public abstract <X> void output(OutputTag<X> outputTag, X value);

      /**
       * Get key of the element being processed.
       */
/*得到元素正在被处理的key
*/
      public abstract K getCurrentKey();
   }

   /**
    * Information available in an invocation of {@link #onTimer(long, OnTimerContext, Collector)}.
    */

/*
调用onTimer时可以用
*/

   public abstract class OnTimerContext extends Context {
      /**
       * The {@link TimeDomain} of the firing timer.
       */
/*
触发计时器的TimeDomain*/
      public abstract TimeDomain timeDomain();

      /**
       * Get key of the firing timer.
       */
      @Override
      /*
得到触发计时器的key
*/
public abstract K getCurrentKey();
   }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值