-
Notifications
You must be signed in to change notification settings - Fork 0
/
1. ingestion_circuits_file.py
97 lines (60 loc) · 2.52 KB
/
1. ingestion_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
87
88
89
90
91
92
93
94
95
# 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 ----------
circuit_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 ----------
circuit_df = spark.read \
.option("header", True) \
.schema(circuit_schema) \
.csv("/mnt/formuladluche/raw/circuits.csv")
# COMMAND ----------
# MAGIC %md
# MAGIC ##### Step 2 - Select the required columns
# COMMAND ----------
from pyspark.sql.functions import col
# COMMAND ----------
circuit_selected_df = circuit_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 ----------
circuit_renamed_df = circuit_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 as column
# COMMAND ----------
from pyspark.sql.functions import current_timestamp
# COMMAND ----------
circuit_final_df = circuit_renamed_df.withColumn("ingestion_date", current_timestamp())
# COMMAND ----------
# MAGIC %md
# MAGIC ##### Step 5 - Write data to parquet
# COMMAND ----------
circuit_final_df.write.mode("overwrite").parquet("/mnt/formuladluche/processed/circuits")
# COMMAND ----------
# MAGIC %fs
# MAGIC ls /mnt/formuladluche/processed/circuits
# COMMAND ----------
df = spark.read.parquet("/mnt/formuladluche/processed/circuits")
# COMMAND ----------
display(df)
# COMMAND ----------