forked from jonesberg/DataAnalysisWithPythonAndPySpark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkpoint.py
31 lines (26 loc) · 849 Bytes
/
checkpoint.py
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
"""Checkpoint src for the book Data Analysis with Python and PySpark, Chapter 4."""
import os
from pyspark.sql import SparkSession
import pyspark.sql.functions as F
spark = SparkSession.builder.getOrCreate()
spark.sparkContext.setLogLevel("WARN")
DIRECTORY = "../../data/broadcast_logs"
logs = (
spark.read.csv(
os.path.join(DIRECTORY, "BroadcastLogs_2018_Q3_M8_sample.CSV"),
sep="|",
header=True,
inferSchema=True,
timestampFormat="yyyy-MM-dd",
)
.drop("BroadcastLogID", "SequenceNO")
.withColumn(
"duration_seconds",
(
F.col("Duration").substr(1, 2).cast("int") * 60 * 60
+ F.col("Duration").substr(4, 2).cast("int") * 60
+ F.col("Duration").substr(7, 2).cast("int")
),
)
)
logs.select('duration_seconds').summary().show()