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
3. https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev/event_timestamps_watermarks.html
本文详细介绍了Flink中的时间概念,包括eventtime、ingestiontime和processingtime,重点讲解了WaterMark的概念和作用。WaterMark是一个时间戳,用于处理乱序事件,确保window计算时已收到所有eventTime小于等于waterMark的事件。Flink提供了AssignerWithPeriodicWatermarks和AssignerWithPunctuatedWatermarks两种WaterMark策略,分别适用于周期性和标志性事件触发。违反假设的event通常通过sideOutput进行处理。
932

被折叠的 条评论
为什么被折叠?



