File tree Expand file tree Collapse file tree 1 file changed +8
-10
lines changed Expand file tree Collapse file tree 1 file changed +8
-10
lines changed Original file line number Diff line number Diff line change 1
- from pyspark import SparkContext
1
+ from pyspark import SparkContext , SparkConf
2
2
3
3
if __name__ == "__main__" :
4
-
5
- sc = SparkContext ("local" , "wordCounts" )
6
- sc . setLogLevel ( "ERROR" )
4
+ conf = SparkConf (). setAppName ( "wordCounts" ). setMaster ( "local[*]" )
5
+ sc = SparkContext (conf = conf )
6
+
7
7
lines = sc .textFile ("in/word_count.text" )
8
8
wordRdd = lines .flatMap (lambda line : line .split (" " ))
9
9
10
10
wordPairRdd = wordRdd .map (lambda word : (word , 1 ))
11
11
wordToCountPairs = wordPairRdd .reduceByKey (lambda x , y : x + y )
12
12
13
- countToWordParis = wordToCountPairs .map (lambda wordToCount : (wordToCount [1 ], wordToCount [0 ]))
14
-
15
- sortedCountToWordParis = countToWordParis .sortByKey (ascending = False )
13
+ sortedWordCountPairs = wordToCountPairs \
14
+ .sortBy (lambda wordCount : wordCount [1 ], ascending = False )
16
15
17
- sortedWordToCountPairs = sortedCountToWordParis .map (lambda countToWord : (countToWord [1 ], countToWord [0 ]))
18
-
19
- for word , count in sortedWordToCountPairs .collect ():
16
+ for word , count in sortedWordCountPairs .collect ():
20
17
print ("{} : {}" .format (word , count ))
18
+
You can’t perform that action at this time.
0 commit comments