Skip to content

Scala to Python - sparkSql folder #2

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 6 commits into from
Sep 30, 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
Added sparkSql/join/UkMakerSpaces.py
  • Loading branch information
Pedro Bernardo committed Sep 30, 2017
commit 62d6afd4c81980a073c10c50ca1869b36dd318dc
27 changes: 27 additions & 0 deletions sparkSql/join/UkMakerSpaces.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from pyspark.sql import SparkSession, functions as fs

if __name__ == "__main__":

session = SparkSession.builder.appName("UkMakerSpaces").master("local").getOrCreate()
sc = session.sparkContext
sc.setLogLevel("ERROR")

makerSpace = session.read \
.option("header", "true") \
.csv("in/uk-makerspaces-identifiable-data.csv")

postCode = session.read \
.option("header", "true") \
.csv("in/uk-postcode.csv") \
.withColumn("PostCode", fs.concat_ws("", fs.col("PostCode"), fs.lit(" ")))

print("=== Print 20 records of makerspace table ===")
makerSpace.select("Name of makerspace", "Postcode").show()

print("=== Print 20 records of postcode table ===")
postCode.show()

joined = makerSpace.join(postCode, makerSpace["Postcode"].startswith(postCode["Postcode"]), "left_outer")

print("=== Group by Region ===")
joined.groupBy("Region").count().show(200)