代码:
import org.apache.flink.api.scala._
object WordCount1 {
def main(args: Array[String]): Unit ={
//创建执行环境
val env = ExecutionEnvironment.getExecutionEnvironment
//从文件中读取数据
val inputPath = "hello.txt"
val inputDS: DataSet[String] = env.readTextFile(inputPath)
val wordCountDS: AggregateDataSet[(String, Int)] = inputDS.flatMap(_.split(" "))
.map((_, 1))
.groupBy(0) //按第一个字段分组
.sum(1) //对第二个字段求和
wordCountDS.print()
}
}
结果:
有帮助的欢迎评论打赏哈,谢谢!