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 all commits
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
21 changes: 21 additions & 0 deletions pairRdd/aggregation/combinebykey/AverageHousePriceSolution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from pyspark import SparkContext

if __name__ == "__main__":

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

lines = sc.textFile("in/RealEstate.csv")
cleanedLines = lines.filter(lambda line: "Bedrooms" not in line)

housePricePairRdd = cleanedLines.map(lambda line: (line.split(",")[3], float(line.split(",")[2])))

createCombiner = lambda x: (1, x)
mergeValue = lambda avgCount, x: (avgCount[0] + 1, avgCount[1] + x)
mergeCombiners = lambda avgCountA, avgCountB: (avgCountA[0] + avgCountB[0], avgCountA[1] + avgCountB[1])

housePriceTotal = housePricePairRdd.combineByKey(createCombiner, mergeValue, mergeCombiners)

housePriceAvg = housePriceTotal.mapValues(lambda avgCount: avgCount[1] / avgCount[0])
for bedrooms, avgPrice in housePriceAvg.collect():
print("{} : {}".format(bedrooms, avgPrice))
30 changes: 0 additions & 30 deletions pairRdd/aggregation/combinebykey/AverageHousePriceSolution.scala

This file was deleted.

14 changes: 14 additions & 0 deletions pairRdd/aggregation/reducebykey/WordCount.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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))

wordCounts = wordPairRdd.reduceByKey(lambda x, y: x + y)
for word, count in wordCounts.collect():
print("{} : {}".format(word, count))
20 changes: 0 additions & 20 deletions pairRdd/aggregation/reducebykey/WordCount.scala

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.sparkTutorial.pairRdd.aggregation.reducebykey.housePrice
from pyspark import SparkContext

object AverageHousePriceProblem {
if __name__ == "__main__":

def main(args: Array[String]) {

/* Create a Spark program to read the house data from in/RealEstate.csv,
output the average price for houses with different number of bedrooms.
'''
Create a Spark program to read the house data from in/RealEstate.csv,
output the average price for houses with different number of bedrooms.

The houses dataset contains a collection of recent real estate listings in San Luis Obispo county and
around it. 
Expand All @@ -31,8 +30,6 @@ def main(args: Array[String]) {
(2, 325000)
...

3, 1 and 2 mean the number of bedrooms. 325000 means the average price of houses with 3 bedrooms is 325000.
*/
}
3, 1 and 2 mean the number of bedrooms. 325000 means the average price of houses with 3 bedrooms is 325000.

}
'''
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from pyspark import SparkContext

if __name__ == "__main__":

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

lines = sc.textFile("in/RealEstate.csv")
cleanedLines = lines.filter(lambda line: "Bedrooms" not in line)

housePricePairRdd = cleanedLines.map(lambda line: \
(line.split(",")[3], (1, float(line.split(",")[2]))))

housePriceTotal = housePricePairRdd \
.reduceByKey(lambda x, y: (x[0] + y[0], x[1] + y[1]))

print("housePriceTotal: ")
for bedroom, total in housePriceTotal.collect():
print("{} : {}".format(bedroom, total))

housePriceAvg = housePriceTotal.mapValues(lambda avgCount: avgCount[1] / avgCount[0])
print("\nhousePriceAvg: ")
for bedroom, avg in housePriceAvg.collect():
print("{} : {}".format(bedroom, avg))

This file was deleted.

7 changes: 7 additions & 0 deletions pairRdd/aggregation/reducebykey/housePrice/AvgCount.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class AvgCount():

def __init__(self, count: int, total: float):
self.count = count
self.total = total


4 changes: 0 additions & 4 deletions pairRdd/aggregation/reducebykey/housePrice/AvgCount.scala

This file was deleted.

12 changes: 12 additions & 0 deletions pairRdd/create/PairRddFromRegularRdd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from pyspark import SparkContext

if __name__ == "__main__":

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

inputStrings = ["Lily 23", "Jack 29", "Mary 29", "James 8"]
regularRDDs = sc.parallelize(inputStrings)

pairRDD = regularRDDs.map(lambda s: (s.split(" ")[0], s.split(" ")[1]))
pairRDD.coalesce(1).saveAsTextFile("out/pair_rdd_from_regular_rdd")
18 changes: 0 additions & 18 deletions pairRdd/create/PairRddFromRegularRdd.scala

This file was deleted.

11 changes: 11 additions & 0 deletions pairRdd/create/PairRddFromTupleList.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from pyspark import SparkContext

if __name__ == "__main__":

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

tuples = [("Lily", 23), ("Jack", 29), ("Mary", 29), ("James", 8)]
pairRDD = sc.parallelize(tuples)

pairRDD.coalesce(1).saveAsTextFile("out/pair_rdd_from_tuple_list")
17 changes: 0 additions & 17 deletions pairRdd/create/PairRddFromTupleList.scala

This file was deleted.

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

if __name__ == "__main__":

'''
Create a Spark program to read the airport data from in/airports.text;
generate a pair RDD with airport name being the key and country name being the value.
Then remove all the airports which are located in United States and output the pair RDD to out/airports_not_in_usa_pair_rdd.text

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:

("Kamloops", "Canada")
("Wewak Intl", "Papua New Guinea")
...

'''
22 changes: 0 additions & 22 deletions pairRdd/filter/AirportsNotInUsaProblem.scala

This file was deleted.

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

if __name__ == "__main__":

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

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

airportPairRDD = airportsRDD.map(lambda line: \
(Utils.COMMA_DELIMITER.split(line)[1],
Utils.COMMA_DELIMITER.split(line)[3]))
airportsNotInUSA = airportPairRDD.filter(lambda keyValue: keyValue[1] != "\"United States\"")

airportsNotInUSA.saveAsTextFile("out/airports_not_in_usa_pair_rdd.text")
21 changes: 0 additions & 21 deletions pairRdd/filter/AirportsNotInUsaSolution.scala

This file was deleted.

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", ...]
...

'''



22 changes: 0 additions & 22 deletions pairRdd/groupbykey/AirportsByCountryProblem.scala

This file was deleted.

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)))
Loading