object WordCount {
def main(args: Array[String]): Unit = {
val lines = Array("hello tom hello jerry", "hello kitty hello tom", "hello tom tom")
//按空格切割行成詞,形成RDD(Array(),Array(),...),在flatmap壓平成RDD(...)
val words: Array[String] = lines.flatMap(l => l.split(" "))
//對每個單詞映射成元組,value爲1
val wordAndOne: Array[(String, Int)] = words.map(w => (w, 1))
//按照單詞分組,成RDD((word,Array(...)),...)
val grouped: Map[String, Array[(String, Int)]] = wordAndOne.groupBy(t => t._1)
//對每個單詞計算value
val wordAndCounts: Map[String, Int] = grouped.map(t => (t._1, t._2.length))
//按照value排序
val result: List[(String, Int)] = wordAndCounts.toList.sortBy(t => -t._2)
//打印結果
print(result.toBuffer)
}
}
spark編寫wordcount程序
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.