import org.apache.flink.api.scala.{ExecutionEnvironment, _} /** * DATE:2022/10/1 18:54 * AUTHOR:GX */ object wordCount { def main(args: Array[String]): Unit = { // createExecutionEnvironment 创建批处理执行环境 val env: ExecutionEnvironment = ExecutionEnvironment.getExecutionEnvironment //从文件中读取数据 val inputPath: String = "H:\\javademo\\demo\\Flink\\src\\main\\resources\\hello.text" val inputDataSet:DataSet[String] = env.readTextFile(inputPath) //对数据进行转换处理统计,先分词,在按照word进行分组,最后进行聚合统计 val resultDataSet:DataSet[(String,Int)] = inputDataSet .flatMap(_.split(" ")) .map((_,1)) .groupBy(0) //一第一个元素作为key进行分组 .sum(1) //对当前所有数据的第二个元素求和 //打印输出 resultDataSet.print() } }