flink的8个底层ProcessFunction,带示例
8个底层ProcessFunction
- ProcessFunction
- KeyedProcessFunction
- CoProcessFunction
- ProcessJoinFunction
- BroadcastProcessFunction
- KeyedBroadcastProcessFunction
- ProcessWindowFunction
- ProcessAllWindowFunction
KeyedProcessFunction:10秒内温度连续上升报警
package FlinkProject.runMain
import FlinkProject.utils.sensor
import org.apache.flink.api.common.serialization.SimpleStringSchema
import org.apache.flink.api.common.state.{ValueState, ValueStateDescriptor}
import org.apache.flink.streaming.api.functions.KeyedProcessFunction
import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer
import org.apache.flink.util.Collector
import java.util.Properties
object ProcessFunctionTest {
def main(args: Array[String]): Unit = {
val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment
val prop = new Properties()
prop.setProperty("bootstrap.servers", "localhost:9092")
prop.setProperty("group.id", "group_id")
val stream: DataStream[String] = env.addSource(new FlinkKafkaConsumer[String]("flink", new SimpleStringSchema(), prop))
val result: DataStream[String] = stream.filter((_: String).nonEmpty).map((x: String) => {
val data: Array[String] = x.split(",")
sensor(data(0), data(1).toLong, data(2).toDouble)
}).keyBy((_: sensor).id)
.process(new myKeyedProcessFunction(10000L))
result.print()
env.execute()
}
}
class myKeyedProcessFunction(interval: Long) extends KeyedProcessFunction[String, sensor, String] {
lazy val timer: ValueState[Long] = getRuntimeContext.getState(new ValueStateDescriptor[Long]("timer", classOf[Long], 0))
lazy val lastTempState: ValueState[Double] = getRuntimeContext.getState(new ValueStateDescriptor[Double]("temp", classOf[Double], -1234567876))
override def processElement(value: sensor, ctx: KeyedProcessFunction[String, sensor, String]#Context, out: Collector[String]): Unit = {
if (value.temperature > lastTempState.value() && timer.value() == 0) {
val ts: Long = ctx.timerService().currentProcessingTime() + interval
ctx.timerService().registerProcessingTimeTimer(ts)
timer.update(ts)
} else if (value.temperature < lastTempState.value()) {
ctx.timerService().deleteEventTimeTimer(timer.value())
timer.update(ctx.timerService().currentProcessingTime() + interval)
}
lastTempState.update(value.temperature)
}
override def onTimer(timestamp: Long, ctx: KeyedProcessFunction[String, sensor, String]#OnTimerContext, out: Collector[String]): Unit = {
out.collect(timer.value() / 1000 + "> sensor:" + ctx.getCurrentKey + "连续" + interval / 1000 + "秒连续上升")
timer.clear()
}
}