請(qǐng)問(wèn)我想把最后wordcounts里的內(nèi)容按行打印出來(lái)要怎樣編寫(xiě)代碼?,向下面這樣:means 1under 2this 3...Hadoop 流行的一個(gè)通用的數(shù)據(jù)流模式是 MapReduce。Spark 能很容易地實(shí)現(xiàn) MapReduce:scala> val wordCounts = textFile.flatMap(line => line.split(" ")).map(word => (word, 1)).reduceByKey((a, b) => a + b)
wordCounts: spark.RDD[(String, Int)] = spark.ShuffledAggregatedRDD@71f027b8這里,我們結(jié)合 flatMap, map 和 reduceByKey 來(lái)計(jì)算文件里每個(gè)單詞出現(xiàn)的數(shù)量,它的結(jié)果是包含一組(String, Int) 鍵值對(duì)的 RDD。我們可以使用 [collect] 操作在我們的 shell 中收集單詞的數(shù)量:scala> wordCounts.collect()
res6: Array[(String, Int)] = Array((means,1), (under,2), (this,3), (Because,1), (Python,2), (agree,1), (cluster.,1), ...)請(qǐng)問(wèn)我想把最后wordcounts里的內(nèi)容按行打印出來(lái)要怎樣編寫(xiě)代碼?,向下面這樣:means 1under 2this 3...
1 回答

嚕嚕噠
TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超7個(gè)贊
可以寫(xiě)個(gè)函數(shù),我用python,你可以類(lèi)比。
output = wordCounts.collect() def output(output): for key, value in output: print key, value
添加回答
舉報(bào)
0/150
提交
取消