-
Notifications
You must be signed in to change notification settings - Fork 3
/
Pyspark-ETL-Pipeline
1217 lines (1217 loc) · 49.4 KB
/
Pyspark-ETL-Pipeline
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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Pyspark ETL Pipeline \n",
"### Data Engineering Capstone Project\n",
"\n",
"#### Project Summary\n",
"This project builds an ETL pipeline for Immigration Data and City Temperature Data to form a final fact table to help answer questions on the relationships between the two datasets.\n",
"\n",
"The project follows the follow steps:\n",
"* Step 1: Scope the Project and Gather Data\n",
"* Step 2: Explore and Assess the Data\n",
"* Step 3: Define the Data Model\n",
"* Step 4: Run ETL to Model the Data\n",
"* Step 5: Complete Project Write Up"
]
},
{
"cell_type": "code",
"execution_count": 87,
"metadata": {},
"outputs": [],
"source": [
"# Do all imports and installs here\n",
"import pandas as pd, re\n",
"from pyspark.sql import SparkSession\n",
"from pyspark.sql.functions import udf, lower, col"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 1: Scope the Project and Gather Data\n",
"\n",
"#### Scope \n",
"\n",
"This project consumes and aggregates I94 Immigration Data from the US National Tourism and Trade Office and Temperature Data from Kaggle to determine if there is a correlation between immigration patterns and temperature averages in cities. The data creates a fact table by joining on city. Spark is used in this process. \n",
"\n",
"#### Describe and Gather Data \n",
"When exploring data there are several factors you have to figure out about your data before combining, cleaning, and filtering it. Below are what columns are important in each of the datasets imported.\n",
"\n",
"#### Immigration Data: Us National Tourism and Trade Office (SAS7BDAT format)\n",
"I94 Immigration Data: This data comes from the US National Tourism and Trade Office. A data dictionary is included in the workspace. This is where the data comes from. There's a sample file so you can take a look at the data in csv format before reading it all in. You do not have to use the entire dataset, just use what you need to accomplish the goal you set at the beginning of the project.\n",
"\n",
"* cicid = city id\n",
"* i94yr = 4 digit year\n",
"* i94mon = numeric month\n",
"* i94cit = 3 digit code of origin city\n",
"* i94port = 3 character code of destination USA city\n",
"* arrdate = arrival date in the USA\n",
"* i94mode = 1 digit travel code\n",
"* depdate = departure date from the USA\n",
"* i94visa = reason for immigration\n",
"\n",
"#### Temperature data: Kaggle (csv format)\n",
"World Temperature Data: This dataset came from Kaggle. You can read more about it here.\n",
"\n",
"* AverageTemperature = average temperature\n",
"* AverageTemperatureUncertainy = average temperature uncertainty\n",
"* City = city name\n",
"* Country = country name\n",
"* Latitude= latitude\n",
"* Longitude = longitude\n",
"\n",
"#### Correct SAS Label Data\n",
"* i94port = numerical value\n",
"* city = city name"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Scoping the project\n",
"This section answers the following initial questions about the data. \n",
"1. How big is the data?\n",
"2. What columns are in the data?\n",
"3. What are the column formats"
]
},
{
"cell_type": "code",
"execution_count": 88,
"metadata": {},
"outputs": [],
"source": [
"# Read in the data here\n",
"fname = '../../data/18-83510-I94-Data-2016/i94_apr16_sub.sas7bdat'\n",
"df_i94 = pd.read_sas(fname, 'sas7bdat', encoding=\"ISO-8859-1\")\n"
]
},
{
"cell_type": "code",
"execution_count": 89,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(3096313, 28)"
]
},
"execution_count": 89,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_i94.shape"
]
},
{
"cell_type": "code",
"execution_count": 90,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Index(['cicid', 'i94yr', 'i94mon', 'i94cit', 'i94res', 'i94port', 'arrdate',\n",
" 'i94mode', 'i94addr', 'depdate', 'i94bir', 'i94visa', 'count',\n",
" 'dtadfile', 'visapost', 'occup', 'entdepa', 'entdepd', 'entdepu',\n",
" 'matflag', 'biryear', 'dtaddto', 'gender', 'insnum', 'airline',\n",
" 'admnum', 'fltno', 'visatype'],\n",
" dtype='object')"
]
},
"execution_count": 90,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_i94.columns"
]
},
{
"cell_type": "code",
"execution_count": 91,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>cicid</th>\n",
" <th>i94yr</th>\n",
" <th>i94mon</th>\n",
" <th>i94cit</th>\n",
" <th>i94res</th>\n",
" <th>i94port</th>\n",
" <th>arrdate</th>\n",
" <th>i94mode</th>\n",
" <th>i94addr</th>\n",
" <th>depdate</th>\n",
" <th>...</th>\n",
" <th>entdepu</th>\n",
" <th>matflag</th>\n",
" <th>biryear</th>\n",
" <th>dtaddto</th>\n",
" <th>gender</th>\n",
" <th>insnum</th>\n",
" <th>airline</th>\n",
" <th>admnum</th>\n",
" <th>fltno</th>\n",
" <th>visatype</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>6.0</td>\n",
" <td>2016.0</td>\n",
" <td>4.0</td>\n",
" <td>692.0</td>\n",
" <td>692.0</td>\n",
" <td>XXX</td>\n",
" <td>20573.0</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>...</td>\n",
" <td>U</td>\n",
" <td>NaN</td>\n",
" <td>1979.0</td>\n",
" <td>10282016</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>1.897628e+09</td>\n",
" <td>NaN</td>\n",
" <td>B2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>7.0</td>\n",
" <td>2016.0</td>\n",
" <td>4.0</td>\n",
" <td>254.0</td>\n",
" <td>276.0</td>\n",
" <td>ATL</td>\n",
" <td>20551.0</td>\n",
" <td>1.0</td>\n",
" <td>AL</td>\n",
" <td>NaN</td>\n",
" <td>...</td>\n",
" <td>Y</td>\n",
" <td>NaN</td>\n",
" <td>1991.0</td>\n",
" <td>D/S</td>\n",
" <td>M</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>3.736796e+09</td>\n",
" <td>00296</td>\n",
" <td>F1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>15.0</td>\n",
" <td>2016.0</td>\n",
" <td>4.0</td>\n",
" <td>101.0</td>\n",
" <td>101.0</td>\n",
" <td>WAS</td>\n",
" <td>20545.0</td>\n",
" <td>1.0</td>\n",
" <td>MI</td>\n",
" <td>20691.0</td>\n",
" <td>...</td>\n",
" <td>NaN</td>\n",
" <td>M</td>\n",
" <td>1961.0</td>\n",
" <td>09302016</td>\n",
" <td>M</td>\n",
" <td>NaN</td>\n",
" <td>OS</td>\n",
" <td>6.666432e+08</td>\n",
" <td>93</td>\n",
" <td>B2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>16.0</td>\n",
" <td>2016.0</td>\n",
" <td>4.0</td>\n",
" <td>101.0</td>\n",
" <td>101.0</td>\n",
" <td>NYC</td>\n",
" <td>20545.0</td>\n",
" <td>1.0</td>\n",
" <td>MA</td>\n",
" <td>20567.0</td>\n",
" <td>...</td>\n",
" <td>NaN</td>\n",
" <td>M</td>\n",
" <td>1988.0</td>\n",
" <td>09302016</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>AA</td>\n",
" <td>9.246846e+10</td>\n",
" <td>00199</td>\n",
" <td>B2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>17.0</td>\n",
" <td>2016.0</td>\n",
" <td>4.0</td>\n",
" <td>101.0</td>\n",
" <td>101.0</td>\n",
" <td>NYC</td>\n",
" <td>20545.0</td>\n",
" <td>1.0</td>\n",
" <td>MA</td>\n",
" <td>20567.0</td>\n",
" <td>...</td>\n",
" <td>NaN</td>\n",
" <td>M</td>\n",
" <td>2012.0</td>\n",
" <td>09302016</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>AA</td>\n",
" <td>9.246846e+10</td>\n",
" <td>00199</td>\n",
" <td>B2</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>5 rows × 28 columns</p>\n",
"</div>"
],
"text/plain": [
" cicid i94yr i94mon i94cit i94res i94port arrdate i94mode i94addr \\\n",
"0 6.0 2016.0 4.0 692.0 692.0 XXX 20573.0 NaN NaN \n",
"1 7.0 2016.0 4.0 254.0 276.0 ATL 20551.0 1.0 AL \n",
"2 15.0 2016.0 4.0 101.0 101.0 WAS 20545.0 1.0 MI \n",
"3 16.0 2016.0 4.0 101.0 101.0 NYC 20545.0 1.0 MA \n",
"4 17.0 2016.0 4.0 101.0 101.0 NYC 20545.0 1.0 MA \n",
"\n",
" depdate ... entdepu matflag biryear dtaddto gender insnum \\\n",
"0 NaN ... U NaN 1979.0 10282016 NaN NaN \n",
"1 NaN ... Y NaN 1991.0 D/S M NaN \n",
"2 20691.0 ... NaN M 1961.0 09302016 M NaN \n",
"3 20567.0 ... NaN M 1988.0 09302016 NaN NaN \n",
"4 20567.0 ... NaN M 2012.0 09302016 NaN NaN \n",
"\n",
" airline admnum fltno visatype \n",
"0 NaN 1.897628e+09 NaN B2 \n",
"1 NaN 3.736796e+09 00296 F1 \n",
"2 OS 6.666432e+08 93 B2 \n",
"3 AA 9.246846e+10 00199 B2 \n",
"4 AA 9.246846e+10 00199 B2 \n",
"\n",
"[5 rows x 28 columns]"
]
},
"execution_count": 91,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_i94.head()"
]
},
{
"cell_type": "code",
"execution_count": 205,
"metadata": {},
"outputs": [],
"source": [
"fname2 = '../../data2/GlobalLandTemperaturesByCity.csv'\n",
"df_temp = pd.read_csv(fname2)"
]
},
{
"cell_type": "code",
"execution_count": 206,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(8599212, 7)"
]
},
"execution_count": 206,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_temp.shape"
]
},
{
"cell_type": "code",
"execution_count": 207,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Index(['dt', 'AverageTemperature', 'AverageTemperatureUncertainty', 'City',\n",
" 'Country', 'Latitude', 'Longitude'],\n",
" dtype='object')"
]
},
"execution_count": 207,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_temp.columns"
]
},
{
"cell_type": "code",
"execution_count": 208,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>dt</th>\n",
" <th>AverageTemperature</th>\n",
" <th>AverageTemperatureUncertainty</th>\n",
" <th>City</th>\n",
" <th>Country</th>\n",
" <th>Latitude</th>\n",
" <th>Longitude</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1743-11-01</td>\n",
" <td>6.068</td>\n",
" <td>1.737</td>\n",
" <td>Århus</td>\n",
" <td>Denmark</td>\n",
" <td>57.05N</td>\n",
" <td>10.33E</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1743-12-01</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>Århus</td>\n",
" <td>Denmark</td>\n",
" <td>57.05N</td>\n",
" <td>10.33E</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1744-01-01</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>Århus</td>\n",
" <td>Denmark</td>\n",
" <td>57.05N</td>\n",
" <td>10.33E</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1744-02-01</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>Århus</td>\n",
" <td>Denmark</td>\n",
" <td>57.05N</td>\n",
" <td>10.33E</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>1744-03-01</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>Århus</td>\n",
" <td>Denmark</td>\n",
" <td>57.05N</td>\n",
" <td>10.33E</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" dt AverageTemperature AverageTemperatureUncertainty City \\\n",
"0 1743-11-01 6.068 1.737 Århus \n",
"1 1743-12-01 NaN NaN Århus \n",
"2 1744-01-01 NaN NaN Århus \n",
"3 1744-02-01 NaN NaN Århus \n",
"4 1744-03-01 NaN NaN Århus \n",
"\n",
" Country Latitude Longitude \n",
"0 Denmark 57.05N 10.33E \n",
"1 Denmark 57.05N 10.33E \n",
"2 Denmark 57.05N 10.33E \n",
"3 Denmark 57.05N 10.33E \n",
"4 Denmark 57.05N 10.33E "
]
},
"execution_count": 208,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_temp.head()"
]
},
{
"cell_type": "code",
"execution_count": 211,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Country City \n",
"Afghanistan Baglan 2169\n",
" Gardez 2169\n",
" Jalalabad 2169\n",
" Kabul 2169\n",
" Qunduz 2169\n",
" Herat 2115\n",
" Gazni 2112\n",
" Qandahar 2053\n",
"Albania Durrës 3239\n",
" Elbasan 3239\n",
" Tirana 3239\n",
"Algeria Algiers 3129\n",
" Constantine 3129\n",
" Médéa 3129\n",
" Wahran 3129\n",
" Warqla 3129\n",
"Angola Luanda 1893\n",
" Benguela 1878\n",
" Huambo 1878\n",
" Kuito 1878\n",
" Lobito 1878\n",
" Lubango 1878\n",
"Argentina Concordia 2181\n",
" Corrientes 2181\n",
" Formosa 2181\n",
" Lambaré 2181\n",
" Posadas 2181\n",
" Resistencia 2181\n",
" Bahia Blanca 1901\n",
" Catamarca 1901\n",
" ... \n",
"Vietnam Soc Trang 2265\n",
" Vinh 2265\n",
" Vinh Long 2265\n",
" Vung Tau 2265\n",
" Cam Pha 2085\n",
" Ha Noi 2085\n",
" Hai Phong 2085\n",
" Hanoi 2085\n",
" Hong Gai 2085\n",
" Hòa Bình 2085\n",
" Nam Dinh 2085\n",
" Thanh Hóa 2085\n",
" Thái Nguyên 2085\n",
"Yemen Ibb 1797\n",
" Aden 1653\n",
"Zambia Chingola 1965\n",
" Kabwe 1965\n",
" Kitwe 1965\n",
" Luanshya 1965\n",
" Lusaka 1965\n",
" Mufulira 1965\n",
" Ndola 1965\n",
" Livingstone 1881\n",
"Zimbabwe Bulawayo 1965\n",
" Chitungwiza 1965\n",
" Gweru 1965\n",
" Harare 1965\n",
" Kadoma 1965\n",
" Kwekwe 1965\n",
" Mutare 1965\n",
"Name: City, Length: 3490, dtype: int64"
]
},
"execution_count": 211,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_temp.groupby('Country')['City'].value_counts()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Read Data Directly into Spark\n",
"1. I94 Data read into pyspark df\n",
"2. Temperature Data read into pyspark df\n",
"3. I94 Correct Label File into pyspark df"
]
},
{
"cell_type": "code",
"execution_count": 100,
"metadata": {},
"outputs": [],
"source": [
"\n",
"from pyspark.sql import SparkSession\n",
"spark = SparkSession.builder.\\\n",
"config(\"spark.jars.packages\",\"saurfang:spark-sas7bdat:2.0.0-s_2.11\")\\\n",
".enableHiveSupport().getOrCreate()\n",
"df_spark =spark.read.format('com.github.saurfang.sas.spark').load('../../data/18-83510-I94-Data-2016/i94_apr16_sub.sas7bdat')\n"
]
},
{
"cell_type": "code",
"execution_count": 101,
"metadata": {
"collapsed": true,
"jupyter": {
"outputs_hidden": true
}
},
"outputs": [
{
"ename": "AnalysisException",
"evalue": "'path file:/home/workspace/sas_data already exists.;'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mPy4JJavaError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/opt/spark-2.4.3-bin-hadoop2.7/python/pyspark/sql/utils.py\u001b[0m in \u001b[0;36mdeco\u001b[0;34m(*a, **kw)\u001b[0m\n\u001b[1;32m 62\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 63\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkw\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 64\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mpy4j\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mprotocol\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mPy4JJavaError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/opt/spark-2.4.3-bin-hadoop2.7/python/lib/py4j-0.10.7-src.zip/py4j/protocol.py\u001b[0m in \u001b[0;36mget_return_value\u001b[0;34m(answer, gateway_client, target_id, name)\u001b[0m\n\u001b[1;32m 327\u001b[0m \u001b[0;34m\"An error occurred while calling {0}{1}{2}.\\n\"\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 328\u001b[0;31m format(target_id, \".\", name), value)\n\u001b[0m\u001b[1;32m 329\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mPy4JJavaError\u001b[0m: An error occurred while calling o596.parquet.\n: org.apache.spark.sql.AnalysisException: path file:/home/workspace/sas_data already exists.;\n\tat org.apache.spark.sql.execution.datasources.InsertIntoHadoopFsRelationCommand.run(InsertIntoHadoopFsRelationCommand.scala:114)\n\tat org.apache.spark.sql.execution.command.DataWritingCommandExec.sideEffectResult$lzycompute(commands.scala:104)\n\tat org.apache.spark.sql.execution.command.DataWritingCommandExec.sideEffectResult(commands.scala:102)\n\tat org.apache.spark.sql.execution.command.DataWritingCommandExec.doExecute(commands.scala:122)\n\tat org.apache.spark.sql.execution.SparkPlan$$anonfun$execute$1.apply(SparkPlan.scala:131)\n\tat org.apache.spark.sql.execution.SparkPlan$$anonfun$execute$1.apply(SparkPlan.scala:127)\n\tat org.apache.spark.sql.execution.SparkPlan$$anonfun$executeQuery$1.apply(SparkPlan.scala:155)\n\tat org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151)\n\tat org.apache.spark.sql.execution.SparkPlan.executeQuery(SparkPlan.scala:152)\n\tat org.apache.spark.sql.execution.SparkPlan.execute(SparkPlan.scala:127)\n\tat org.apache.spark.sql.execution.QueryExecution.toRdd$lzycompute(QueryExecution.scala:80)\n\tat org.apache.spark.sql.execution.QueryExecution.toRdd(QueryExecution.scala:80)\n\tat org.apache.spark.sql.DataFrameWriter$$anonfun$runCommand$1.apply(DataFrameWriter.scala:676)\n\tat org.apache.spark.sql.DataFrameWriter$$anonfun$runCommand$1.apply(DataFrameWriter.scala:676)\n\tat org.apache.spark.sql.execution.SQLExecution$$anonfun$withNewExecutionId$1.apply(SQLExecution.scala:78)\n\tat org.apache.spark.sql.execution.SQLExecution$.withSQLConfPropagated(SQLExecution.scala:125)\n\tat org.apache.spark.sql.execution.SQLExecution$.withNewExecutionId(SQLExecution.scala:73)\n\tat org.apache.spark.sql.DataFrameWriter.runCommand(DataFrameWriter.scala:676)\n\tat org.apache.spark.sql.DataFrameWriter.saveToV1Source(DataFrameWriter.scala:285)\n\tat org.apache.spark.sql.DataFrameWriter.save(DataFrameWriter.scala:271)\n\tat org.apache.spark.sql.DataFrameWriter.save(DataFrameWriter.scala:229)\n\tat org.apache.spark.sql.DataFrameWriter.parquet(DataFrameWriter.scala:566)\n\tat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n\tat sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\n\tat sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\n\tat java.lang.reflect.Method.invoke(Method.java:498)\n\tat py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244)\n\tat py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)\n\tat py4j.Gateway.invoke(Gateway.java:282)\n\tat py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)\n\tat py4j.commands.CallCommand.execute(CallCommand.java:79)\n\tat py4j.GatewayConnection.run(GatewayConnection.java:238)\n\tat java.lang.Thread.run(Thread.java:748)\n",
"\nDuring handling of the above exception, another exception occurred:\n",
"\u001b[0;31mAnalysisException\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-101-9814a8024ab4>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m#write to parquet\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mdf_spark\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwrite\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mparquet\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"sas_data\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mdf_spark\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mspark\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mread\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mparquet\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"sas_data\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/opt/spark-2.4.3-bin-hadoop2.7/python/pyspark/sql/readwriter.py\u001b[0m in \u001b[0;36mparquet\u001b[0;34m(self, path, mode, partitionBy, compression)\u001b[0m\n\u001b[1;32m 837\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpartitionBy\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpartitionBy\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 838\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_set_opts\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcompression\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcompression\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 839\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_jwrite\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mparquet\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpath\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 840\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 841\u001b[0m \u001b[0;34m@\u001b[0m\u001b[0msince\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1.6\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/opt/spark-2.4.3-bin-hadoop2.7/python/lib/py4j-0.10.7-src.zip/py4j/java_gateway.py\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(self, *args)\u001b[0m\n\u001b[1;32m 1255\u001b[0m \u001b[0manswer\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgateway_client\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msend_command\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcommand\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1256\u001b[0m return_value = get_return_value(\n\u001b[0;32m-> 1257\u001b[0;31m answer, self.gateway_client, self.target_id, self.name)\n\u001b[0m\u001b[1;32m 1258\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1259\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mtemp_arg\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mtemp_args\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/opt/spark-2.4.3-bin-hadoop2.7/python/pyspark/sql/utils.py\u001b[0m in \u001b[0;36mdeco\u001b[0;34m(*a, **kw)\u001b[0m\n\u001b[1;32m 67\u001b[0m e.java_exception.getStackTrace()))\n\u001b[1;32m 68\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstartswith\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'org.apache.spark.sql.AnalysisException: '\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 69\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mAnalysisException\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msplit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m': '\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstackTrace\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 70\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstartswith\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'org.apache.spark.sql.catalyst.analysis'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 71\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mAnalysisException\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msplit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m': '\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstackTrace\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mAnalysisException\u001b[0m: 'path file:/home/workspace/sas_data already exists.;'"
]
}
],
"source": [
"#write to parquet\n",
"df_spark.write.parquet(\"sas_data\")\n",
"df_spark=spark.read.parquet(\"sas_data\")"
]
},
{
"cell_type": "code",
"execution_count": 102,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"root\n",
" |-- cicid: double (nullable = true)\n",
" |-- i94yr: double (nullable = true)\n",
" |-- i94mon: double (nullable = true)\n",
" |-- i94cit: double (nullable = true)\n",
" |-- i94res: double (nullable = true)\n",
" |-- i94port: string (nullable = true)\n",
" |-- arrdate: double (nullable = true)\n",
" |-- i94mode: double (nullable = true)\n",
" |-- i94addr: string (nullable = true)\n",
" |-- depdate: double (nullable = true)\n",
" |-- i94bir: double (nullable = true)\n",
" |-- i94visa: double (nullable = true)\n",
" |-- count: double (nullable = true)\n",
" |-- dtadfile: string (nullable = true)\n",
" |-- visapost: string (nullable = true)\n",
" |-- occup: string (nullable = true)\n",
" |-- entdepa: string (nullable = true)\n",
" |-- entdepd: string (nullable = true)\n",
" |-- entdepu: string (nullable = true)\n",
" |-- matflag: string (nullable = true)\n",
" |-- biryear: double (nullable = true)\n",
" |-- dtaddto: string (nullable = true)\n",
" |-- gender: string (nullable = true)\n",
" |-- insnum: string (nullable = true)\n",
" |-- airline: string (nullable = true)\n",
" |-- admnum: double (nullable = true)\n",
" |-- fltno: string (nullable = true)\n",
" |-- visatype: string (nullable = true)\n",
"\n"
]
}
],
"source": [
"df_i94_spark.printSchema()"
]
},
{
"cell_type": "code",
"execution_count": 103,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"+-------+------------------+\n",
"|summary| i94cit|\n",
"+-------+------------------+\n",
"| count| 3096313|\n",
"| mean| 304.9069344733559|\n",
"| stddev|210.02688853063327|\n",
"| min| 101.0|\n",
"| max| 999.0|\n",
"+-------+------------------+\n",
"\n"
]
}
],
"source": [
"df_i94_spark.describe('i94cit').show()"
]
},
{
"cell_type": "code",
"execution_count": 104,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"243"
]
},
"execution_count": 104,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_i94_spark.select('i94cit').distinct().count()"
]
},
{
"cell_type": "code",
"execution_count": 105,
"metadata": {},
"outputs": [],
"source": [
"df_temp_spark = (spark.read.format(\"csv\").options(header=\"true\")\n",
" .load(fname2))"
]
},
{
"cell_type": "code",
"execution_count": 106,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"root\n",
" |-- dt: string (nullable = true)\n",
" |-- AverageTemperature: string (nullable = true)\n",
" |-- AverageTemperatureUncertainty: string (nullable = true)\n",
" |-- City: string (nullable = true)\n",
" |-- Country: string (nullable = true)\n",
" |-- Latitude: string (nullable = true)\n",
" |-- Longitude: string (nullable = true)\n",
"\n"
]
}
],
"source": [
"df_temp_spark.printSchema()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 2: Explore and Assess the Data\n",
"#### Explore the Data \n",
"All valid entries for destination city codes are stored in `I94_SAS_Labels_Description_Valid.txt`. All cities from the I94 immigration data and temperature data is filtered through the valid cities list. Other cleaning steps are listed below. \n",
"\n",
"#### Cleaning Steps\n",
"Document steps necessary to clean the data. Data quality issues include, \n",
"\n",
"1. Remove Missing values (NaN)\n",
"2. Duplicate data\n",
"3. Invalid Data Values (SAS Label Descriptions)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 238,
"metadata": {},
"outputs": [],
"source": [
"# format txt document into clean, usable dictionary/list\n",
"valid_port = {}\n",
"list_of_dicts = []\n",
"\n",
"with open('I94_SAS_Labels_Description_Valid.txt') as file:\n",
" for line in file:\n",
" line = line.strip()\n",
" key, val = line.split('=')\n",
" val_list = val.split(',')\n",
" valid_port[key] = val_list\n",
" for city in val_list:\n",
" city = city.strip()\n",
" valid_port_dict = {}\n",
" valid_port_dict['i94port'] = key\n",
" valid_port_dict['portcity'] = city\n",
" list_of_dicts.append(valid_port_dict)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 239,
"metadata": {},
"outputs": [],
"source": [
"myJson = sc.parallelize(list_of_dicts)\n",
"port_df = sqlContext.read.json(myJson)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Clean and Filter I94 data"
]
},
{
"cell_type": "code",
"execution_count": 157,
"metadata": {},
"outputs": [],
"source": [
"# I94 data (df_i94_spark) \n",
"# df_i94_spark_test = df_i94_spark.limit(1000) # always start practice on smaller dataset\n",
"\n",
"# drop any duplicates\n",
"df_i94_spark=df_i94_spark.dropDuplicates()\n",
"\n",
"# lowercase all columns to standardize text format\n",
"df_i94_spark = df_i94_spark.toDF(*[c.lower() for c in df_i94_spark.columns])\n",
"\n",
"df_i94_spark = df_i94_spark.filter(df_i94_spark.i94port.isin(list(valid_port.keys())))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Clean and Filter Immigration data"
]
},
{
"cell_type": "code",
"execution_count": 241,
"metadata": {},
"outputs": [
{
"ename": "AttributeError",
"evalue": "'DataFrame' object has no attribute 'AverageTemperature'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-241-1c493289e6c2>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;31m# keep non-null values for averagetemperature and city\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mdf_temp_spark\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdf_temp_spark\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfilter\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdf_temp_spark\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mAverageTemperature\u001b[0m \u001b[0;34m!=\u001b[0m \u001b[0;34m'NaN'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 6\u001b[0m \u001b[0mdf_temp_spark\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdf_temp_spark\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfilter\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdf_temp_spark\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mCity\u001b[0m \u001b[0;34m!=\u001b[0m \u001b[0;34m'null'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/opt/spark-2.4.3-bin-hadoop2.7/python/pyspark/sql/dataframe.py\u001b[0m in \u001b[0;36m__getattr__\u001b[0;34m(self, name)\u001b[0m\n\u001b[1;32m 1298\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mname\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcolumns\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1299\u001b[0m raise AttributeError(\n\u001b[0;32m-> 1300\u001b[0;31m \"'%s' object has no attribute '%s'\" % (self.__class__.__name__, name))\n\u001b[0m\u001b[1;32m 1301\u001b[0m \u001b[0mjc\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_jdf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mapply\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1302\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mColumn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mjc\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mAttributeError\u001b[0m: 'DataFrame' object has no attribute 'AverageTemperature'"
]
}
],
"source": [
"# City Demoographics Data (df_temp_spark)\n",
"# df_temp_spark_test = df_temp_spark.limit(1000) # always start practice on smaller dataset\n",
"\n",
"# keep non-null values for averagetemperature and city\n",
"df_temp_spark = df_temp_spark.filter(df_temp_spark.AverageTemperature != 'NaN')\n",
"df_temp_spark = df_temp_spark.filter(df_temp_spark.City != 'null')\n",
"\n",
"# drop duplicates\n",
"df_temp_spark = df_temp_spark.dropDuplicates(['City', 'Country'])\n",
"\n",
"# lowercase all columns to standardize text format\n",
"df_temp_spark = df_temp_spark.toDF(*[c.lower() for c in df_temp_spark.columns])\n",
"\n",
"# use inner join to filter down to cities with ports\n",
"temp_final = df_temp_spark.join(port_df, df_temp_spark.city==port_df.portcity)"
]
},
{
"cell_type": "code",
"execution_count": 242,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['dt',\n",
" 'averagetemperature',\n",
" 'averagetemperatureuncertainty',\n",
" 'city',\n",
" 'country',\n",
" 'latitude',\n",
" 'longitude',\n",
" 'i94port',\n",
" 'portcity']"
]
},
"execution_count": 242,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temp_final.columns"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 3: Define the Data Model\n",
"#### 3.1 Conceptual Data Model\n",
"The data model is to create a fact table (described below) from the combination of the Immigration Data and the Temperature linked by city after being cleaned and filtered above. Selecting only the columns that are important.\n",
"\n",
"Fact Table: I94 Immigration Data and Temperature Data\n",
"* i94yr = 4 digit year\n",
"* i94mon = numeric month\n",
"* i94cit = 3 digit code of origin city\n",
"* i94port = 3 character code of destination city\n",
"* arrdate = arrival date\n",
"* i94mode = 1 digit travel code\n",
"* depdate = departure date\n",
"* i94visa = reason for immigration\n",
"* AverageTemperature = average temp"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 3.2 Mapping Out Data Pipelines\n",
"List of steps necessary to pipeline the data into data model\n",
"1. Import all data and convert into same dataformat (Step 1 above)\n",
"2. Clean and Filter data to for every month in folder (Step 2 above)\n",
"3. Create Immigrant and Temperature dimension tables by selecting specific important columns\n",
"4. Create above Fact Table by joining two dimension gtables on filtered i94port column"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 4: Run Pipelines to Model the Data \n",
"#### 4.1 Create the data model\n",
"Build the data pipelines to create the data model."
]
},
{
"cell_type": "code",
"execution_count": 187,
"metadata": {},
"outputs": [],
"source": [
"# Clean Immigration Data\n",
"# df_i94_spark\n",
"\n",
"# Extract columns for immigration dimension table\n",
"immig_table = df_i94_spark.select([\"i94yr\", \"i94mon\", \"i94cit\", \"i94port\", \"arrdate\", \"i94mode\", \"depdate\", \"i94visa\"])\n",
"\n",
"# Write dimension table\n",
"immig_table.write.mode(\"append\").partitionBy(\"i94port\").parquet(\"/results/immigration.parquet\")\n"
]
},
{
"cell_type": "code",
"execution_count": 243,
"metadata": {},
"outputs": [],
"source": [
"# Clean Temperature Data\n",
"# df_temp_spark\n",
"\n",
"# Extract columns for temp dimension table\n",