-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05-silver.py
483 lines (411 loc) · 24.3 KB
/
05-silver.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
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
# Databricks notebook source
# MAGIC %run ./01-config
# COMMAND ----------
class Upserter:
def __init__(self, merge_query, temp_view_name):
self.merge_query = merge_query
self.temp_view_name = temp_view_name
def upsert(self, df_micro_batch, batch_id):
df_micro_batch.createOrReplaceTempView(self.temp_view_name)
df_micro_batch._jdf.sparkSession().sql(self.merge_query)
class CDCUpserter:
def __init__(self, merge_query, temp_view_name, id_column, sort_by):
self.merge_query = merge_query
self.temp_view_name = temp_view_name
self.id_column = id_column
self.sort_by = sort_by
def upsert(self, df_micro_batch, batch_id):
from pyspark.sql.window import Window
from pyspark.sql import functions as F
window = Window.partitionBy(self.id_column).orderBy(F.col(self.sort_by).desc())
df_micro_batch.filter(F.col("update_type").isin(["new", "update"])) \
.withColumn("rank", F.rank().over(window)).filter("rank == 1").drop("rank") \
.createOrReplaceTempView(self.temp_view_name)
df_micro_batch._jdf.sparkSession().sql(self.merge_query)
class Silver():
def __init__(self, env):
self.Conf = Config()
self.checkpoint_base = self.Conf.base_dir_checkpoint + "/checkpoints"
self.catalog = env
self.db_name = self.Conf.maxFilesPerTrigger
spark.sql(f"USE {self.catalog}.{self.db_name}")
def upsert_users(self, once=True, processing_time="15 seconds", startingVersion=0):
from pyspark.sql import functions as F
query = f"""
MERGE INTO {self.catalog}.{self.db_name}.users a
USING users_delta b
ON a.user_id = b.user_id
WHEN NOT MATCHED THEN INSERT *
"""
data_upserter = Upserter(query, "users_delta")
df_delta = (spark.readStream
.option("startingVersion", startingVersion)
.option("ignoreDeletes", True)
.table(f"{self.catalog}.{self.db_name}.registered_users_bz")
.selectExpr("user_id", "device_id", "mac_address", "cast(registration_timestamp as timestamp)")
.withWatermark("registration_timestamp", "30 seconds")
.dropDuplicates(["user_id", "device_id"])
)
stream_writer = (df_delta.writeStream
.foreachBatch(data_upserter.upsert)
.outputMode("update")
.option("checkpointLocation", f"{self.checkpoint_base}/users")
.queryName("users_upsert_stream")
)
spark.sparkContext.setLocalProperty("spark.scheduler.pool", "silver_p2")
if once == True:
return stream_writer.trigger(availableNow=True).start()
else:
return stream_writer.trigger(processing_time=processing_time).start()
def upsert_gym_logs(self, once=True, processing_time="15 seconds", startingVersion=0):
from pyspark.sql import functions as F
# idempotent - Insert new login records
# - Update logout time when
# 1. It is greater than login time
# 2. It is greater than earlier logout
# 3. It is not NULL (This is also satisified by above conditions)
query = f"""
MERGE INTO {self.catalog}.{self.db_name}.gym_logs a
USING gym_logs_delta b
ON a.mac_address = b.mac_address AND a.gym = b.gym AND a.login = b.login
THEN UPDATE SET logout = b.logout
WHEN NOT MATCHED THEN INSERT *
"""
data_upserter = Upserter(query, "gym_logs_delta")
df_delta = (spark.readStream
.option("startingVersion", startingVersion)
.option("ignoreDeletes", True)
.table(f"{self.catalog}.{self.db_name.gym_logins_bz}")
.selectExpr("mac_address", "gym", "cast(login as timestamp)", "cast(logout as timestamp)")
.withWaterMark("login", "30 seconds")
.dropDuplicates(["mac_address", "gym", "login"])
)
stream_writer = (df_delta.writeStream
.foreachBatch(data_upserter.upsert)
.outputMode("update")
.option("checkpointLocation", f"{self.checkpoint_base}/gym_logs")
.queryName("gym_logs_upsert_stream")
)
spark.sparkContext.setLocalProperty("spark.scheduler.pool", "silver_p3")
if once == True:
return stream_writer.trigger(availableNow=True).start()
else:
return stream_writer.trigger(processing_time=processing_time).start()
def upsert_user_profile(self, once=False, processing_time="15 seconds", startingVersion=0):
from pyspark.sql import functions as F
# Idempotent - Insert new record
# - Ignore deletes
# - Update user details when
# 1. update_type in ("new", "append")
# 2. current update is newer than the earlier
schema = """
user_id bigint, update_type STRING, timestamp FLOAT,
dob STRING, sex STRING, gender STRING, first_name STRING, last_name STRING,
address STRUCT<street_address: STRING, city: STRING, state: STRING, zip: INT>
"""
query = f"""
MERGE INTO {self.catalog}.{self.db_name}.user_profile a
USING user_profile_cdc b
ON a.user_id=b.user_id
WHEN MATCHED AND a.updated < b.updated
THEN UPDATE SET *
WHEN NOT MATCHED
THEN INSERT *
"""
data_upserter = CDCUpserter(query, "user_profile_cdc", "user_id", "updated")
df_cdc = (spark.readStream
.option("startingVersion", startingVersion)
.option("ignoreDeletes", True)
# .option("withEventTimeOrder", "true")
# .option("maxFilesPerTrigger", self.maxFilesPerTrigger)
.table(f"{self.catalog}.{self.db_name}.kafka_multiplex_bz")
.filter("topic = 'user_info'")
.select(F.from_json(F.col("value").cast("string"), schema).alias("v"))
.select("v.*")
.select("user_id", F.to_date('dob', 'MM/dd/yyyy').alias('dob'),
'sex', 'gender', 'first_name', 'last_name', 'address.*',
F.col('timestamp').cast("timestamp").alias("updated"),
"update_type")
.withWatermark("updated", "30 seconds")
.dropDuplicates(["user_id", "updated"])
)
stream_writer = (df_cdc.writeStream
.foreachBatch(data_upserter.upsert)
.outputMode("update")
.option("checkpointLocation", f"{self.checkpoint_base}/user_profile")
.queryName("user_profile_stream")
)
spark.sparkContext.setLocalProperty("spark.scheduler.pool", "silver_p3")
if once == True:
return stream_writer.trigger(availableNow=True).start()
else:
return stream_writer.trigger(processingTime=processing_time).start()
def upsert_workouts(self, once=False, processing_time="10 seconds", startingVersion=0):
from pyspark.sql import functions as F
schema = "user_id INT, workout_id INT, timestamp FLOAT, action STRING, session_id INT"
# Idempotent - User cannot have two workout sessions at the same time. So ignore the duplicates and insert the new records
query = f"""
MERGE INTO {self.catalog}.{self.db_name}.workouts a
USING workouts_delta b
ON a.user_id=b.user_id AND a.time=b.time
WHEN NOT MATCHED THEN INSERT *
"""
data_upserter = Upserter(query, "workouts_delta")
df_delta = (spark.readStream
.option("startingVersion", startingVersion)
.option("ignoreDeletes", True)
# .option("withEventTimeOrder", "true")
# .option("maxFilesPerTrigger", self.maxFilesPerTrigger)
.table(f"{self.catalog}.{self.db_name}.kafka_multiplex_bz")
.filter("topic = 'workout'")
.select(F.from_json(F.col("value").cast("string"), schema).alias("v"))
.select("v.*")
.select("user_id", "workout_id",
F.col("timestamp").cast("timestamp").alias("time"),
"action", "session_id")
.withWatermark("time", "30 seconds")
.dropDuplicates(["user_id", "time"])
)
stream_writer = (df_delta.writeStream
.foreachBatch(data_upserter.upsert)
.outputMode("update")
.option("checkpointLocation", f"{self.checkpoint_base}/workouts")
.queryName("workouts_upsert_stream")
)
spark.sparkContext.setLocalProperty("spark.scheduler.pool", "silver_p3")
if once == True:
return stream_writer.trigger(availableNow=True).start()
else:
return stream_writer.trigger(processingTime=processing_time).start()
def upsert_heart_rate(self, once=False, processing_time="10 seconds", startingVersion=0):
from pyspark.sql import functions as F
schema = "device_id LONG, time TIMESTAMP, heartrate DOUBLE"
# Idempotent - Only one BPM signal is allowed at a timestamp. So ignore the duplicates and insert the new records
query = f"""
MERGE INTO {self.catalog}.{self.db_name}.heart_rate a
USING heart_rate_delta b
ON a.device_id=b.device_id AND a.time=b.time
WHEN NOT MATCHED THEN INSERT *
"""
data_upserter = Upserter(query, "heart_rate_delta")
df_delta = (spark.readStream
.option("startingVersion", startingVersion)
.option("ignoreDeletes", True)
# .option("withEventTimeOrder", "true")
# .option("maxFilesPerTrigger", self.maxFilesPerTrigger)
.table(f"{self.catalog}.{self.db_name}.kafka_multiplex_bz")
.filter("topic = 'bpm'")
.select(F.from_json(F.col("value").cast("string"), schema).alias("v"))
.select("v.*", F.when(F.col("v.heartrate") <= 0, False).otherwise(True).alias("valid"))
.withWatermark("time", "30 seconds")
.dropDuplicates(["device_id", "time"])
)
stream_writer = (df_delta.writeStream
.foreachBatch(data_upserter.upsert)
.outputMode("update")
.option("checkpointLocation", f"{self.checkpoint_base}/heart_rate")
.queryName("heart_rate_upsert_stream")
)
spark.sparkContext.setLocalProperty("spark.scheduler.pool", "silver_p2")
if once == True:
return stream_writer.trigger(availableNow=True).start()
else:
return stream_writer.trigger(processingTime=processing_time).start()
def age_bins(self, dob_col):
from pyspark.sql import functions as F
age_col = F.floor(F.months_between(F.current_date(), dob_col) / 12).alias("age")
return (F.when((age_col < 18), "under 18")
.when((age_col >= 18) & (age_col < 25), "18-25")
.when((age_col >= 25) & (age_col < 35), "25-35")
.when((age_col >= 35) & (age_col < 45), "35-45")
.when((age_col >= 45) & (age_col < 55), "45-55")
.when((age_col >= 55) & (age_col < 65), "55-65")
.when((age_col >= 65) & (age_col < 75), "65-75")
.when((age_col >= 75) & (age_col < 85), "75-85")
.when((age_col >= 85) & (age_col < 95), "85-95")
.when((age_col >= 95), "95+")
.otherwise("invalid age").alias("age"))
def upsert_user_bins(self, once=True, processing_time="15 seconds", startingVersion=0):
from pyspark.sql import functions as F
# Idempotent - This table is maintained as SCD Type 1 dimension
# - Insert new user_id records
# - Update old records using the user_id
query = f"""
MERGE INTO {self.catalog}.{self.db_name}.user_bins a
USING user_bins_delta b
ON a.user_id=b.user_id
WHEN MATCHED
THEN UPDATE SET *
WHEN NOT MATCHED THEN INSERT *
"""
data_upserter = Upserter(query, "user_bins_delta")
df_user = spark.table(f"{self.catalog}.{self.db_name}.users").select("user_id")
# Running stream on silver table requires ignoreChanges
# No watermark required - Stream to staic join is stateless
df_delta = (spark.readStream
.option("startingVersion", startingVersion)
.option("ignoreChanges", True)
# .option("withEventTimeOrder", "true")
# .option("maxFilesPerTrigger", self.maxFilesPerTrigger)
.table(f"{self.catalog}.{self.db_name}.user_profile")
.join(df_user, ["user_id"], "left")
.select("user_id", self.age_bins(F.col("dob")), "gender", "city", "state")
)
stream_writer = (df_delta.writeStream
.foreachBatch(data_upserter.upsert)
.outputMode("update")
.option("checkpointLocation", f"{self.checkpoint_base}/user_bins")
.queryName("user_bins_upsert_stream")
)
spark.sparkContext.setLocalProperty("spark.scheduler.pool", "silver_p3")
if once == True:
return stream_writer.trigger(availableNow=True).start()
else:
return stream_writer.trigger(processingTime=processing_time).start()
def upsert_completed_workouts(self, once=True, processing_time="15 seconds", startingVersion=0):
from pyspark.sql import functions as F
# Idempotent - Only one user workout session completes. So ignore the duplicates and insert the new records
query = f"""
MERGE INTO {self.catalog}.{self.db_name}.completed_workouts a
USING completed_workouts_delta b
ON a.user_id=b.user_id AND a.workout_id = b.workout_id AND a.session_id=b.session_id
WHEN NOT MATCHED THEN INSERT *
"""
data_upserter = Upserter(query, "completed_workouts_delta")
df_start = (spark.readStream
.option("startingVersion", startingVersion)
.option("ignoreDeletes", True)
# .option("withEventTimeOrder", "true")
# .option("maxFilesPerTrigger", self.maxFilesPerTrigger)
.table(f"{self.catalog}.{self.db_name}.workouts")
.filter("action = 'start'")
.selectExpr("user_id", "workout_id", "session_id", "time as start_time")
.withWatermark("start_time", "30 seconds")
# .dropDuplicates(["user_id", "workout_id", "session_id", "start_time"])
)
df_stop = (spark.readStream
.option("startingVersion", startingVersion)
.option("ignoreDeletes", True)
# .option("withEventTimeOrder", "true")
# .option("maxFilesPerTrigger", self.maxFilesPerTrigger)
.table(f"{self.catalog}.{self.db_name}.workouts")
.filter("action = 'stop'")
.selectExpr("user_id", "workout_id", "session_id", "time as end_time")
.withWatermark("end_time", "30 seconds")
# .dropDuplicates(["user_id", "workout_id", "session_id", "end_time"])
)
# State cleanup - Define a condition to clean the state
# - stop must occur within 3 hours of start
# - stop < start + 3 hours
join_condition = [df_start.user_id == df_stop.user_id, df_start.workout_id == df_stop.workout_id,
df_start.session_id == df_stop.session_id,
df_stop.end_time < df_start.start_time + F.expr('interval 3 hour')]
df_delta = (df_start.join(df_stop, join_condition)
.select(df_start.user_id, df_start.workout_id, df_start.session_id, df_start.start_time,
df_stop.end_time)
)
stream_writer = (df_delta.writeStream
.foreachBatch(data_upserter.upsert)
.outputMode("append")
.option("checkpointLocation", f"{self.checkpoint_base}/completed_workouts")
.queryName("completed_workouts_upsert_stream")
)
spark.sparkContext.setLocalProperty("spark.scheduler.pool", "silver_p1")
if once == True:
return stream_writer.trigger(availableNow=True).start()
else:
return stream_writer.trigger(processingTime=processing_time).start()
def upsert_workout_bpm(self, once=True, processing_time="15 seconds", startingVersion=0):
from pyspark.sql import functions as F
# Idempotent - Only one user workout session completes. So ignore the duplicates and insert the new records
query = f"""
MERGE INTO {self.catalog}.{self.db_name}.workout_bpm a
USING workout_bpm_delta b
ON a.user_id=b.user_id AND a.workout_id = b.workout_id AND a.session_id=b.session_id AND a.time=b.time
WHEN NOT MATCHED THEN INSERT *
"""
data_upserter = Upserter(query, "workout_bpm_delta")
df_users = spark.read.table("users")
df_completed_workouts = (spark.readStream
.option("startingVersion", startingVersion)
.option("ignoreDeletes", True)
# .option("withEventTimeOrder", "true")
# .option("maxFilesPerTrigger", self.maxFilesPerTrigger)
.table(f"{self.catalog}.{self.db_name}.completed_workouts")
.join(df_users, "user_id")
.selectExpr("user_id", "device_id", "workout_id", "session_id", "start_time",
"end_time")
.withWatermark("end_time", "30 seconds")
)
df_bpm = (spark.readStream
.option("startingVersion", startingVersion)
.option("ignoreDeletes", True)
# .option("withEventTimeOrder", "true")
# .option("maxFilesPerTrigger", self.maxFilesPerTrigger)
.table(f"{self.catalog}.{self.db_name}.heart_rate")
.filter("valid = True")
.selectExpr("device_id", "time", "heartrate")
.withWatermark("time", "30 seconds")
)
# State cleanup - Define a condition to clean the state
# - Workout could be a maximum of three hours
# - workout must end within 3 hours of bpm
# - workout.end < bpm.time + 3 hours
join_condition = [df_completed_workouts.device_id == df_bpm.device_id,
df_bpm.time > df_completed_workouts.start_time,
df_bpm.time <= df_completed_workouts.end_time,
df_completed_workouts.end_time < df_bpm.time.expr('interval 3 hour')]
df_delta = (df_bpm.join(df_completed_workouts, join_condition)
.select("user_id", "workout_id", "session_id", "start_time", "end_time", "time", "heartrate")
)
stream_writer = (df_delta.writeStream
.foreachBatch(data_upserter.upsert)
.outputMode("append")
.option("checkpointLocation", f"{self.checkpoint_base}/workout_bpm")
.queryName("workout_bpm_upsert_stream")
)
spark.sparkContext.setLocalProperty("spark.scheduler.pool", "silver_p2")
if once == True:
return stream_writer.trigger(availableNow=True).start()
else:
return stream_writer.trigger(processingTime=processing_time).start()
def _await_queries(self, once):
if once:
for stream in spark.streams.active:
stream.awaitTermination()
def upsert(self, once=True, processing_time="5 seconds"):
import time
start = int(time.time())
print(f"\nExecuting silver layer upsert ...")
self.upsert_users(once, processing_time)
self.upsert_gym_logs(once, processing_time)
self.upsert_user_profile(once, processing_time)
self.upsert_workouts(once, processing_time)
self.upsert_heart_rate(once, processing_time)
self._await_queries(once)
print(f"Completed silver layer 1 upsert {int(time.time()) - start} seconds")
self.upsert_user_bins(once, processing_time)
self.upsert_completed_workouts(once, processing_time)
self._await_queries(once)
print(f"Completed silver layer 2 upsert {int(time.time()) - start} seconds")
self.upsert_workout_bpm(once, processing_time)
self._await_queries(once)
print(f"Completed silver layer 3 upsert {int(time.time()) - start} seconds")
def assert_count(self, table_name, expected_count, filter="true"):
print(f"Validating record counts in {table_name}...", end='')
actual_count = spark.read.table(f"{self.catalog}.{self.db_name}.{table_name}").where(filter).count()
assert actual_count == expected_count, f"Expected {expected_count:,} records, found {actual_count:,} in {table_name} where {filter}"
print(f"Found {actual_count:,} / Expected {expected_count:,} records where {filter}: Success")
def validate(self, sets):
import time
start = int(time.time())
print(f"\nValidating silver layer records...")
self.assert_count("users", 5 if sets == 1 else 10)
self.assert_count("gym_logs", 8 if sets == 1 else 16)
self.assert_count("user_profile", 5 if sets == 1 else 10)
self.assert_count("workouts", 16 if sets == 1 else 32)
self.assert_count("heart_rate", sets * 253801)
self.assert_count("user_bins", 5 if sets == 1 else 10)
self.assert_count("completed_workouts", 8 if sets == 1 else 16)
self.assert_count("workout_bpm", 3968 if sets == 1 else 8192)
print(f"Silver layer validation completed in {int(time.time()) - start} seconds")