背景
使用Event time时间模型时,由于网络或传输等原因,事件被Flink处理的顺序不一定是事件产生的顺序(乱序),可能会存在两方面影响:
- 当前窗口不知道何时停止,开始计算结果;
- 影响窗口计算结果的准确性,见示例;
WaterMark机制
WaterMark本质上是一个带有时间戳的特殊event,当Flink中的运算符接收到水印时,它明白(假设)它不会看到比该时间戳更早的消息。
A Watermark(t) declares that event time has reached time t in that stream, meaning that there should be no more elements from the stream with a timestamp t’ <= t (i.e. events with timestamps older or equal to the watermark).It can then safely compute and emit the result of the window
生成WaterMark
WaterMark需要开发人员根据具体的场景采取合适的策略生成;
生成方式:
- 数据源中产生;
- 在Flink入口处生成( Watermark Generators);
参考:
- 云邪博客:http://wuchong.me/blog/2018/11/18/flink-tips-watermarks-in-apache-flink-made-easy/
- 官网:https://ci.apache.org/projects/flink/flink-docs-release-1.7/dev/event_time.html