Flink的ProcessFunction的测输出流

package flinkSourse

import org.apache.flink.streaming.api.functions.ProcessFunction
import org.apache.flink.streaming.api.scala._
import org.apache.flink.util.Collector

object FlinkProcessSideOutput {
  def main(args: Array[String]): Unit = {
    val executionEnvironment: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment
    executionEnvironment.setParallelism(1)
    //    executionEnvironment.setStreamTimeCharacteristic(TimeCharacteristic.EventTime) //watermark周期性生成,默认是200ms

    val stream2: DataStream[String] = executionEnvironment.socketTextStream("127.0.0.1", 1111)
    val transforStream: DataStream[SensorReading] = stream2.map(data => {
      val tmpList: Array[String] = data.split(",")
      SensorReading(tmpList(0), tmpList(1).toLong * 1000, tmpList(2).toDouble)
    })

    val highStream: DataStream[SensorReading] = transforStream.process(new SplitProcessFunction(30.0))
    highStream.print("high")
    highStream.getSideOutput(new OutputTag[(String, Long, Double)]("low")).print("low")

    executionEnvironment.execute("transform")

  }
}


class SplitProcessFunction(threshold: Double) extends ProcessFunction[SensorReading, SensorReading] {
  override def processElement(i: SensorReading, context: ProcessFunction[SensorReading, SensorReading]#Context, collector: Collector[SensorReading]): Unit = {
    if (i.temperature > threshold) {
      collector.collect(i)
    } else {
      context.output(new OutputTag[(String, Long, Double)]("low"), (i.id, i.timestamp, i.temperature))
    }

  }
}

你可能感兴趣的:(flink,flink,scala,大数据)