Skip to content

Pedro changes for videos #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 17, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Changed SortedWordCountSolution.py to use the sortBy operation
  • Loading branch information
Pedro Bernardo committed Dec 17, 2017
commit a6dc078dfd3ca12df8f9de7a69ab9348cfcf9109
18 changes: 8 additions & 10 deletions pairRdd/sort/SortedWordCountSolution.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
from pyspark import SparkContext
from pyspark import SparkContext, SparkConf

if __name__ == "__main__":

sc = SparkContext("local", "wordCounts")
sc.setLogLevel("ERROR")
conf = SparkConf().setAppName("wordCounts").setMaster("local[*]")
sc = SparkContext(conf = conf)

lines = sc.textFile("in/word_count.text")
wordRdd = lines.flatMap(lambda line: line.split(" "))

wordPairRdd = wordRdd.map(lambda word: (word, 1))
wordToCountPairs = wordPairRdd.reduceByKey(lambda x, y: x + y)

countToWordParis = wordToCountPairs.map(lambda wordToCount: (wordToCount[1], wordToCount[0]))

sortedCountToWordParis = countToWordParis.sortByKey(ascending=False)
sortedWordCountPairs = wordToCountPairs \
.sortBy(lambda wordCount: wordCount[1], ascending=False)

sortedWordToCountPairs = sortedCountToWordParis.map(lambda countToWord: (countToWord[1], countToWord[0]))

for word, count in sortedWordToCountPairs.collect():
for word, count in sortedWordCountPairs.collect():
print("{} : {}".format(word, count))