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) //主输出流 } } }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM