1. 准备文件
我们之前在hadoop运行wordcount例子一文中使用MR来计算wordcount。这里我们分析wordcount采用同一个文件来计算。
2. spark shell中分析
进入spark shell执行以下操作即可完成计算wordcount:
val s = sc.textFile("/wordcount/input.log")
val counts = s.flatMap(line => line.split(" ")).map(word => (word, 1)).reduceByKey(_ + _)
counts.saveAsTextFile("/wordcount/spark-out")
使用spark来分析发现速度明显比MR执行速度快的多。