flink DataStream wc

 1 import org.apache.flink.streaming.api.scala.{DataStream, StreamExecutionEnvironment}
 2 
 3 object FlinkDemo02_Stream_wc {
 4     def main(args: Array[String]): Unit = {
 5         //1 获取执行环境
 6         val env = StreamExecutionEnvironment.getExecutionEnvironment
 7         //2 获取数据集
 8         val ds: DataStream[String] = env.socketTextStream("linux01", 9999)
 9         //3 执行
10         import org.apache.flink.api.scala._
11         val result: DataStream[(String, Int)] = ds.flatMap(_.split("\\s"))
12           .filter(_.nonEmpty)
13               .map((_, 1))
14                   .keyBy(0)
15                       .sum(1)
16         result.print().setParallelism(1)
17         
18         //4 启动executor, 执行任务
19         env.execute("socket_stream_wc")
20     }
21 }
View Code

 

posted on 2020-06-22 01:13  天天背单词  阅读(137)  评论(0编辑  收藏  举报

导航