Flink中开窗的延迟触发、侧输出流的应用
注意如何处理迟到数据和侧数据流?: flink 解决乱序事件: 1.设定eventTime 和 waterMark 通过延时时长,使窗口等待一定时间以后再触发 2.如果窗口已经触发了,还有数据没有来,那就使用允许迟到时间 (allowedLateness)此时窗口虽然被触发了但是没有关闭,等数据来了再次触发 3.若允许迟到时间过了,还有数据没有来,那就使用将数据放入侧输出流 流程图示意如下:
import java.time.Duration
import org.apache.flink.api.common.eventtime.{SerializableTimestampAssigner, WatermarkStrategy}
import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows
import org.apache.flink.streaming.api.windowing.time.Time
import org.example.bean.TrainAlarm
object AssignEventTimeAndWm3 {
def main(args: Array[String]): Unit = {
val env = StreamExecutionEnvironment.getExecutionEnvironment
env.setParallelism(1)
val inputStream= env.socketTextStream("master", 666)
.map(line => {
val ps = line.split(",")
TrainAlarm(ps(0), ps(1).toLong, ps(2).toDouble)
})
.assignTimestampsAndWatermarks(
WatermarkStrategy.forBoundedOutOfOrderness(Duration.ofSeconds(5))
.withTimestampAssigner(new SerializableTimestampAssigner[TrainAlarm] {
override def extractTimestamp(element: TrainAlarm, l: Long): Long = {
element.ts*1000L
}
})
)
val lateTag = new OutputTag[TrainAlarm]("late")
val rst = inputStream.keyBy(_.id)
.window(TumblingEventTimeWindows.of(Time.seconds(5)))
.allowedLateness(Time.minutes(1))
.sideOutputLateData(lateTag)
.max("temp")
rst.print()
print("late===>")
rst.getSideOutput(lateTag).print()
env.execute()
}
}
|