flink DataStream iterate迭代流scala代码示例

1.scala代码示例

import org.apache.flink.api.java.utils.ParameterTool
import org.apache.flink.streaming.api.scala.{DataStream, StreamExecutionEnvironment}
import org.apache.flink.api.scala._
import org.apache.flink.ml.math.DenseVector
/**
 * @Author: ch
 * @Date: 25/05/2020 2:55 PM
 * @Version 1.0
 * @Describe:
 */
object DataStreamIterateTest {
  /**
   * 测试迭代流
   * @param args
   */
  def main(args: Array[String]): Unit = {
    // the port to connect to
    var port = 0
    try {
      ParameterTool.fromArgs(args).getInt("port")
    } catch {
      case e: Exception => {
        port  = 9000
      }
    }
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment
    val socketText: DataStream[String] = env.socketTextStream("127.0.0.1", port, '\n')
    val iterate = socketText
      .iterate( //stepfunction: initialStream => (feedback, output)
        stepFunction =>{
          val feedback: DataStream[String] = stepFunction.filter(s=>s=="haha").setParallelism(1)
          feedback.print()
          val output: DataStream[String] = stepFunction.filter(s=>s!="haha")
          (feedback,output)
        }
      )
      .print()
    env.execute()
  }
}

2.运行socket进行测试,输入"hehe" "haha",其中"hehe"输出到output流中,“haha”输出到feedback流中并不断迭代。
flink DataStream iterate迭代流scala代码示例_第1张图片

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