延迟程度
小延迟(乱序),用 watermark 容错(减慢时间的推进,让本已迟到的数据被认为没有迟到);
中延迟(乱序),用 allowLateness (允许一定限度内的迟到,并对迟到数据重新触发窗口计算);
大延迟(乱序),用 sideOutputLateData(将超出 allowLateness 的迟到数据输出到一个侧流中)。
Late Element种处理方案
1、丢弃
(默认)
2、allowedLateness
allowedLateness 指定允许数据延迟的时间。
在某些情况下,我们希望对迟到的数据再提供一个宽容的时间。
Flink 提供allowedLateness 方法可以实现对迟到数据设置一个延迟时间,在指定延迟时间内到达的数据还是可以触发 window 执行的。
调用 .allowedLateness(Time lateness)
3、侧输出流
sideOutputLateData 收集迟到的数据通过 sideOutputLateData 可以把迟到的数据统一收集,统一存储,方便后期排查问题。
该⽅法会将延迟的数据发送到给定 OutputTag 的 side output 中去,然后你可以通过 SingleOutputStreamOperator.getSideOutput(OutputTag) 来获取这些延迟的数据。
设置允许延迟的时间是通过 allowedLateness(lateness: Time) 设置;
保存延迟数据则是通过 sideOutputLateData(outputTag: OutputTag[T]) 保存;
获取延迟数据是通过 DataStream.getSideOutput(tag: OutputTag[X]) 获取;
1、设置空闲状态保留时间,Flink SQL 可以指定空闲状态(即未更新的状态)被保留的最小时间,当状态中某个 key对应的状态未更新的时间达到阈值时,该条状态被自动清理:
#参数指定
configuration.setString("table.exec.state.ttl", "1 h");
据我所知,FlinkSQL 不支持将迟到的数据输出到侧流中。 如果你下游使用的是 window 的话,可以通过设置 table.exec.emit.late-fire.enabled
和 table.exec.emit.late-fire.delay
来触发晚于 watermark 到达的数据。 其中允许等待晚与 watermark 的数据的时间由 table.exec.state.ttl
控制,等价于 Datastream 中的 allowedLateness, 故 window 的最大等待时间为 watermark 的 outOfOrder + allowedLateness。
table.exec.emit.early-fire.enabled
table.exec.emit.early-fire.delay
set 'table.exec.emit.early-fire.enabled' = 'true';
set 'table.exec.emit.early-fire.delay' = '5000';
set 'table.exec.window.allow-retract-input' = 'true';
SET 'table.exec.state.ttl' = '5000';
具体含义如下:
table.exec.emit.early-fire.enabled=true:这个参数用于启用或禁用 early firing。在处理时间窗口或者事件时间窗口时,Flink 默认只在窗口关闭时发出结果。如果你启用了 early firing(提前触发),Flink 会在窗口关闭之前定期发出中间结果。这对于需要实时反馈的场景非常有用。
table.exec.emit.early-fire.delay=5min:这个参数用于设置 early firing 的延迟时间。在上一个参数启用 early firing 的情况下,Flink 会每隔这个延迟时间发出一次中间结果。
ta