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
Prev Previous commit
Next Next commit
Added pairRdd/groupbykey/*.py
  • Loading branch information
Pedro Bernardo committed Oct 2, 2017
commit d6b58da2436d979a76bc0ed9ffa9027f65a2938f
23 changes: 23 additions & 0 deletions pairRdd/groupbykey/AirportsByCountryProblem.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from pyspark import SparkContext

if __name__ == "__main__":

'''
Create a Spark program to read the airport data from in/airports.text,
output the the list of the names of the airports located in each country.

Each row of the input file contains the following columns:
Airport ID, Name of airport, Main city served by airport, Country where airport is located, IATA/FAA code,
ICAO Code, Latitude, Longitude, Altitude, Timezone, DST, Timezone in Olson format

Sample output:

"Canada", ["Bagotville", "Montreal", "Coronation", ...]
"Norway" : ["Vigra", "Andenes", "Alta", "Bomoen", "Bronnoy",..]
"Papua New Guinea", ["Goroka", "Madang", ...]
...

'''



18 changes: 18 additions & 0 deletions pairRdd/groupbykey/AirportsByCountrySolution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from pyspark import SparkContext
from commons.Utils import Utils

if __name__ == "__main__":

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

lines = sc.textFile("in/airports.text")

countryAndAirportNameAndPair = lines.map(lambda airport:\
(Utils.COMMA_DELIMITER.split(airport)[3],
Utils.COMMA_DELIMITER.split(airport)[1]))

airportsByCountry = countryAndAirportNameAndPair.groupByKey()

for country, airportName in airportsByCountry.collectAsMap().items():
print("{}: {}".format(country,list(airportName)))
18 changes: 18 additions & 0 deletions pairRdd/groupbykey/GroupByKeyVsReduceByKey.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from pyspark import SparkContext

if __name__ == "__main__":

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

words = ["one", "two", "two", "three", "three", "three"]
wordsPairRdd = sc.parallelize(words).map(lambda word: (word, 1))

wordCountsWithReduceByKey = wordsPairRdd.reduceByKey(lambda x, y: x + y).collect()
print("wordCountsWithReduceByKey: {}".format(list(wordCountsWithReduceByKey)))

wordCountsWithGroupByKey = wordsPairRdd \
.groupByKey() \
.mapValues(lambda intIterable: len(intIterable)) \
.collect()
print("wordCountsWithGroupByKey: {}".format(list(wordCountsWithGroupByKey)))