-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.ingest_circuits_file.py
87 lines (55 loc) · 2.44 KB
/
1.ingest_circuits_file.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Databricks notebook source
# MAGIC %md
# MAGIC ### Ingest circuits.csv file
# COMMAND ----------
# MAGIC %md
# MAGIC ##### Step 1 - Read the CSV file using the spark dataframe reader
# COMMAND ----------
from pyspark.sql.types import StructType, StructField, IntegerType, StringType, DoubleType
# COMMAND ----------
circuits_schema = StructType(fields=[StructField("circuitId", IntegerType(), False),
StructField("circuitRef", StringType(), True),
StructField("name", StringType(), True),
StructField("location", StringType(), True),
StructField("country", StringType(), True),
StructField("lat", DoubleType(), True),
StructField("lng", DoubleType(), True),
StructField("alt", IntegerType(), True),
StructField("url", StringType(), True)
])
# COMMAND ----------
circuits_df = spark.read \
.option("header", True) \
.schema(circuits_schema) \
.csv("/mnt/formula1dl/raw/circuits.csv")
# COMMAND ----------
# MAGIC %md
# MAGIC ##### Step 2 - Select only the required columns
# COMMAND ----------
from pyspark.sql.functions import col
# COMMAND ----------
circuits_selected_df = circuits_df.select(col("circuitId"), col("circuitRef"), col("name"), col("location"), col("country"), col("lat"), col("lng"), col("alt"))
# COMMAND ----------
# MAGIC %md
# MAGIC ##### Step 3 - Rename the columns as required
# COMMAND ----------
circuits_renamed_df = circuits_selected_df.withColumnRenamed("circuitId", "circuit_id") \
.withColumnRenamed("circuitRef", "circuit_ref") \
.withColumnRenamed("lat", "latitude") \
.withColumnRenamed("lng", "longitude") \
.withColumnRenamed("alt", "altitude")
# COMMAND ----------
# MAGIC %md
# MAGIC ##### Step 4 - Add ingestion date to the dataframe
# COMMAND ----------
from pyspark.sql.functions import current_timestamp
# COMMAND ----------
circuits_final_df = circuits_renamed_df.withColumn("ingestion_date", current_timestamp())
# COMMAND ----------
# MAGIC %md
# MAGIC ##### Step 5 - Write data to datalake as parquet
# COMMAND ----------
circuits_final_df.write.mode("overwrite").parquet("/mnt/formula1dl/processed/circuits")
# COMMAND ----------
display(spark.read.parquet("/mnt/formula1dl/processed/circuits"))
# COMMAND ----------