我们之前学习的转换算子是无法访问事件的时间戳信息和水位线信息的。而这在一些应用场景下,极为重要。例如 MapFunction 这样的 map 转换算子就无法访问时间戳或者当前事件的事件时间。
基于此,DataStream API 提供了一系列的 Low-Level 转换算子。可以访问时间戳、watermark 以及注册定时事件。还可以输出特定的一些事件,例如超时事件等。
Process Function 用来构建事件驱动的应用以及实现自定义的业务逻辑(使用之前的window 函数和转换算子无法实现)。例如,Flink SQL 就是使用 Process Function 实现的。
Flink 提供了 8 个 Process Function:
- ProcessFunction
- KeyedProcessFunction
- CoProcessFunction
- ProcessJoinFunction
- BroadcastProcessFunction
- KeyedBroadcastProcessFunction
- ProcessWindowFunction
- ProcessAllWindowFunction
KeyedProcessFunction
这里我们重点介绍 KeyedProcessFunction。
KeyedProcessFunction 用来操作 KeyedStream。KeyedProcessFunction 会处理流的每一个元素,输出为 0 个、1 个或者多个元素。所有的 Process Function 都继承自RichFunction 接口,所以都有 open()、close()和 getRuntimeContext()等方法。而KeyedProcessFunction<K, I, O>还额外提供了两个方法:
- processElement(I value, Context ctx, Collector out), 流中的每一个元素都会调用这个方法,调用结果将会放在 Collector 数据类型中输出。Context 可以访问元素的时间戳,元素的 key,以及 TimerService 时间服务。Context 还可以将结果输出到别的流(side outputs)。
- onTimer(long timestamp, OnTimerContext ctx, Collector out) 是一个回调
函数。当之前注册的定时器触发时调用。参数 timestamp 为定时器所设定的触发的时间戳。Collector 为输出结果的集合。OnTimerContext 和processElement 的 Context 参数一样,提供了上下文的一些信息,例如定时器触发的时间信息(事件时间或者处理时间)。
TimerService 和 定时器(Timers)
Context 和 OnTimerContext 所持有的 TimerService 对象拥有以下方法:
- long currentProcessingTime() 返回当前处理时间
- long currentWatermark() 返回当前 watermark 的时间戳
- void registerProcessingTimeTimer(long timestamp) 会注册当前 key 的
processing time 的定时器。当 processing time 到达定时时间时,触发 timer。 - void registerEventTimeTimer(long timestamp) 会注册当前 key 的 event time 定时器。当水位线大于等于定时器注册的时间时,触发定时器执行回调函数。
- void deleteProcessingTimeTimer(long timestamp) 删除之前注册处理时间定时器。如果没有这个时间戳的定时器,则不执行。
- void deleteEventTimeTimer(long timestamp) 删除之前注册的事件时间定时器,如果没有此时间戳的定时器,则不执行。
- 当定时器 timer 触发时,会执行回调函数 onTimer()。注意定时器 timer 只能在keyed streams 上面使用。
代码案例:
package com.dahuan.processfunction;
import com.dahuan.bean.SensorReading;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;
public class Process_KeyedProcessFunction {
public static void main(String[] args) throws Exception {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.setParallelism( 1 );
final String host = "localhost";
final int port = 8888;
DataStream<String> inputStream = env.socketTextStream( host, port );
DataStream<SensorReading> dataStream = inputStream.map( data -> {
String[] split = data.split( "," );
return new SensorReading( split[0], new Long( split[1] ), new Double( split[2] ) );
} );
//测试KeyedProcessFunction 先分组然后自定义处理
dataStream.keyBy( "id" )
.process( new MyProcess() )
.print();
env.execute("Process_KeyedProcessFunction");
}
//实现自定义处理函数
public static class MyProcess extends KeyedProcessFunction<Tuple,SensorReading,Integer>{
ValueState<Long> tsTimerState;
@Override
public void open(Configuration parameters) throws Exception {
tsTimerState = getRuntimeContext().getState( new ValueStateDescriptor<Long>("ts-time",Long.class) );
}
@Override
public void processElement(SensorReading value, Context ctx, Collector<Integer> out) throws Exception {
out.collect( value.getId().length() );
//TODO Context
ctx.timestamp(); //获取当前时间戳
ctx.getCurrentKey(); //获取正在处理的元素的键
ctx.timerService().currentProcessingTime(); //返回当前处理时间
ctx.timerService().currentWatermark(); //返回当前事件时间水印
ctx.timerService().registerProcessingTimeTimer( 10000L ); //注册要在处理时间超过给定时间时触发的计N时器。
tsTimerState.update( ctx.timerService().currentProcessingTime() + 1000L );
//ctx.timerService().deleteProcessingTimeTimer(tsTimerState.value() );
ctx.timerService().registerEventTimeTimer((value.getTimestamp() + 10) * 1000L); //注册一个在事件时间水印超过给定时间时将要触发的计时器。
ctx.timerService().deleteProcessingTimeTimer( 10000L ); //删除具有给定触发时间的处理时间计时器。仅当预先注册了此类计时器并且尚未到期时,此方法才有效。
ctx.timerService().deleteEventTimeTimer( 10000L );//删除具有给定触发时间的事件时间计时器。仅当预先注册了此类计时器并且尚未到期时,此方法才有效
}
@Override
public void onTimer(long timestamp, OnTimerContext ctx, Collector<Integer> out) throws Exception {
System.out.println(timestamp + "定时器触发" );
ctx.getCurrentKey(); //获取触发计时器的密钥。
ctx.timeDomain(); //触发计时器的时间
}
@Override
public void close() throws Exception {
tsTimerState.clear();
}
}
}
下面举个例子说明 KeyedProcessFunction 如何操作 KeyedStream。
需求:监控温度传感器的温度值,如果温度值在 10 秒钟之内(processing time)
连续上升,则报警。
package com.dahuan.processfunction;
import com.dahuan.bean.SensorReading;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;
public class Process_ApplicationCase {
public static void main(String[] args) throws Exception {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.setParallelism( 1 );
final String host = "localhost";
final int port = 8888;
DataStream<String> inputStream = env.socketTextStream( host, port );
DataStream<SensorReading> dataStream = inputStream.map( data -> {
String[] split = data.split( "," );
return new SensorReading( split[0], new Long( split[1] ), new Double( split[2] ) );
} );
dataStream.keyBy( "id" )
.process( new TempConsIncreWarning( 10 ) )
.print();
env.execute( "Process_ApplicationCase" );
}
//TODO 自定义处理函数,检测一段时间内的温度 连续上升,输出报警
public static class TempConsIncreWarning extends KeyedProcessFunction<Tuple, SensorReading, String> {
//定义私有属性,统计当前时间间隔
private Integer interval;
public TempConsIncreWarning(Integer interval) {
this.interval = interval;
}
//定义状态,保存上一次的温度值
private ValueState<Double> lastTempState;
private ValueState<Long> timerTsState;
@Override
public void open(Configuration parameters) throws Exception {
lastTempState = getRuntimeContext().getState( new ValueStateDescriptor<Double>( "last-temp", Double.class, Double.MAX_VALUE ) );
timerTsState = getRuntimeContext().getState( new ValueStateDescriptor<Long>( "timer-ts", Long.class ) );
}
@Override
public void processElement(SensorReading value, Context ctx, Collector<String> out) throws Exception {
//取出状态
Double lastTemp = lastTempState.value();
Long timerTs = timerTsState.value();
//TODO 如果温度上升并且没有定时器,注册10秒后的定时器,开始等待
if (value.getTemperature() > lastTemp && timerTs == null) {
//TODO 计算出定时器的时间戳 返回当前处理时间
Long ts = ctx.timerService().currentProcessingTime() + interval * 1000L;
//TODO 注册
ctx.timerService().registerProcessingTimeTimer( ts );
timerTsState.update( ts );
} else if (value.getTemperature() < lastTemp && timerTs != null) {
ctx.timerService().deleteProcessingTimeTimer( timerTs );
timerTsState.clear();
}
//更新状态温度
lastTempState.update( value.getTemperature() );
}
@Override
public void onTimer(long timestamp, OnTimerContext ctx, Collector<String> out) throws Exception {
//TODO 定时器触发,输出报警信息
out.collect( "传感器" + ctx.getCurrentKey().getField( 0 ) + "温度值连续" + interval + "s上升" );
timerTsState.clear();
}
//清楚状态
@Override
public void close() throws Exception {
lastTempState.clear();
}
}
}
侧输出流(SideOutput)
大部分的 DataStream API 的算子的输出是单一输出,也就是某种数据类型的流。除了 split 算子,可以将一条流分成多条流,这些流的数据类型也都相同。process function 的 side outputs 功能可以产生多条流,并且这些流的数据类型可以不一样。
一个 side output 可以定义为 OutputTag[X]对象,X 是输出流的数据类型。process function 可以通过 Context 对象发射一个事件到一个或者多个 side outputs。
下面是一个示例程序,用来监控传感器温度值,将温度值低于 30 度的数据输出
到 side output。
package com.dahuan.processfunction;
import com.dahuan.bean.SensorReading;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;
public class Process_SideOutCase {
public static void main(String[] args) throws Exception {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.setParallelism( 1 );
final String host = "localhost";
final int port = 8888;
DataStream<String> inputStream = env.socketTextStream( host, port );
DataStream<SensorReading> dataStream = inputStream.map( data -> {
String[] split = data.split( "," );
return new SensorReading( split[0], new Long( split[1] ), new Double( split[2] ) );
} );
//TODO 定义一个OutPutTag,用来表示测输出流低温流
OutputTag<SensorReading> lowTempTag = new OutputTag<SensorReading>( "low-temp" ){};
//TODO 自定义侧输出流实现分流操作
SingleOutputStreamOperator<SensorReading> highTempStream = dataStream.process( new ProcessFunction<SensorReading, SensorReading>() {
@Override
public void processElement(SensorReading value, Context ctx, Collector<SensorReading> out) throws Exception {
//TODO 判断温度,大于30度,高温流输出到主流,小于低温输出到侧输出流
if(value.getTemperature() > 30){
out.collect( value );
}else{
ctx.output(lowTempTag,value);
}
}
} );
highTempStream.print( "high-temp" );
highTempStream.getSideOutput( lowTempTag ).print("low-temp");
env.execute( "Process_ApplicationCase" );
}
}
CoProcessFunction
对于两条输入流,DataStream API 提供了 CoProcessFunction 这样的 low-level
操作。CoProcessFunction 提供了操作每一个输入流的方法: processElement1()和processElement2()。
类似于 ProcessFunction,这两种方法都通过 Context 对象来调用。这个 Context对象可以访问事件数据,定时器时间戳,TimerService,以及 side outputs。
CoProcessFunction 也提供了 onTimer()回调函数。