-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_tables.py
executable file
·175 lines (163 loc) · 6.72 KB
/
process_tables.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
#!/usr/bin/python3
from pyspark.sql import functions as F
from pyspark.sql import types as T
from pyspark.sql.functions import monotonically_increasing_id
import sys
def set_df_columns_nullable(spark, df, column_list, nullable=False):
for struct_field in df.schema:
if struct_field.name in column_list:
struct_field.nullable = nullable
df_mod = spark.createDataFrame(df.rdd, df.schema)
return df_mod
def write_parquet(table, parquet_path):
"""
write parquet files
"""
try:
table.write.parquet(parquet_path, mode = 'overwrite')
except Exception as e:
print("Unexpected error: %s" % e)
def check_parquet(spark, parquet_path):
# CHECK DATA
n=0
contains_nulls = False
try:
check = spark.read.parquet(parquet_path)
check = check.select(check.columns[n])
for c in check.columns:
if not check.where(F.col(c).isNull()).limit(1):
print(f"col(c)")
contains_nulls = True
print(f" CHECK {parquet_path} FAILLLLL")
nb_check = check.count()
print(f" AND {nb_check} AFTER LOADING FILE")
null_check = check.where(F.col(c).isNull()).count()
print(f" AND {null_check}")
sys.exit()
else:
print(f" CHECK NUUL VALUE IN {check.columns[n]}")
print(f" CHECK {parquet_path} SUCESS")
except Exception as e:
print(f"{parquet_path}")
print("Unexpected error in check {parquet_path} : %s" % e)
sys.exit()
return
def create_usairport_table(spark, df_clean_airport_code, df_clean_global_airports, output_parquet):
"""
create airport table and saved in parquet file
# output_parquet = '../../output/'
"""
try:
tac = df_clean_airport_code.alias('tac')
tga = df_clean_global_airports.alias('tga')
df_join = tac.join(tga, ((tac.iata_code == tga.iata_code) | (tac.airport_name == tga.airport_name) | (tac.city_name == tga.city_name)), how='left')
dim_airport_us = df_join \
.filter('tga != tga.iata_code ""' and 'state_id !=""') \
.selectExpr("airport_id",
"ident",
"tga.iata_code",
"tac.airport_name",
"tac.city_name",
"state_id") \
.sort('ident') \
.dropDuplicates(['iata_code'])
#dim_airport_us.printSchema()
#print(dim_airport_us.count())
dim_airport_us.collect()
parquet_path = output_parquet + 'usairport_table'
#dim_airport_us.toPandas().to_csv(output_parquet+"usairport_table.csv", header=True)
write_parquet(dim_airport_us, parquet_path)
column = 'airport_id'
check_parquet(spark, parquet_path)
return(dim_airport_us)
except Exception as e:
print("Unexpected error: %s" % e)
sys.exit()
def create_country_table(spark, df_clean_iso_country, df_clean_temperature, output_parquet):
"""
# create country table
# output_parquet = '../../output/'
"""
try:
tic = df_clean_iso_country.alias('tic')
tt = df_clean_temperature.alias('tt')
df_join = tic.join(tt, (tic.country_name == tt.country), how='left')
dim_country = df_join \
.filter('country_num != ""' and 'country_iso3 != ""') \
.drop_duplicates(subset = ['country_name']) \
.orderBy('country_name') \
.drop('country') \
.dropDuplicates(['country_num'])
#dim_country.printSchema()
#dim_country.show(2)
dim_country.collect()
#dim_country.toPandas().to_csv(output_parquet+"country_table.csv", header=True)
parquet_path = output_parquet + 'country_table'
write_parquet(dim_country, parquet_path)
check_parquet(spark, parquet_path)
return(dim_country)
except Exception as e:
print("Unexpected error: %s" % e)
sys.exit()
def create_indicator_table(spark, df_clean_indicator_dev, output_parquet):
"""
# create indicator table
# output_parquet = '../../output/'
"""
try:
dim_indicator = df_clean_indicator_dev \
.filter('country_code !=""') \
.dropDuplicates() \
.orderBy('country_name') \
.select('country_code', 'indicator_group', 'avg_2015' )
#dim_indicator.printSchema()
#dim_indicator.show(2)
dim_indicator.collect()
#dim_indicator.toPandas().to_csv(output_parquet+"indicator_table.csv", header=True)
parquet_path = output_parquet + 'indicator_table'
write_parquet(dim_indicator, parquet_path)
check_parquet(spark, parquet_path)
return( dim_indicator)
except Exception as e:
print("Unexpected error: %s" % e)
sys.exit()
def create_demography_table(spark, df_clean_demograph, output_parquet):
# create demography table per ethnic per state
try:
dim_demography = df_clean_demograph \
.filter('state_id !=""') \
.groupBy( 'state_id', 'ethnic') \
.agg((F.avg('ethnic_count').cast(T.DecimalType(22, 2))).alias('avg_ethnic')) \
.dropDuplicates() \
.orderBy("state_id")
#dim_demography.printSchema()
#print(dim_airport_us.count())
#dim_demography.show(2)
dim_demography.collect()
#dim_demography.toPandas().to_csv(output_parquet+"demograph_table.csv", header=True)
parquet_path = output_parquet + 'demograph_table'
write_parquet(dim_demography, parquet_path)
check_parquet(spark, parquet_path)
return(dim_demography)
except Exception as e:
print("Unexpected error: %s" % e)
sys.exit()
def create_fact_immigration_table(spark, df_clean_immigration, output_parquet ):
"""
create facts tables
"""
try:
fact_student = df_clean_immigration \
.filter('id_i94 !=""') \
.dropDuplicates(['id_i94'])
#fact_student.printSchema()
fact_student.count()
#fact_student.toPandas().to_csv(output_parquet+"fact_immigration.csv", header=True)
fact_student.collect()
parquet_path = output_parquet + 'fact_immigration'
write_parquet(fact_student, parquet_path)
check_parquet(spark, parquet_path)
return(fact_student)
except Exception as e:
print("Unexpected error: %s" % e)
sys.exit()