Flink学习笔记之WaterMark

 

Flink的时间概念

event time: 业务系统中事件发生的事件。通常因为各种原因会有部分延迟到达系统,所以需要进行乱序处理。

ingestion time:到达流处理系统的事件,因为是在入口的地方赋值,具有流中统一不变的特性。

processing time:流处理器的本地事件,因为flink是并发执行,各个处理器的本地时钟还有网络等因素导致差异性较大。

 

WaterMark是什么?

public final class Watermark extends StreamElement {

   /** The watermark that signifies end-of-event-time. */
   public static final Watermark MAX_WATERMARK = new Watermark(Long.MAX_VALUE);

   // ------------------------------------------------------------------------

   /** The timestamp of the watermark in milliseconds. */
   private final long timestamp;

   /**
    * Creates a new watermark with the given timestamp in milliseconds.
    */
   public Watermark(long timestamp) {
      this.timestamp = timestamp;
   }

   /**
    * Returns the timestamp associated with this {@link Watermark} in milliseconds.
    */
   public long getTimestamp() {
      return timestamp;
   }
}

通过Flink源代码可以看出,WaterMark的本质其实就是一个时间戳,用于和真正的event time进行比较的时间戳。用来决定乱序的时间是否已经全部到达。

 

WaterMark解决什么问题?

Flink基于一种假设:window触发计算时, eventTime <= waterMark 的事件都已经到达来解决乱序问题的。

若是有违反这种假设的event,通常配合sideOutput进行兜底操作。

 

Flink常用的WaterMark

 

AssignerWithPeriodicWatermarks

//指定为evenTime时间语义 env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime)

//生成watermark的周期 env.getConfig.setAutoWatermarkInterval(watermarkInterval)

通常是周期性的获取WaterMark,时间间隔由ExecutionConfig.setAutoWatermarkInterval(...)方式指定。

public interface AssignerWithPeriodicWatermarks<T> extends TimestampAssigner<T> {

   @Nullable
   Watermark getCurrentWatermark();
}

该接口仅定义来一个返回watermark的方法,Flink提供来一个延迟固定时间的watermark类 “BoundedOutOfOrdernessTimestampExtractor“可以直接使用。

 

AssignerWithPunctuatedWatermarks

public interface AssignerWithPunctuatedWatermarks<T> extends TimestampAssigner<T> {

   @Nullable
   Watermark checkAndGetNextWatermark(T lastElement, long extractedTimestamp);
}

该接口通常是有标志性事件到达触发watermark时使用。当一个新的事件到达时,检查一下是否生成新的watermark。

 

 

参考文献

1. https://www.cnblogs.com/rossiXYZ/p/12286407.html

2. https://www.jianshu.com/p/9db56f81fa2a?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

3. https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev/event_timestamps_watermarks.html

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值