package Flink.HelloFlink
//导入隐式类型转换
import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.windowing.time.Time
object wordCount {
case class WordWithCount(word: String, count: Int)
def main(args: Array[String]): Unit = {
//获取运行环境
//
var env = StreamExecutionEnvironment.getExecutionEnvironment
//设置分区数量为1(并行度为1)
env.setParallelism(1)
//获取数据源
//
val stream = env.socketTextStream("hadoop102", 9999, '\n')
//写对流的转换处理逻辑
val transformed: DataStream[WordWithCount] = stream
//采用空格切分输入的字符串
.flatMap(line => line.split("\\s"))
//类似与mr中的map
.map(w => WordWithCount(w, 1))
//使用word字段进行分组,shuffle
.keyBy(0)
//开窗口为5秒钟滚动窗口
.timeWindow(Time.seconds(5))
//针对count字段进行累加操作,类似mr中的reduce
.sum(1)
//讲计算的结果输出到标准输出
transformed.print()
//执行计算逻辑
env.execute()
}
}
结果如下: