Flink 侧输出流 DEMO
作者:互联网
代码:
package com.kpwong.sideoutput import com.atguigu.window.{SensorReading} import org.apache.flink.streaming.api.functions.ProcessFunction //import org.apache.flink.streaming.api.functions.timestamps.BoundedOutOfOrdernessTimestampExtractor import org.apache.flink.streaming.api.scala.{DataStream, StreamExecutionEnvironment} import org.apache.flink.streaming.api.scala._ //import org.apache.flink.streaming.api.windowing.time.Time import org.apache.flink.util.Collector object SideOutPutTest { def main(args: Array[String]): Unit = { val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment val SocketDS: DataStream[String] = env.socketTextStream("hadoop202",9999) val sensorStream: DataStream[SensorReading] = SocketDS.map(data => { val dataArray: Array[String] = data.split(",") SensorReading(dataArray(0).trim, dataArray(1).trim.toLong, dataArray(2).trim.toDouble) }).process(new FreeAlert()) // val processDS: DataStream[SensorReading] = sensorStream.assignTimestampsAndWatermarks(new BoundedOutOfOrdernessTimestampExtractor[SensorReading](Time.seconds(1)) { // override def extractTimestamp(element: SensorReading) = { // element.timestamp * 1000 // } // }).process(new FreeAlert()) sensorStream.print("main process flow") sensorStream.getSideOutput(new OutputTag[SensorReading]("zeroAlert")).print("Side out put flow") env.execute() } } //冰点报警,如果小于0度,就输出到侧数据流 class FreeAlert extends ProcessFunction[SensorReading,SensorReading]{ lazy val alertOutPut:OutputTag[SensorReading] = new OutputTag[SensorReading]("zeroAlert") override def processElement(value: SensorReading, ctx: ProcessFunction[SensorReading, SensorReading]#Context, out: Collector[SensorReading]): Unit = { if(value.temperature < 0.0) { //小于零度,到侧输出流 ctx.output(alertOutPut,value) } else { out.collect(value) //主输出流 } } }
标签:输出,flink,val,DEMO,Flink,SensorReading,org,apache,import 来源: https://www.cnblogs.com/kpwong/p/14101497.html