import org.apache.spark.SparkContext
import org.apache.spark.SparkContext._
import org.apache.spark.SparkConf
import java.io.PrintWriter
object WordCount {
def main(args: Array[String]) {
val inputFile = "file:///usr/local/spark/mycode/wordcount/A.txt"
val conf = new SparkConf().setAppName("WordCount").setMaster("local[2]")
val sc = new SparkContext(conf)
val textFile = sc.textFile(inputFile)
val startTime = System.currentTimeMillis()
val wordCount = textFile.flatMap(line => line.split(" ")).map(word => (word, 1)).reduceByKey((a, b) => a + b)
val endTime = System.currentTimeMillis()
val totalTime = endTime - startTime
val outputFilePath = "file:///usr/local/spark/mycode/wordcount/output_A.txt"
val writer = new PrintWriter(outputFilePath)
wordCount.sortBy(-_._2).collect().foreach { case (word, count) => writer.println(s"$word: $count") }
writer.close()
println(s"WordCount completed in ${totalTime}ms")
}
}