Skip to content

Scala to Python - pairRdd folder #4

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 10 commits into from
Oct 2, 2017
Merged
Show file tree
Hide file tree
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
Next Next commit
Added pairRdd/sort/*.py
  • Loading branch information
Pedro Bernardo committed Oct 2, 2017
commit 4f0a0147ff6234b420a3a4d447a7c068fa428fb7
23 changes: 23 additions & 0 deletions pairRdd/sort/AverageHousePriceSolution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from pairRdd.aggregation.reducebykey.housePrice.AvgCount import AvgCount
from pyspark import SparkContext


if __name__ == "__main__":

sc = SparkContext("local", "averageHousePriceSolution")
sc.setLogLevel("ERROR")

lines = sc.textFile("in/RealEstate.csv")
cleanedLines = lines.filter(lambda line: "Bedrooms" not in line)
housePricePairRdd = cleanedLines.map(lambda line: \
((int(float(line.split(",")[3]))), AvgCount(1, float(line.split(",")[2]))))

housePriceTotal = housePricePairRdd.reduceByKey(lambda x, y: \
AvgCount(x.count + y.count, x.total + y.total))

housePriceAvg = housePriceTotal.mapValues(lambda avgCount: avgCount.total / avgCount.count)

sortedHousePriceAvg = housePriceAvg.sortByKey()

for bedrooms, avgPrice in sortedHousePriceAvg.collect():
print("{} : {}".format(bedrooms, avgPrice))
16 changes: 16 additions & 0 deletions pairRdd/sort/SortedWordCountProblem.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from pyspark import SparkContext

if __name__ == "__main__":

'''
Create a Spark program to read the an article from in/word_count.text,
output the number of occurrence of each word in descending order.

Sample output:

apple : 200
shoes : 193
bag : 176
...

'''
20 changes: 20 additions & 0 deletions pairRdd/sort/SortedWordCountSolution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from pyspark import SparkContext

if __name__ == "__main__":

sc = SparkContext("local", "wordCounts")
sc.setLogLevel("ERROR")
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)

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

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