object WordCount {
def main(args: Array[String]): Unit = {
val lineList = List(
"hadoop hive spark hadoop java",
"spark java sqool hive",
"hadoop hive spark java"
)
//压平函数
var faltSplit: List[String] = lineList.flatMap(item=>item.split(" "))
//把每个单词生成一个一个的tuple
var first= faltSplit.map(item=>(item,1))
//一key进行分组
var second = first.groupBy(item=>item._1)
//统计value的长度
var third = second.mapValues(_.size)
//排序
val fifth = third.toList.sortBy(_._1)
println(first)
println(second)
println(third)
println(fifth)
}
}
Scala实现wordcount程序
最新推荐文章于 2020-11-30 10:40:53 发布