美文网首页程序员flink
Flink详解系列之四--时间

Flink详解系列之四--时间

作者: 王吉吉real | 来源:发表于2021-01-01 23:58 被阅读0次

在流处理场景中,处理的是持续到达且可能是无穷的事件流,时间在这样的应用中是最核心的要素。
Flink定义了三种时间类型:

  • 事件时间(Event Time) 事件时间是数据流中事件实际发生的真实时间,通常用时间戳来描述。它反映的是事件本身发生的时间,具有确定性,不依赖系统的时间,能还原事件之间发生的实际的顺序。
  • 处理时间(Processing Time) 处理时间是指定事件被算子处理的时间,是系统时间。
  • 摄取时间(Ingestion Time) 摄取时间是事件进入到Flink系统的时间,即该事件在数据源中被读取的时间。使用场景较少,与事件时间相比,它的价值不大,因为它无法提供确定的结果。

事件时间与处理时间是最常用的两个时间概念。事件时间能将处理速度与结果解耦,使结果不受实时数据乱序的影响。处理时间则主要用于更重视速度而非准确性的场景以及周期性的实时监控场景。

代码层面, Flink的时间是一个枚举类:

@PublicEvolving
public enum TimeCharacteristic {
    ProcessingTime,
    IngestionTime,
    EventTime
}

在实时处理应用创建会用到的执行环境类StreamExecutionEnvironment中,默认使用的是处理时间。

    /** The time characteristic that is used if none other is set. */
    private static final TimeCharacteristic DEFAULT_TIME_CHARACTERISTIC = TimeCharacteristic.ProcessingTime;
    /** The time characteristic used by the data streams. */
    private TimeCharacteristic timeCharacteristic = DEFAULT_TIME_CHARACTERISTIC;

如果不希望使用处理时间,而使用其他时间的话,需要在StreamExecutionEnvironment中手动设置:

@PublicEvolving
    public void setStreamTimeCharacteristic(TimeCharacteristic characteristic) {
        this.timeCharacteristic = Preconditions.checkNotNull(characteristic);
        if (characteristic == TimeCharacteristic.ProcessingTime) {
            getConfig().setAutoWatermarkInterval(0);
        } else {
            getConfig().setAutoWatermarkInterval(200);
        }
    }

相关文章

网友评论

    本文标题:Flink详解系列之四--时间

    本文链接:https://www.haomeiwen.com/subject/ogdsoktx.html