-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlength_of_play_sql.py
More file actions
39 lines (31 loc) · 890 Bytes
/
length_of_play_sql.py
File metadata and controls
39 lines (31 loc) · 890 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from pyspark.sql import SparkSession
import pyspark.sql.functions as sf
import os.path
# Start Spark
spark = SparkSession \
.builder \
.appName("pyspark example") \
.getOrCreate()
basedir = os.path.dirname(os.path.realpath(__file__))
# Read all of Shakespeare's plays
df = spark.read.parquet(os.path.join(basedir,
"data/shakespeare.gz.parquet"))
# Print the schema to the console
df.printSchema()
# Calculate the number of lines for each work (play)
df.createOrReplaceTempView("shakespeare")
result = spark.sql("""
SELECT play_name, count(line_id) AS lines
FROM shakespeare
GROUP BY play_name
ORDER BY lines DESC
""")
# Print a part of the result to the console
result.show()
# Save the result as one file in JSON Lines format
result \
.repartition(1) \
.write \
.json(os.path.join(basedir, "length_of_play"), mode="overwrite")
# Stop Spark
spark.stop()