【发布时间】:2018-05-01 09:47:03
【问题描述】:
我对 Flink 在事件时间加水印时如何处理延迟元素感到有些困惑。
我的理解是,当 Flink 读取一个数据流时,当看到任何事件时间大于当前 watermark 的数据时,watermark 时间就会增加。然后,任何覆盖时间严格小于水印的窗口都会被触发以进行驱逐(假设不是延迟允许。
但是,以这个最小的例子:
import org.apache.flink.api.scala._
import org.apache.flink.streaming.api.TimeCharacteristic
import org.apache.flink.streaming.api.scala.{StreamExecutionEnvironment}
import org.apache.flink.streaming.api.windowing.assigners.{TumblingEventTimeWindows}
import org.apache.flink.streaming.api.windowing.time.Time
import org.apache.flink.util.Collector
import org.apache.log4j.{Level, Logger}
object EventTimeExample {
Logger.getLogger("org").setLevel(Level.OFF)
Logger.getLogger("akka").setLevel(Level.OFF)
case class ExampleType(time: Long, value: Long)
def main(args: Array[String]) {
// Set up environment
val env = StreamExecutionEnvironment.createLocalEnvironment(1)
env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime)
// Example S3 path
val simple = env.fromCollection(Seq(
ExampleType(1525132800000L, 1),
ExampleType(1525132800000L, 2) ,
ExampleType(1525132920000L, 3),
ExampleType(1525132800000L, 4)
))
.assignAscendingTimestamps(_.time)
val windows = simple
.windowAll(TumblingEventTimeWindows.of(Time.seconds(60)))
.apply{
(window, iter, collector: Collector[(Long, Long, String)]) => {
collector.collect(window.getStart, window.getEnd, iter.map(_.value).toString())
}
}
windows.print
env.execute("TimeStampExample")
}
}
运行结果是:
(1525132800000,1525132860000,List(1, 2, 4))
(1525132920000,1525132980000,List(3))
但是,如果我的理解是正确的,4不应该包含在这里的第一个窗口中,因为当达到3记录的值时应该更新水印时间。
现在我认识到这是一个微不足道的例子,但不理解这一点会使理解更复杂的流程变得困难。
【问题讨论】:
标签: apache-flink watermark flink-streaming