zoukankan      html  css  js  c++  java
  • Flink之ProcessFunction侧输出流

    1、代码案例

    package processFunction
    
    import com.yangwj.api.SensorReading
    import org.apache.flink.streaming.api.functions.{ProcessFunction}
    import org.apache.flink.streaming.api.scala.{DataStream, StreamExecutionEnvironment}
    import org.apache.flink.streaming.api.scala._
    import org.apache.flink.util.Collector
    /**
     * @author yangwj
     * @date 2021/1/10 21:25
     * @version 1.0
     */
    object SideOutPutTest {
      def main(args: Array[String]): Unit = {
        val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment
        
        val inputFile:String = "G:\Java\Flink\guigu\flink\src\main\resources\sensor.txt"
        val input: DataStream[String] = env.readTextFile(inputFile)
    
        val dataStream = input.map(data => {
          val arr: Array[String] = data.split(",")
          SensorReading(arr(0), arr(1).toLong, arr(2).toDouble)
        })
    
        val hightStream= dataStream.process(new SplitTemp(35.5))
    
        hightStream.print("high")
        hightStream.getSideOutput(new OutputTag[(String,Long,Double)]("low")).print("low")
        env.execute("SideOutPutTest Test")
    
      }
    }
    
    //分流测试
    class SplitTemp(threshold:Double) extends ProcessFunction[SensorReading,SensorReading]{
      override def processElement(i: SensorReading, context: ProcessFunction[SensorReading, SensorReading]#Context, collector: Collector[SensorReading]): Unit = {
    
        if(i.temperature > threshold){
          //如果温度大于35.5,那么输出到主流
          collector.collect(i)
        }else {
          //如果温度小于35.5,那么输出到测流
          context.output(new OutputTag[(String, Long, Double)]("low"), (i.id, i.timestamp, i.temperature))
        }
      }
    }
  • 相关阅读:
    第一次被队友说给力
    An Unfair Game-[ACdream1035]
    男人八题解题报告
    it's hard to say
    剪花布条[HDU2087]
    1117: 无序字母对 character
    20181101
    20181104
    20181102
    20181031
  • 原文地址:https://www.cnblogs.com/ywjfx/p/14264932.html
Copyright © 2011-2022 走看看