-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiceberg.py
More file actions
365 lines (326 loc) · 9.87 KB
/
iceberg.py
File metadata and controls
365 lines (326 loc) · 9.87 KB
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
import timeit
start_time = timeit.default_timer()
from pyspark.sql import SparkSession
from pyspark.sql import functions as F
from pyspark.sql.types import StructType, StructField, StringType, IntegerType, DoubleType, TimestampType
import random
end_time = timeit.default_timer()
elapsed_time = end_time - start_time
print("Elapsed time : ",elapsed_time)
start_time = timeit.default_timer()
spark = SparkSession.builder \
.appName("IcebergLocalDevelopment") \
.master("local[*]") \
.config('spark.jars.packages', 'org.apache.iceberg:iceberg-spark-runtime-3.5_2.12:1.5.2') \
.config("spark.sql.extensions", "org.apache.iceberg.spark.extensions.IcebergSparkSessionExtensions") \
.config("spark.sql.catalog.local", "org.apache.iceberg.spark.SparkCatalog") \
.config("spark.sql.catalog.local.type", "hadoop") \
.config("spark.sql.catalog.local.warehouse", "spark-warehouse/iceberg") \
.getOrCreate()
end_time = timeit.default_timer()
elapsed_time = end_time - start_time
print("Elapsed time : ",elapsed_time)
start_time = timeit.default_timer()
df = spark.read.json("expedia-lodging-listings-en_us-1-all.jsonl")
df.show(10)
end_time = timeit.default_timer()
elapsed_time = end_time - start_time
print("Elapsed time : ",elapsed_time)
start_time = timeit.default_timer()
target_schema = StructType([
StructField("listing_source_site", StringType(), False),
StructField("property_modified_date", TimestampType(), False),
StructField("star_rating", StringType(), True),
StructField("currency", StringType(), False),
StructField("usd_price", DoubleType(), True),
StructField("chain_and_brand", StructType([
StructField("brand_id", IntegerType(), True),
StructField("chain_id", IntegerType(), True),
StructField("brand_name", StringType(), True),
StructField("chain_name", StringType(), True)
]), True),
StructField("country_code", StringType(), False)
])
end_time = timeit.default_timer()
elapsed_time = end_time - start_time
print("Elapsed time : ",elapsed_time)
start_time = timeit.default_timer()
country_code_mapping = {
"Bonaire Saint Eustatius and Saba": "BSES",
"Paraguay": "PY",
"Anguilla": "AI",
"Macao": "MO",
"U.S. Virgin Islands": "USV",
"Senegal": "SN",
"Sweden": "SE",
"Guyana": "GY",
"Philippines": "PH",
"Jersey": "JE",
"Eritrea": "ER",
"Djibouti": "DJ",
"Norfolk Island": "NF",
"Tonga": "TO",
"Singapore": "SG",
"Malaysia": "MY",
"Fiji": "FJ",
"Turkey": "TR",
"Malawi": "MW",
"Germany": "DE",
"Northern Mariana Islands": "MP",
"Comoros": "KM",
"Cambodia": "KH",
"Maldives": "MV",
"Ivory Coast": "IC",
"Jordan": "JO",
"Rwanda": "RW",
"Palau": "PW",
"France": "FR",
"Turks and Caicos Islands": "TC",
"Greece": "GR",
"Sri Lanka": "LK",
"Montserrat": "MS",
"Taiwan": "TW",
"Dominica": "DM",
"British Virgin Islands": "VG",
"Algeria": "DZ",
"Togo": "TG",
"Equatorial Guinea": "GQ",
"Slovakia": "SK",
"Reunion": "RE",
"Argentina": "AR",
"Belgium": "BE",
"Angola": "AO",
"San Marino": "SM",
"Ecuador": "EC",
"Qatar": "QA",
"Lesotho": "LS",
"Albania": "AL",
"Madagascar": "MG",
"Finland": "FI",
"New Caledonia": "NC",
"Ghana": "GH",
"Myanmar": "MM",
"Nicaragua": "NI",
"Guernsey": "GG",
"Peru": "PE",
"Benin": "BJ",
"Sierra Leone": "SL",
"United States": "US",
"India": "IN",
"Bahamas": "BS",
"China": "CN",
"Curacao": "CUR",
"Belarus": "BY",
"Malta": "MT",
"Kuwait": "KW",
"Sao Tome and Principe": "ST",
"Palestinian Territory": "PT",
"Puerto Rico": "PR",
"Chile": "CL",
"Tajikistan": "TJ",
"Martinique": "MQ",
"Cayman Islands": "KY",
"Isle of Man": "IM",
"Croatia": "HR",
"Burundi": "BI",
"Nigeria": "NG",
"Andorra": "AD",
"Bolivia": "BO",
"Gabon": "GA",
"Italy": "IT",
"Suriname": "SR",
"Lithuania": "LT",
"Norway": "NO",
"Turkmenistan": "TM",
"Spain": "ES",
"Cuba": "CU",
"Mauritania": "MR",
"Guadeloupe": "GP",
"Denmark": "DK",
"Barbados": "BB",
"Bangladesh": "BD",
"Ireland": "IE",
"Liechtenstein": "LI",
"Swaziland": "SL",
"Thailand": "TH",
"Laos": "LA",
"Christmas Island": "CX",
"Bhutan": "BT",
"Democratic Republic of the Congo": "DRC",
"Morocco": "MA",
"Monaco": "MC",
"Panama": "PA",
"Cape Verde": "CV",
"Hong Kong": "HK",
"Israel": "IL",
"Iceland": "IS",
"Saint Barthelemy": "SB",
"Saint Kitts and Nevis": "KN",
"Oman": "OM",
"French Polynesia": "PF",
"South Korea": "KR",
"Cyprus": "CY",
"Gibraltar": "GI",
"Uruguay": "UY",
"Mexico": "MX",
"Aruba": "AW",
"Montenegro": "ME",
"Georgia": "GE",
"Zimbabwe": "ZW",
"Estonia": "EE",
"Indonesia": "ID",
"Saint Vincent and the Grenadines": "VC",
"Guatemala": "GT",
"Guam": "GU",
"Mongolia": "MN",
"Republic of the Congo": "CG",
"Azerbaijan": "AZ",
"Sint Maarten": "SIM",
"Grenada": "GD",
"Armenia": "AM",
"Tunisia": "TN",
"Liberia": "LR",
"Honduras": "HN",
"Trinidad and Tobago": "TT",
"Saudi Arabia": "SA",
"Uganda": "UG",
"Wallis and Futuna": "WF",
"French Guiana": "GF",
"Namibia": "NA",
"Mayotte": "YT",
"Switzerland": "CH",
"Zambia": "ZM",
"Ethiopia": "ET",
"Jamaica": "JM",
"Latvia": "LV",
"United Arab Emirates": "AE",
"Brunei": "BR",
"Saint Lucia": "LC",
"Saint Martin": "SAM",
"Aland Islands": "AI",
"Guinea": "GN",
"Canada": "CA",
"Seychelles": "SC",
"Kyrgyzstan": "KG",
"Uzbekistan": "UZ",
"Macedonia": "MD",
"Faroe Islands": "FO",
"Samoa": "WS",
"Czech Republic": "CZ",
"Mozambique": "MZ",
"Cook Islands": "CK",
"Brazil": "BR",
"Belize": "BZ",
"Kenya": "KE",
"Gambia": "GM",
"Lebanon": "LB",
"Slovenia": "SI",
"Antigua and Barbuda": "AG",
"Dominican Republic": "DO",
"Japan": "JP",
"Tanzania": "TZ",
"Botswana": "BW",
"Luxembourg": "LU",
"New Zealand": "NZ",
"United States Minor Outlying Islands": "UM",
"Bosnia and Herzegovina": "BA",
"Greenland": "GL",
"Haiti": "HT",
"Poland": "PL",
"Portugal": "PT",
"Australia": "AU",
"Cameroon": "CM",
"Papua New Guinea": "PG",
"Romania": "RO",
"Guinea-Bissau": "GW",
"Bulgaria": "BG",
"Austria": "AT",
"Nepal": "NP",
"Egypt": "EG",
"Costa Rica": "CR",
"El Salvador": "SV",
"Kazakhstan": "KZ",
"Serbia": "RS",
"South Africa": "ZA",
"Burkina Faso": "BF",
"Bermuda": "BM",
"Bahrain": "BH",
"Micronesia": "MC",
"Colombia": "CO",
"Hungary": "HU",
"Pakistan": "PK",
"Vanuatu": "VU",
"Mauritius": "MU",
"United Kingdom": "GB",
"Moldova": "MD",
"Vietnam": "VN",
"Netherlands": "NL",
"Mali": "ML",
"Chad": "TD",
"Svalbard and Jan Mayen": "SJ",
"Sudan": "SD",
"Niue": "NU",
"Kiribati": "KI",
"Iraq": "IQ",
"American Samoa": "AS",
"Saint Pierre and Miquelon": "PM",
"Niger": "NE",
"Solomon Islands": "SB"
}
end_time = timeit.default_timer()
elapsed_time = end_time - start_time
print("Elapsed time : ",elapsed_time)
start_time = timeit.default_timer()
def get_country_code(country_name):
if country_name is None:
return "Unknown"
return country_code_mapping.get(country_name, "Unknown")
country_udf = F.udf(get_country_code, StringType())
end_time = timeit.default_timer()
elapsed_time = end_time - start_time
print("Elapsed time : ",elapsed_time)
start_time = timeit.default_timer()
transformed_df = df.select(
F.lit("expedia").alias("listing_source_site"),
F.when(F.col("lastUpdated").isNull(), F.lit("' '"))
.otherwise(F.to_timestamp("lastUpdated", "dd-MM-yyyy HH:mm:ss"))
.alias("property_modified_date"),
F.when(F.col("starRating").isNull(), F.lit("' '"))
.otherwise(F.col("starRating").cast(StringType()))
.alias("star_rating"),
F.when(F.col("referencePrice.currency").isNull(), F.lit("' '"))
.otherwise(F.col("referencePrice.currency"))
.alias("currency"),
F.when(F.col("referencePrice.value").isNull(), F.lit("' '"))
.otherwise(F.col("referencePrice.value").cast(DoubleType()))
.alias("usd_price"),
F.struct(
F.when(F.col("chainAndBrand.brandId").isNull(), F.concat(F.lit('"brand_id": '), F.lit("' '")))
.otherwise(F.concat(F.lit('"brand_id": '), F.col("chainAndBrand.brandId").cast(IntegerType()))).alias("brand_id"),
F.when(F.col("chainAndBrand.chainId").isNull(), F.concat(F.lit('"chain_id": '), F.lit("' '")))
.otherwise(F.concat(F.lit('"chain_id": '), F.col("chainAndBrand.chainId").cast(IntegerType()))).alias("chain_id"),
F.when(F.col("chainAndBrand.brandName").isNull(), F.concat(F.lit('"brand_name": '), F.lit("' '")))
.otherwise(F.concat(F.lit('"brand_name": '), F.col("chainAndBrand.brandName").cast(StringType()))).alias("brand_name"),
F.when(F.col("chainAndBrand.chainName").isNull(), F.concat(F.lit('"chain_name": '), F.lit("' '")))
.otherwise(F.concat(F.lit('"chain_name": '), F.col("chainAndBrand.chainName").cast(StringType()))).alias("chain_name")
).alias("chain_and_brand"),
F.col("country")
)
end_time = timeit.default_timer()
elapsed_time = end_time - start_time
print("Elapsed time : ",elapsed_time)
start_time = timeit.default_timer()
final_df = transformed_df.withColumn("country_code", country_udf(F.col("country"))).drop("country")
final_df.show(5, truncate=False)
end_time = timeit.default_timer()
elapsed_time = end_time - start_time
print("Elapsed time : ",elapsed_time)
final_df.writeTo("local.test_db") \
.partitionedBy("country_code") \
.createOrReplace()
# Read from the Iceberg table
iceberg_df = spark.read \
.format("iceberg") \
.load("local.test_db")
# Show the data
iceberg_df.show()