Skip to content

Commit f66c9e6

Browse files
author
James Lee
committed
adapt SortedWordCountSolution
1 parent 052196f commit f66c9e6

File tree

2 files changed

+28
-39
lines changed

2 files changed

+28
-39
lines changed

src/main/scala/com/sparkTutorial/pairRdd/sort/SortedWordCountSolution.java

Lines changed: 0 additions & 39 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.sparkTutorial.pairRdd.sort
2+
3+
import org.apache.log4j.{Level, Logger}
4+
import org.apache.spark.{SparkConf, SparkContext}
5+
6+
object SortedWordCountSolution {
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 wordToCountPairs = wordPairRdd.reduceByKey((x, y) => x + y)
19+
20+
val countToWordParis = wordToCountPairs.map(wordToCount => (wordToCount._2, wordToCount._1))
21+
22+
val sortedCountToWordParis = countToWordParis.sortByKey(ascending = false)
23+
24+
val sortedWordToCountPairs = sortedCountToWordParis.map(countToWord => (countToWord._2, countToWord._1))
25+
26+
for ((word, count) <- sortedWordToCountPairs.collect()) println(word + " : " + count)
27+
}
28+
}

0 commit comments

Comments
 (0)