-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspark_etl_tests.py
211 lines (155 loc) · 6.95 KB
/
spark_etl_tests.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
from pyspark.sql import SparkSession, functions, types
import sys
import time
NUM_START_ROWS = 2500
NUM_EXECUTIONS_PER_TEST = 3
NUM_DSIZE_DOUBLINGS = 12
def main(input1, input2, output):
spark_results = []
air_schema = types.StructType([
types.StructField('DATE_PST', types.StringType()),
types.StructField('DATE', types.DateType()),
types.StructField('TIME', types.StringType()),
types.StructField('STATION_NAME', types.StringType()),
types.StructField('STATION_NAME_FULL', types.StringType()),
types.StructField('EMS_ID', types.StringType()),
types.StructField('NAPS_ID', types.IntegerType()),
types.StructField('RAW_VALUE', types.FloatType()),
types.StructField('ROUNDED_VALUE', types.FloatType()),
types.StructField('UNIT', types.StringType()),
types.StructField('INSTRUMENT', types.StringType()),
types.StructField('PARAMETER', types.StringType()),
types.StructField('OWNER', types.StringType()),
types.StructField('REGION', types.StringType()),
])
station_schema = types.StructType([
types.StructField('STATION_NAME_FULL', types.StringType()),
types.StructField('STATION_NAME', types.StringType()),
types.StructField('EMS_ID', types.StringType()),
types.StructField('SERIAL', types.IntegerType()),
types.StructField('ADDRESS', types.StringType()),
types.StructField('CITY', types.StringType()),
types.StructField('LAT', types.FloatType()),
types.StructField('LONG', types.FloatType()),
types.StructField('ELEVATION', types.IntegerType()),
types.StructField('STATUS_DESCRIPTION', types.StringType()),
types.StructField('OWNER', types.StringType()),
types.StructField('REGION', types.StringType()),
types.StructField('STATUS', types.StringType()),
types.StructField('OPENED', types.TimestampType()),
types.StructField('CLOSED', types.StringType()),
types.StructField('NAPS_ID', types.IntegerType()),
])
station_co_df = spark.read.csv(f"{input1}", header=True, schema=station_schema)
station_co_df = functions.broadcast(station_co_df)
for i in range(0, NUM_DSIZE_DOUBLINGS):
print('Test', i, '***************************************************************************************************************')
co_subset = spark.read.csv(f"{input2}/test_{i}", schema=air_schema)
# ******************************************************************************
# MEAN TEST
test = {'Test': 'Mean', 'Test Number': i}
# Starting timer
t0 = time.time()
for j in range(0, NUM_EXECUTIONS_PER_TEST):
# Executing the Mean test
co_subset.select(functions.mean("RAW_VALUE")).show()
# Stopping clock
t1 = time.time()
# Recording Results
total_time = t1 - t0
avg_time = total_time / NUM_EXECUTIONS_PER_TEST
test['Total'] = total_time
test['Average'] = avg_time
spark_results.append(test)
# ******************************************************************************
# SORT TEST
test = {'Test': 'Sort', 'Test Number': i}
# Starting timer
t0 = time.time()
for j in range(0, NUM_EXECUTIONS_PER_TEST):
# Executing the Sort test
co_subset.sort("RAW_VALUE", ascending=True).show(5)
# Stopping clock
t1 = time.time()
# Recording Results
total_time = t1 - t0
avg_time = total_time / NUM_EXECUTIONS_PER_TEST
test['Total'] = total_time
test['Average'] = avg_time
spark_results.append(test)
# ******************************************************************************
# MERGE TEST
test = {'Test': 'Merge', 'Test Number': i}
# Starting timer
t0 = time.time()
for j in range(0, NUM_EXECUTIONS_PER_TEST):
# Executing the Merge test
co_subset_merged = co_subset.join(station_co_df, how='left', on='STATION_NAME')
co_subset_merged.show(5)
# Stopping clock
t1 = time.time()
# Recording Results
total_time = t1 - t0
avg_time = total_time / NUM_EXECUTIONS_PER_TEST
test['Total'] = total_time
test['Average'] = avg_time
spark_results.append(test)
# ******************************************************************************
# FILTER TEST
test = {'Test': 'Filter', 'Test Number': i}
# Starting timer
t0 = time.time()
for j in range(0, NUM_EXECUTIONS_PER_TEST):
# Executing the Filter test
co_subset.filter(co_subset['STATION_NAME'] == 'Victoria Topaz').show(5)
# Stopping clock
t1 = time.time()
# Recording Results
total_time = t1 - t0
avg_time = total_time / NUM_EXECUTIONS_PER_TEST
test['Total'] = total_time
test['Average'] = avg_time
spark_results.append(test)
# ******************************************************************************
# ALL TEST
test = {'Test': 'All', 'Test Number': i}
# Starting timer
t0 = time.time()
for j in range(0, NUM_EXECUTIONS_PER_TEST):
# Executing the Mean test
co_subset.select(functions.mean("RAW_VALUE")).show()
# Executing the Sort test
co_subset.sort("RAW_VALUE", ascending=True).show(5)
# Executing the Merge test
co_subset_merged = co_subset.join(station_co_df, how='left', on='STATION_NAME')
co_subset_merged.show(5)
# Executing the Filter test
co_subset.filter(co_subset['STATION_NAME'] == 'Victoria Topaz').show(5)
# Stopping clock
t1 = time.time()
# Recording Results
total_time = t1 - t0
avg_time = total_time / NUM_EXECUTIONS_PER_TEST
test['Total'] = total_time
test['Average'] = avg_time
spark_results.append(test)
# Keeping this as a pandas df to maintain order
# pd.DataFrame(spark_results).to_csv('AWS Results/spark_etl_results.csv')
# pd.DataFrame(spark_results).to_csv(f"{output}")
schema = types.StructType([
types.StructField('Test', types.StringType()),
types.StructField('Test Number', types.IntegerType()),
types.StructField('Total', types.DoubleType()),
types.StructField('Average', types.DoubleType())
])
results_rdd = sc.parallelize(spark_results, numSlices=1)
spark.createDataFrame(data=results_rdd, schema=schema).write.csv(output, header=True)
if __name__ == '__main__':
input1 = sys.argv[1]
input2 = sys.argv[2]
output = sys.argv[3]
spark = SparkSession.builder.appName('spark etl tests').getOrCreate()
assert spark.version >= '3.0' # make sure we have Spark 3.0+
spark.sparkContext.setLogLevel('WARN')
sc = spark.sparkContext
main(input1, input2, output)