File tree 1 file changed +25
-0
lines changed
src/main/scala/com/sparkTutorial/pairRdd/sort
1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments