請問我想把最后wordcounts里的內容按行打印出來要怎樣編寫代碼?,向下面這樣:means 1under 2this 3...Hadoop 流行的一個通用的數據流模式是 MapReduce。Spark 能很容易地實現 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這里,我們結合 flatMap, map 和 reduceByKey 來計算文件里每個單詞出現的數量,它的結果是包含一組(String, Int) 鍵值對的 RDD。我們可以使用 [collect] 操作在我們的 shell 中收集單詞的數量:scala> wordCounts.collect()
res6: Array[(String, Int)] = Array((means,1), (under,2), (this,3), (Because,1), (Python,2), (agree,1), (cluster.,1), ...)請問我想把最后wordcounts里的內容按行打印出來要怎樣編寫代碼?,向下面這樣:means 1under 2this 3...
1 回答

嚕嚕噠
TA貢獻1784條經驗 獲得超7個贊
可以寫個函數,我用python,你可以類比。
output = wordCounts.collect() def output(output): for key, value in output: print key, value
- 1 回答
- 0 關注
- 2961 瀏覽
添加回答
舉報
0/150
提交
取消