Skip to content

Commit 7cbb399

Browse files
authored
Create AnotherSortedWordCountSolution.scala
1 parent b16b4a8 commit 7cbb399

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.sparkTutorial.pairRdd.sort
2+
3+
import org.apache.log4j.{Level, Logger}
4+
import org.apache.spark.{SparkConf, SparkContext}
5+
6+
object AnotherSortedWordCountSolution {
7+
8+
def main(args: Array[String]) {
9+
10+
Logger.getLogger("org").setLevel(Level.ERROR)
11+
val conf = new SparkConf().setAppName("wordCounts").setMaster("local[3]")
12+
val sc = new SparkContext(conf)
13+
14+
val lines = sc.textFile("in/word_count.text")
15+
val wordRdd = lines.flatMap(line => line.split(" "))
16+
17+
val wordPairRdd = wordRdd.map(word => (word, 1))
18+
val wordCounts = wordPairRdd.reduceByKey((x, y) => x + y)
19+
20+
val sortedWordCounts = wordCounts.sortBy(wordCount => wordCount._2, ascending = false)
21+
22+
for ((word, count) <- sortedWordCounts.collect()) println(word + " : " + count)
23+
24+
}
25+
}

0 commit comments

Comments
 (0)