标签:val lin 一个 socket scala parameter filter 并行 text
package com.sjw.flink
import org.apache.flink.api.java.utils.ParameterTool
import org.apache.flink.streaming.api.scala._
object StremingWordCount {
def main(args: Array[String]): Unit = {
//从外部传入参数
val params: ParameterTool = ParameterTool.fromArgs(args)
val host: String = params.get("host")
val port: Int = params.getInt("port")
//创建流式执行环境
val environment = StreamExecutionEnvironment.getExecutionEnvironment
//设置并行路
environment.setParallelism(2)
//获取数ju
val dataDS: DataStream[String] = environment.socketTextStream(host,port)
//val dataDS: DataStream[String] = environment.socketTextStream("sunjunwei1.com",7777)
//处理数据
val resultDS: DataStream[(String,Int)] = dataDS.flatMap(_.split(" "))
.filter(_.nonEmpty) //非空过滤
.map((_,1))
.keyBy(0)
.sum(1)
// 设置并行路 使用一个来执行
resultDS.print("streamWordCount").setParallelism(1)
//启动flink执行程序
environment.execute("stream job")
}
}
标签:val lin 一个 socket scala parameter filter 并行 text
原文地址:https://www.cnblogs.com/whyuan/p/13276847.html