import scala.io.Source
object test {
def main(args: Array[String]): Unit = {
// Hello Scala
// Hello Spark
// Hello Hadoop
// TODO 1. 读取文件
val list: List[String] = Source.fromFile("input/word.txt").getLines().toList
// TODO 2. 扁平化
val wordList: List[String] = list.flatMap(_.split(" "))
// TODO 3. 转换结构
val word2OneList: List[(String, Int)] = wordList.map((_, 1))
// TODO 4. 分组
val word2ListMap: Map[String, List[(String, Int)]] = word2OneList.groupBy(_._1)
// TODO 4. 聚合
val word2CountMap: Map[String, Int] = word2ListMap.map(
kv => {
(kv._1, kv._2.size)
}
)
println(word2CountMap)
}
}
用Scala实现WordCount
最新推荐文章于 2022-05-16 10:38:27 发布