object WordCount {
def main(args: Array[String]): Unit = {
//conf对象
val conf = new SparkConf().setAppName("wordcount").setMaster("local[1]")
//上下文对象
val sc = new SparkContext(conf)
// val lines = sc.textFile("hdfs://spark001:9000/spark.txt", 1)
val lines = sc.textFile("spark.txt", 1)
val words = lines.flatMap { line => line.split(" ") }
val pairs = words.map { word => (word, 1) }
val wordcounts = pairs.reduceByKey { _ + _ }
val haha = wordcounts.map(x => ((-1)*x._2 , x._1))
val doubi =haha.sortByKey().collect
doubi.foreach { x => println(x._2 + " appears " + (-1)*x._1 + " times.") }
}
}
Spark入门的WordCount
最新推荐文章于 2023-05-01 21:17:48 发布