準(zhǔn)備一份數(shù)據(jù)/home/admin/data/helloworld.txt
hello world hello
hello world welcome
scala> val wc = sc.textFile("file:///home/admin/data/helloworld.txt")
scala> wc.flatMap(x=>x.split("\t")).map(x=>(x,1)).reduceByKey(_+_).collect()
res19: Array[(String, Int)] = Array((hello,3), (welcome,1), (world,2))
按照詞頻排序
scala> wc.flatMap(x=>x.split("\t")).map(x=>(x,1)).reduceByKey(_+_).sortBy(_._2,true).collect().foreach(println(_))
(welcome,1)
(world,2)
(hello,3)