-
Notifications
You must be signed in to change notification settings - Fork 0
/
etl.py
605 lines (450 loc) · 18.8 KB
/
etl.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
import os
import configparser
from pyspark.sql import SparkSession
def create_session(s3, sas):
try:
if s3:
config = configparser.ConfigParser()
config.read('config.cfg')
os.environ['AWS_ACCESS_KEY_ID']=config['AWS']['AWS_ACCESS_KEY_ID']
os.environ['AWS_SECRET_ACCESS_KEY']=config['AWS']['AWS_SECRET_ACCESS_KEY']
if sas:
spark = SparkSession.builder\
.config("spark.jars.repositories", "https://repos.spark-packages.org/")\
.config("spark.jars.packages", "saurfang:spark-sas7bdat:2.0.0-s_2.11")\
.config("spark.jars.packages", "org.apache.hadoop:hadoop-aws:2.7.0") \
.enableHiveSupport().getOrCreate()
print(" Configurations set for SAS7DBAT and AWS")
else:
spark = SparkSession.builder\
.config("spark.jars.packages", "org.apache.hadoop:hadoop-aws:2.7.0") \
.enableHiveSupport().getOrCreate()
print(" Configurations set for AWS")
print("Spark session created!")
except:
print("Spark session failed.")
return spark
#### ETL DIMENSION DATASET - AIRPORT ####
def etl_dim_airport(spark, input_data, output_data):
'''
Function:
1) loads input dimension table: lookup_airport
2) create a temporary spark view: df_airport
3) stage the data using a spark sql query
4) write to storage as parquet files
Parameters:
spark: the spark session cursor
input_data: csv file
output_path: path to S3 bucket or local directory
Returns:
Dataframe
'''
try:
print(" 1) Airport Codes Dimension Table ETL started...\n")
# Load raw Airport Code data
df = spark.read.format("csv").option("header", "true").load(input_data)
# Transform raw Airport Code dataset
df.createOrReplaceTempView("df_airport")
df_airport = spark.sql('''
SELECT
iata_code as i94port,
municipality as city
FROM
df_airport
WHERE
iata_code IS NOT NULL OR iata_code != ""
''')
print("Airport Codes ETL complete!\n")
except:
print("Airport Codes ETL failed.\n")
df_airport.write.parquet(output_data+"dim/",mode='overwrite')
return df_airport
#### ETL DIMENSION DATASET - COUNTRIES ####
def etl_dim_country(spark, input_data, output_data):
'''
Function:
1) loads input dimension table: lookup_country
2) create a temporary spark view: df_country
3) stage the data using a spark sql query
4) write to storage as parquet files
Parameters:
spark: the spark session cursor
input_data: csv file
output_path: path to S3 bucket or local directory
Returns:
Dataframe
'''
try:
print(" 2) Country Codes Dimension Table ETL started...\n")
# Load raw Country Codes data
df = spark.read.format("csv").option("header", "true").load(input_data)
# Transform raw Country Codes dataset
df.createOrReplaceTempView("df_country")
df_country = spark.sql('''
SELECT
df_country.code as i94cit,
df_country.country
FROM
df_country
WHERE
df_country.country NOT LIKE "INVALID%" OR
df_country.country NOT LIKE "NoCountry%"
''')
print("Country Codes ETL complete!\n")
except:
print("Country Codes ETL failed.\n")
df_country.write.parquet(output_data+"dim/",mode='overwrite')
return df_country
#### ETL DIMENSION DATASET - STATES ####
def etl_dim_states(spark, input_data, output_data):
'''
Function:
1) loads input dimension table: lookup_state
2) create a temporary spark view: df_state
3) stage the data using a spark sql query
4) write to storage as parquet files
Parameters:
spark: the spark session cursor
input_data: csv file
output_path: path to S3 bucket or local directory
Returns:
Dataframe
'''
try:
print(" 3) State Codes Dimension Table ETL started...\n")
# Load raw State Codes data
df = spark.read.format("csv").option("header", "true").load(input_data)
# Transform raw State Codes dataset
df.createOrReplaceTempView("df_states")
df_states = spark.sql('''
SELECT
df_states.state as i94addr,
df_states.state_name as state
FROM
df_states
''')
print("State Codes ETL complete!\n")
except:
print("State Codes ETL failed.\n")
print("\n Writing final results as parquet...\n")
df_states.write.parquet(output_data+"dim/",mode='overwrite')
return df_states
#### ETL FACT DATASET - IMMIGRATION I94 ####
def etl_fact(spark, input_data, output_data, sas):
'''
Function:
1) loads raw fact data
2) create a temporary spark view: df_immigration
3) stage the data using a spark sql query
4) write to storage as parquet files
Parameters:
spark: the spark session cursor
input_data: sas7dbat or parquet files
output_path: path to S3 bucket or local directory
sas: user entered parameter to switch sas7dbat and parquet format when reading input data
if sas is true, it will read source sas7dbat files
otherwise, it will read from local parquet files (test environment)
Returns:
Dataframe
'''
if sas:
df = spark.read.format('com.github.saurfang.sas.spark').load(input_data)
else:
df = spark.read.parquet(input_data)
# Initial validation to validate that the dataframe is not empty
if df.count() == 0:
Exception("Invalid dataset. Immigrations fact table is empty.")
else:
print("Total Records Loaded: " + str(df.count()))
try:
print("\n Immigration Data ETL starting...\n")
# Insert input data into a temporary view so it can be queried
df.createOrReplaceTempView("df_immigration")
# Query the specific columns and rows from the input data
df_immigration = spark.sql('''
SELECT
df_immigration.i94cit,
df_immigration.i94res,
df_immigration.i94addr,
df_immigration.i94port,
df_immigration.i94yr as year,
df_immigration.i94mon as month,
df_immigration.airline as flight_airline,
df_immigration.FLTNO as flight_number,
df_immigration.i94visa as visa_code,
df_immigration.visatype as visatype,
df_immigration.i94mode as mode_code,
df_immigration.biryear as birth_year,
df_immigration.i94bir as birth_age,
df_immigration.gender,
COUNT(DISTINCT df_immigration.arrdate) as total_flights,
SUM(df_immigration.count) AS total_people
FROM
df_immigration
WHERE
df_immigration.i94mode = 1
GROUP BY
df_immigration.i94cit,
df_immigration.i94res,
df_immigration.i94addr,
df_immigration.i94port,
df_immigration.i94yr,
df_immigration.i94mon,
df_immigration.airline,
df_immigration.FLTNO,
df_immigration.i94visa,
df_immigration.visatype,
df_immigration.i94mode,
df_immigration.biryear,
df_immigration.i94bir,
df_immigration.gender
''')
print("\nImmigration Data ETL complete!\n")
print("\n Writing final results as parquet...\n")
df_immigration.write.parquet(output_data+"fact/etl/",mode='overwrite',partitionBy=['year', 'month', 'i94addr'])
except:
print("\nImmigration Data ETL failed!\n")
return df_immigration
#### CLEAN FACT DATASET ####
def clean_fact(spark, df_immigration, df_country, df_states, df_airport, output_data):
'''
Function:
1) loads the results from etl_fact
2) loads the results from dimension table etl functions
3) create a temporary spark view for all the input dataframes
4) stage the data using spark sql queries
5) write to storage as parquet files
Parameters:
spark: the spark session cursor
df_immigration: dataframe returned by etl_fact
df_country: dataframe returned by etl_dim_country
df_states: dataframe returned by etl_dim_states
df_airport: dataframe returned by etl_dim_airport
output_path: path to S3 bucket or local directory
Returns:
Dataframe
'''
try:
# Insert input data into a temporary view so it can be queried
df_immigration.createOrReplaceTempView("df_immigration")
df_country.createOrReplaceTempView("df_country")
df_states.createOrReplaceTempView("df_states")
df_airport.createOrReplaceTempView("df_airport")
# Add calculated columns and slice dataset for non-null records based on key dimensions
df = spark.sql('''
SELECT
df_immigration.year || df_immigration.i94cit || df_immigration.month || df_immigration.i94port || df_immigration.flight_airline || df_immigration.flight_number || df_immigration.visa_code || df_immigration.visatype || df_immigration.birth_year || df_immigration.gender as irid,
df_immigration.year,
df_immigration.month,
df_immigration.i94addr,
df_immigration.i94port,
df_immigration.flight_airline,
df_immigration.flight_number,
df_immigration.gender,
df_immigration.i94cit,
IF(
df_immigration.visa_code = 1,
"Business",
IF(
df_immigration.visa_code = 2,
"Pleasure",
IF(
df_immigration.visa_code = 3,
"Student",
""
)
)
) as visa_type,
(df_immigration.year - df_immigration.birth_year) as age,
df_immigration.birth_year,
df_immigration.birth_age,
df_immigration.total_flights,
df_immigration.total_people
FROM
df_immigration
WHERE
df_immigration.i94cit IS NOT NULL AND
df_immigration.i94addr IS NOT NULL AND
df_immigration.i94port IS NOT NULL AND
df_immigration.flight_airline IS NOT NULL AND
df_immigration.birth_year IS NOT NULL AND
df_immigration.gender IS NOT NULL
''')
df.createOrReplaceTempView("df_immigration")
print("\n Data slicing and calculated fields done")
# Pull origin country name based on country code
df = spark.sql('''
SELECT
df_country.country as origin_country,
df_immigration.*
FROM
df_immigration
INNER JOIN df_country
USING (i94cit)
''')
df.createOrReplaceTempView("df_immigration")
print("\n Data join with country lookup done")
# Pull state name based on state code
df = spark.sql('''
SELECT
df_states.state,
df_immigration.*
FROM
df_immigration
INNER JOIN df_states
USING (i94addr)
''')
df.createOrReplaceTempView("df_immigration")
print("\n Data join with state lookup done")
# Pull city name based on airport
df = spark.sql('''
SELECT
df_airport.city,
df_immigration.*
FROM
df_immigration
INNER JOIN df_airport
USING (i94port)
''')
df.createOrReplaceTempView("df_immigration")
print("\n Data join with airport lookup done")
print("\n Writing final results as parquet...\n")
df.write.parquet(output_data+"fact/clean/",mode='overwrite',partitionBy=['year', 'month', 'state'])
print("\nImmigration Data Cleaning complete!\n")
except:
print("\nImmigration Data Cleaning failed!\n")
return df
#### VALIDATE FACT DATASET ####
def validation(spark, df):
'''
Function:
1) loads the results from clean_fact
2) create a temporary spark view
3) check if the input dataframe is empty
4) loop through key dimension columns and validate count of unique values
5) quality check the irid and determine if any duplicate records exist
Parameters:
spark: the spark session cursor
df: dataframe returned by clean_fact
Returns:
None
'''
try:
# Insert input data into a temporary view so it can be queried
df.createOrReplaceTempView("df_immigration")
# Initial validation to validate that the dataframe is not empty
if df.count() == 0:
Exception("Invalid dataset. Immigrations fact table is empty.")
else:
print("Total Records Loaded: " + str(df.count()))
print("\n Dimension columns validation starting...\n")
columns = [
("visa_type", 3),
("gender", 4)
]
for k, v in columns:
print("\n Unique Values Quality Validation for Column: {}".format(k))
query = spark.sql("SELECT COUNT(DISTINCT {}) FROM df_immigration".format(k))
result = query.collect()[0][0]
if result < v:
print(" PASSED! Unique values validation...\n Column {} has {} unique values\n Expected values were {}".format(k, result, v))
else:
print(" FAILED! Unique values validation...\n Column {} has {} unique values\n Expected values were {}".format(k, result, v))
print("\nImmigration Data Validation complete!\n")
except:
print("\nImmigration Data Validation failed!\n")
return df
def queries(spark, df):
'''
Function:
1) loads the results from clean_fact
2) create a temporary spark view
3) execute sample queries
Parameters:
spark: the spark session cursor
df: dataframe returned by clean_fact
Returns:
None
'''
try:
# Insert input data into a temporary view so it can be queried
df.createOrReplaceTempView("df")
print("Query 1: Top 5 routes with the most number of teenagers entered in a given month")
query = spark.sql("""
SELECT
df.month,
df.state,
df.city,
df.origin_country,
df.gender,
df.age,
SUM(df.total_flights) AS total_flights,
SUM(df.total_people) AS total_people,
SUM(df.total_people)/SUM(df.total_flights) AS avg_per_flight
FROM
df
WHERE
df.month = 4 AND
df.age < 19 AND
df.age > 12
GROUP BY
df.month,
df.state,
df.city,
df.origin_country,
df.gender,
df.age
ORDER BY
SUM(df.total_people) DESC
""")
print(query.sort(query.total_people.desc()).show(5, truncate=True))
print("Query 2: Routes with more than 5 teenagers per flight")
query.createOrReplaceTempView("query1")
query = spark.sql("""
SELECT
query1.*
FROM
query1
WHERE
query1.avg_per_flight > 5
""")
print(query.sort(query.total_people.desc()).show(5, truncate=True))
query.createOrReplaceTempView("query2")
print("\nSample queries complete!\n")
except:
print("\nSample queries failed!\n")
return df
def main():
# TODO Change below to True if reading/writing from local file in test environment
s3 = True
# TODO Add S3 URI or set to local directory
# output_data = 's3a://udacitydend20210713/fact_data/'
output_data = 'output/'
# TODO Change to True if reading SAS7DBAT files instead of parquet files
sas = False
# TODO Add SAS7DBAT files directory or parquet files (for testing purposes)
# input_data = '../../data/*/*.sas7bdat'
# input_data = '../../data/18-83510-I94-Data-2016/i94_apr16_sub.sas7bdat'
# input_data = 'sas_data/*'
input_data = 'sas_data/part-00002-b9542815-7a8d-45fc-9c67-c9c5007ad0d4-c000.snappy.parquet'
print("\n\nInitialize Spark...\n")
spark = create_session(s3, sas)
file_airports = 'airport_lookup.csv'
file_country = 'country_lookup.csv'
file_state = 'state_lookup.csv'
print("\n\nDimension tables ETL started...\n")
df_airport = etl_dim_airport(spark, file_airports, output_data)
df_country = etl_dim_country(spark, file_country, output_data)
df_states = etl_dim_states(spark, file_state, output_data)
print("\n\nFact table ETL started...\n")
df_immigration = etl_fact(spark, input_data, output_data, sas)
print("\n\nFact table cleaning started...\n")
df_immigration = clean_fact(spark, df_immigration, df_country, df_states, df_airport, output_data)
print(df_immigration.sort(df_immigration.total_people.desc()).show(5, truncate=False))
print("\n\nValidation started...\n")
validation(spark, df_immigration)
print("\n\nSample queries started...\n")
queries(spark, df_immigration)
print("\n\n\n DATA PIPELINE COMPLETE")
if __name__ == '__main__':
main()