课程大纲 |
课程内容 |
学习效果 |
掌握目标 |
ProcessFunction |
ProcessFunction |
掌握 |
|
状态编程 |
状态编程 |
掌握 |
|
容错机制 |
容错机制 |
掌握 |
我们之前学习的转换算子是无法访问事件的时间戳信息和水位线信息的。而这在一些应用场景下,极为重要。例如 MapFunction 这样的 map 转换算子就无法访问时间戳或者当前事件的事件时间。
基于此,DataStream API 提供了一系列的 Low-Level 转换算子。可以访问时间戳、watermark 以及注册定时事件。还可以输出特定的一些事件,例如超时事件等。Process Function 用来构建事件驱动的应用以及实现自定义的业务逻辑(使用之前的window 函数和转换算子无法实现)。例如,Flink SQL 就是使用 Process Function 实现的。
Flink 提供了 8 个 Process Function:
这里我们重点介绍 KeyedProcessFunction。
KeyedProcessFunction 用来操作 KeyedStream。KeyedProcessFunction 会处理流的每一个元素,输出为 0 个、1 个或者多个元素。所有的 Process Function 都继承自RichFunction 接口,所以都有 open()、close()和 getRuntimeContext()等方法。而KeyedProcessFunction[KEY, IN, OUT]还额外提供了两个方法: ·
processElement(v: IN, ctx: Context, out: Collector[OUT]), 流中的每一个元素都会调用这个方法,调用结果将会放在 Collector 数据类型中输出。Context可以访问元素的时间戳,元素的 key,以及 TimerService 时间服务。Context还可以将结果输出到别的流(side outputs)。
onTimer(timestamp: Long, ctx: OnTimerContext, out: Collector[OUT])是一个回调函数。当之前注册的定时器触发时调用。参数 timestamp 为定时器所设定的触发的时间戳。Collector 为输出结果的集合。OnTimerContext 和processElement 的 Context 参数一样,提供了上下文的一些信息,例如定时器触发的时间信息(事件时间或者处理时间)。
Context 和 OnTimerContext 所持有的 TimerService 对象拥有以下方法: ·
当定时器 timer 触发时,会执行回调函数 onTimer()。注意定时器 timer 只能在keyed streams 上面使用。
下面举个例子说明 KeyedProcessFunction 如何操作 KeyedStream。
需求:监控温度传感器的温度值,如果温度值在一秒钟之内(processing time)连续上升,则报警。
/*
定时服务说明
监控温度传感器的温度值,如果温度值在一秒钟之内(processing time)连续上升,则报警。
*/
object TimeServiceOps {
def main(args: Array[String]): Unit = {
val env = StreamExecutionEnvironment.getExecutionEnvironment
env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime)
env.setParallelism(1)
val dataStream = env.socketTextStream("node01",9999)
val textKeyStream = dataStream
.map{
line => val words = line.split("\t")
(words(0).trim, words(1).trim.toLong, words(2).trim.toDouble)
}
.assignTimestampsAndWatermarks(
new BoundedOutOfOrdernessTimestampExtractor[(String, Long, Double)](Time.seconds(2)) {
override def extractTimestamp(t: (String, Long, Double)): Long = {
t._2 * 1000
}
}
)
.keyBy(_._1)
val result = textKeyStream.process(new PorceTempFunc)
textKeyStream.print("textKeyStream ::: ").setParallelism(1)
result.print("result:::").setParallelism(1)
env.execute("TimeServiceOps")
}
}
class PorceTempFunc extends KeyedProcessFunction[String,(String, Long, Double),String]{
//上一次的温度放到状态中
lazy val lastTemp:ValueState[Double] = getRuntimeContext.getState(
new ValueStateDescriptor[Double]("lastTem",Types.of[Double])
)
// 保存注册的定时器的时间戳
lazy val currentTimer:ValueState[Long] = getRuntimeContext.getState(
new ValueStateDescriptor[Long]("currentTimer", Types.of[Long])
)
//lastTem currentTimer如果没有初始化,里面的值是0
//processElement 每一个元素都会调用这个方法,每调用一次都要更新一次lastTem
override def processElement(current: (String, Long, Double),
context: KeyedProcessFunction[String, (String, Long, Double), String]#Context,
collector: Collector[String]): Unit = {
//每调用一次processElement方法都要更新一次lastTem
val perTemp = lastTemp.value()
lastTemp.update(current._3)
//获取定时器的时间戳
val curTimerTimestamp = currentTimer.value()
/*
第一次采集数据:perTemp == 0上一次的温度是0,取消定时器
温度上升: 第二次采集的温度比第一次采集的温度高,注册定时器
这个时间范围内,第三次以及后面采集的温度比上次的温度高,这个时间范围值内以及注册了定时,则不再注册。可用通过定时器的时间戳是否