Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita6dc078

Browse files
author
Pedro Bernardo
committed
Changed SortedWordCountSolution.py to use the sortBy operation
1 parentf9c885f commita6dc078

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed
Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
frompysparkimportSparkContext
1+
frompysparkimportSparkContext,SparkConf
22

33
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+
77
lines=sc.textFile("in/word_count.text")
88
wordRdd=lines.flatMap(lambdaline:line.split(" "))
99

1010
wordPairRdd=wordRdd.map(lambdaword: (word,1))
1111
wordToCountPairs=wordPairRdd.reduceByKey(lambdax,y:x+y)
1212

13-
countToWordParis=wordToCountPairs.map(lambdawordToCount: (wordToCount[1],wordToCount[0]))
14-
15-
sortedCountToWordParis=countToWordParis.sortByKey(ascending=False)
13+
sortedWordCountPairs=wordToCountPairs \
14+
.sortBy(lambdawordCount:wordCount[1],ascending=False)
1615

17-
sortedWordToCountPairs=sortedCountToWordParis.map(lambdacountToWord: (countToWord[1],countToWord[0]))
18-
19-
forword,countinsortedWordToCountPairs.collect():
16+
forword,countinsortedWordCountPairs.collect():
2017
print("{} : {}".format(word,count))
18+

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp