-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathberlin_airport_pyspark_dataproc.py
124 lines (74 loc) · 4.07 KB
/
berlin_airport_pyspark_dataproc.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env python
# coding: utf-8
# In[3]:
from datetime import datetime
from datetime import timedelta
today = datetime.today().strftime('%Y-%m-%d')
yesterday = datetime.today() - timedelta(days=1)
# In[8]:
import argparse
import pyspark
from pyspark.sql import SparkSession
from pyspark.sql import functions as F
parser = argparse.ArgumentParser()
parser.add_argument('--input_departures', required=True)
parser.add_argument('--input_arrivals', required=True)
parser.add_argument('--output', required=True)
args = parser.parse_args()
input_departures = args.input_departures
input_arrivals = args.input_arrivals
output = args.output
spark = SparkSession.builder .appName('test') .getOrCreate()
spark.conf.set('temporaryGcsBucket', 'dataproc_bucket_name')
# In[ ]:
from pyspark.sql import types
# In[ ]:
arrivals_schema = types.StructType([
types.StructField('flight_id', types.StringType(), True),
types.StructField('flight_identification_number', types.StringType(), True),
types.StructField('flight_status', types.StringType(), True),
types.StructField('flight_status_color', types.StringType(), True),
types.StructField('flight_aircraft_model', types.StringType(), True),
types.StructField('flight_airline_name', types.StringType(), True),
types.StructField('airport_origin_name', types.StringType(), True),
types.StructField('airport_origin_country', types.StringType(), True),
types.StructField('airport_origin_city', types.StringType(), True),
types.StructField('airport_origin_timezone', types.StringType(), True),
types.StructField('airport_destination_name', types.StringType(), True),
types.StructField('airport_destination_timezone', types.StringType(), True),
types.StructField('airport_destination_country', types.StringType(), True),
types.StructField('airport_destination_city', types.StringType(), True),
types.StructField('flight_time_scheduled_departure', types.TimestampType(), True),
types.StructField('flight_time_scheduled_arrival', types.TimestampType(), True),
types.StructField('flight_time_real_departure', types.TimestampType(), True),
types.StructField('flight_time_real_arrival', types.TimestampType(), True),
types.StructField('flight_time_duration_minutes', types.LongType(), True)])
# In[ ]:
departures_schema = types.StructType([
types.StructField('flight_id', types.StringType(), True),
types.StructField('flight_identification_number', types.StringType(), True),
types.StructField('flight_status', types.StringType(), True),
types.StructField('flight_status_color', types.StringType(), True),
types.StructField('flight_aircraft_model', types.StringType(), True),
types.StructField('flight_airline_name', types.StringType(), True),
types.StructField('airport_origin_name', types.StringType(), True),
types.StructField('airport_origin_country', types.StringType(), True),
types.StructField('airport_origin_city', types.StringType(), True),
types.StructField('airport_origin_timezone', types.StringType(), True),
types.StructField('airport_destination_name', types.StringType(), True),
types.StructField('airport_destination_timezone', types.StringType(), True),
types.StructField('airport_destination_country', types.StringType(), True),
types.StructField('airport_destination_city', types.StringType(), True),
types.StructField('flight_time_scheduled_departure', types.TimestampType(), True),
types.StructField('flight_time_scheduled_arrival', types.TimestampType(), True),
types.StructField('flight_time_real_departure', types.TimestampType(), True),
types.StructField('flight_time_real_arrival', types.TimestampType(), True),
types.StructField('flight_time_duration_minutes', types.LongType(), True)])
# In[ ]:
df_arrivals_spark = spark.read.schema(arrivals_schema).options(delimiter=',',header='true',).csv(input_arrivals)
# In[ ]:
df_departures_spark = spark.read.schema(departures_schema).options(delimiter=',',header='true',).csv(input_departures)
# In[ ]:
df_union = df_departures_spark.union(df_arrivals_spark)
# In[ ]:
df_union.write.format('bigquery') .option('table', output) .mode("overwrite") .save()