使用Flink Watermark sideOutputLateData的坑

Flink Watermark是用于处理数据乱序问题,网上已经有很多优秀的文章介绍,这里就不重复了。参考:

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

今天要说的使用Watermark过程中自己挖的坑,使用sideOutputLateData()过程中没有正常输出的问题,在此记录一下:

先来看一下源码解析:

/**
	 * Send late arriving data to the side output identified by the given {@link OutputTag}. Data
	 * is considered late after the watermark has passed the end of the window plus the allowed
	 * lateness set using {@link #allowedLateness(Time)}.
	 *
	 * <p>You can get the stream of late data using
	 * {@link SingleOutputStreamOperator#getSideOutput(OutputTag)} on the
	 * {@link SingleOutputStreamOperator} resulting from the windowed operation
	 * with the same {@link OutputTag}.
	 */
	@PublicEvolving
	public WindowedStream<T, K, W> sideOutputLateData(OutputTag<T> outputTag) {
		Preconditions.checkNotNull(outputTag, "Side output tag must not be null.");
		this.lateDataOutputTag = input.getExecutionEnvironment().clean(outputTag);
		return this;
	}

首先,延迟的数据通过outputTag输出,必须要事件时间大于watermark + allowed lateness,数据才会存储在outputTag中。

然后,注意,坑位来了,调用getSideOutput()方法获取DataStream时,必须调用windowed operation返回的SingleOutputStreamOperator对象才能获取到期望的延迟数据。

总结:

sideOutputLateData() 是一个兜底方案,数据延迟严重,可以保证数据不丢失。

使用第三方类库前还是要先阅读源码,不要凭直觉。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章