-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlength_of_play.py
More file actions
36 lines (28 loc) · 850 Bytes
/
Copy pathlength_of_play.py
File metadata and controls
36 lines (28 loc) · 850 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
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)
result = df \
.groupBy("play_name") \
.agg(sf.count("line_id").alias("lines")) \
.orderBy("lines", ascending=False)
# 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()