-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema_conversion.py
204 lines (177 loc) · 9.75 KB
/
schema_conversion.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
'''
________ ______ ______ _ ____ __
/_ __/ / / ____/ /_ __/____(_)___ / __ \____ _/ /_____ _
/ / / / / / / / / ___/ / __ \ / / / / __ `/ __/ __ `/
/ / / /___/ /___ / / / / / / /_/ / / /_/ / /_/ / /_/ /_/ /
/_/ /_____/\____/ /_/ /_/ /_/ .___/ /_____/\__,_/\__/\__,_/
/_/
Authors: Willi Menapace <willi.menapace@studenti.unitn.it>
Luca Zanella <luca.zanella-3@studenti.unitn.it>
Daniele Giuliani <daniele.giuliani@studenti.unitn.it>
Utilities for conversion of the original dataset in common format
'''
import pyspark
from pyspark.sql.functions import col
from pyspark.sql.functions import lit
from schema import *
from pyspark.sql.types import *
def payment_type_string_2_id(payment_type_string):
if payment_type_string is None:
return None
payment_type_string = payment_type_string.lower()
if payment_type_string == "crd":
return 1
elif payment_type_string == "csh":
return 2
elif payment_type_string == "unk":
return 5
elif payment_type_string == "noc":
return 3
elif payment_type_string == "dis":
return 4
else:
return None
def vendor_string_2_id(vendor_string):
if vendor_string is None:
return None
vendor_string = vendor_string.lower()
if vendor_string == "cmt":
return 1
elif vendor_string == "vts":
return 2
elif vendor_string == "dds":
return 3
else:
return None
def v1_yellow_to_common(dataset, conversion_udf, vendor_conversion_udf, payment_type_conversion_udf):
dataset = dataset.select(
vendor_conversion_udf("VendorID".lower()).alias(vendor_id_property),
lit("yellow").alias(taxi_company_property),
col("lpep_pickup_datetime".lower()).alias(pickup_datetime_property),
col("lpep_dropoff_datetime".lower()).alias(dropoff_datetime_property),
conversion_udf("pickup_latitude", "pickup_longitude").alias(pickup_location_id_property),
conversion_udf("dropoff_latitude", "dropoff_longitude").alias(dropoff_location_id_property),
col("Passenger_count".lower()).alias(passenger_count_property),
col("Trip_distance".lower()).alias(trip_distance_property),
col("Store_and_fwd_flag".lower()).alias(store_and_forward_flag_property),
col("RateCodeID".lower()).alias(ratecode_id_property),
col("Fare_amount".lower()).alias(fare_amount_property),
col("Tolls_amount".lower()).alias(tolls_amount_property),
col("Total_amount".lower()).alias(total_amount_property),
col("MTA_tax".lower()).alias(mta_tax_property),
col("improvement_surcharge".lower()).alias(improvement_surcharge_property),
lit(0).alias(extra_property),
col("Tip_amount".lower()).alias(tip_amount_property),
payment_type_conversion_udf("Payment_type".lower()).alias(payment_type_property)
)
return dataset
def v2_yellow_to_common(dataset, conversion_udf, vendor_conversion_udf, payment_type_conversion_udf):
dataset = dataset.select(
col("VendorID".lower()).alias(vendor_id_property),
lit("yellow").alias(taxi_company_property),
col("lpep_pickup_datetime".lower()).alias(pickup_datetime_property),
col("lpep_dropoff_datetime".lower()).alias(dropoff_datetime_property),
conversion_udf("pickup_latitude", "pickup_longitude").alias(pickup_location_id_property),
conversion_udf("dropoff_latitude", "dropoff_longitude").alias(dropoff_location_id_property),
col("Passenger_count".lower()).alias(passenger_count_property),
col("Trip_distance".lower()).alias(trip_distance_property),
col("Store_and_fwd_flag".lower()).alias(store_and_forward_flag_property),
col("RateCodeID".lower()).alias(ratecode_id_property),
col("Fare_amount".lower()).alias(fare_amount_property),
col("Tolls_amount".lower()).alias(tolls_amount_property),
col("Total_amount".lower()).alias(total_amount_property),
col("MTA_tax".lower()).alias(mta_tax_property),
col("improvement_surcharge".lower()).alias(improvement_surcharge_property),
col("Extra".lower()).alias(extra_property),
col("Tip_amount".lower()).alias(tip_amount_property),
col("Payment_type".lower()).alias(payment_type_property)
)
return dataset
def v3_yellow_to_common(dataset, conversion_udf, vendor_conversion_udf, payment_type_conversion_udf):
dataset = dataset.select(
col("VendorID".lower()).alias(vendor_id_property),
lit("yellow").alias(taxi_company_property),
col("lpep_pickup_datetime".lower()).alias(pickup_datetime_property),
col("lpep_dropoff_datetime".lower()).alias(dropoff_datetime_property),
col("PULocationID".lower()).alias(pickup_location_id_property),
col("DOLocationID".lower()).alias(dropoff_location_id_property),
col("Passenger_count".lower()).alias(passenger_count_property),
col("Trip_distance".lower()).alias(trip_distance_property),
col("Store_and_fwd_flag".lower()).alias(store_and_forward_flag_property),
col("RateCodeID".lower()).alias(ratecode_id_property),
col("Fare_amount".lower()).alias(fare_amount_property),
col("Tolls_amount".lower()).alias(tolls_amount_property),
col("Total_amount".lower()).alias(total_amount_property),
col("MTA_tax".lower()).alias(mta_tax_property),
col("improvement_surcharge".lower()).alias(improvement_surcharge_property),
col("Extra".lower()).alias(extra_property),
col("Tip_amount".lower()).alias(tip_amount_property),
col("Payment_type".lower()).alias(payment_type_property)
)
return dataset
def v1_green_to_common(dataset, conversion_udf, vendor_conversion_udf, payment_type_conversion_udf):
dataset = dataset.select(
col("VendorID".lower()).alias(vendor_id_property),
lit("green").alias(taxi_company_property),
col("lpep_pickup_datetime".lower()).alias(pickup_datetime_property),
col("lpep_dropoff_datetime".lower()).alias(dropoff_datetime_property),
conversion_udf("Pickup_latitude", "Pickup_longitude").alias(pickup_location_id_property),
conversion_udf("Dropoff_latitude", "Dropoff_longitude").alias(dropoff_location_id_property),
col("Passenger_count".lower()).alias(passenger_count_property),
col("Trip_distance".lower()).alias(trip_distance_property),
col("Store_and_fwd_flag".lower()).alias(store_and_forward_flag_property),
col("RateCodeID".lower()).alias(ratecode_id_property),
col("Fare_amount".lower()).alias(fare_amount_property),
col("Tolls_amount".lower()).alias(tolls_amount_property),
col("Total_amount".lower()).alias(total_amount_property),
col("MTA_tax".lower()).alias(mta_tax_property),
lit(0).alias(improvement_surcharge_property),
col("Extra".lower()).alias(extra_property),
col("Tip_amount".lower()).alias(tip_amount_property),
col("Payment_type".lower()).alias(payment_type_property)
)
return dataset
def v2_green_to_common(dataset, conversion_udf, vendor_conversion_udf, payment_type_conversion_udf):
dataset = dataset.select(
col("VendorID".lower()).alias(vendor_id_property),
lit("green").alias(taxi_company_property),
col("lpep_pickup_datetime".lower()).alias(pickup_datetime_property),
col("lpep_dropoff_datetime".lower()).alias(dropoff_datetime_property),
conversion_udf("Pickup_latitude", "Pickup_longitude").alias(pickup_location_id_property),
conversion_udf("Dropoff_latitude", "Dropoff_longitude").alias(dropoff_location_id_property),
col("Passenger_count".lower()).alias(passenger_count_property),
col("Trip_distance".lower()).alias(trip_distance_property),
col("Store_and_fwd_flag".lower()).alias(store_and_forward_flag_property),
col("RateCodeID".lower()).alias(ratecode_id_property),
col("Fare_amount".lower()).alias(fare_amount_property),
col("Tolls_amount".lower()).alias(tolls_amount_property),
col("Total_amount".lower()).alias(total_amount_property),
col("MTA_tax".lower()).alias(mta_tax_property),
col("improvement_surcharge".lower()).alias(improvement_surcharge_property),
col("Extra".lower()).alias(extra_property),
col("Tip_amount".lower()).alias(tip_amount_property),
col("Payment_type".lower()).alias(payment_type_property)
)
return dataset
def v3_green_to_common(dataset, conversion_udf, vendor_conversion_udf, payment_type_conversion_udf):
dataset = dataset.select(
col("VendorID".lower()).alias(vendor_id_property),
lit("green").alias(taxi_company_property),
col("lpep_pickup_datetime".lower()).alias(pickup_datetime_property),
col("lpep_dropoff_datetime".lower()).alias(dropoff_datetime_property),
col("PULocationID".lower()).alias(pickup_location_id_property),
col("DOLocationID".lower()).alias(dropoff_location_id_property),
col("Passenger_count".lower()).alias(passenger_count_property),
col("Trip_distance".lower()).alias(trip_distance_property),
col("Store_and_fwd_flag".lower()).alias(store_and_forward_flag_property),
col("RateCodeID".lower()).alias(ratecode_id_property),
col("Fare_amount".lower()).alias(fare_amount_property),
col("Tolls_amount".lower()).alias(tolls_amount_property),
col("Total_amount".lower()).alias(total_amount_property),
col("MTA_tax".lower()).alias(mta_tax_property),
col("improvement_surcharge".lower()).alias(improvement_surcharge_property),
col("Extra".lower()).alias(extra_property),
col("Tip_amount".lower()).alias(tip_amount_property),
col("Payment_type".lower()).alias(payment_type_property)
)
return dataset