A join between a massive clicks dataset and a small lookup table is failing because a few customer IDs hold 90% of the transactions (data skew). Resolve this using broadcast joins.
The processing pipeline will ingest the following local mock file structures (e.g. simulated as `dataset.csv`):
| user_id | action |
|---|---|
| 1001 | click |
| 1001 | view |
| 1001 | purchase |
| 1002 | click |
| 1001 | click |
| --- | |
| user_id | user_name |
| 1001 | System User |
| 1002 | Standard User |
user_id,action 1001,click 1001,view 1001,purchase 1002,click 1001,click --- user_id,user_name 1001,System User 1002,Standard User
Copy this starter template to write the data transformation logic:
# starter.py - Handle Data Skew
from pyspark.sql import SparkSession
from pyspark.sql.functions import col, broadcast, concat, lit
def handle_skew():
spark = SparkSession.builder.appName("DataSkewLab").getOrCreate()
act_data = [(1001, "click"), (1001, "view"), (1001, "purchase"), (1002, "click"), (1001, "click")]
user_data = [(1001, "System User"), (1002, "Standard User")]
# TODO: Perform broadcast join
pass
if __name__ == "__main__":
handle_skew()broadcast() function inside the join statement: act_df.join(broadcast(user_df), on="user_id"). This avoids shuffling the massive activities table by sending a copy of the users table to every executor.
concat(col("user_id"), lit("_"), (rand() * 2).cast("int")).