A large customer aggregate query is shuffling terabytes of data across the network, causing bottleneck delays. Optimize this by tuning the partition size and reducing shuffles.
The processing pipeline will ingest the following local mock file structures (e.g. simulated as `dataset.csv`):
| customer_id | spent |
|---|---|
| 101 | 15.0 |
| 102 | 22.0 |
| 101 | 8.0 |
| 103 | 11.0 |
| 102 | 15.0 |
customer_id,spent 101,15.0 102,22.0 101,8.0 103,11.0 102,15.0
Copy this starter template to write the data transformation logic:
# starter.py - Large-Scale Customer Analytics
from pyspark.sql import SparkSession
from pyspark.sql.functions import col, sum
def optimize_shuffle():
spark = SparkSession.builder.appName("ShuffleOptimization").getOrCreate()
# TODO: Set spark.sql.shuffle.partitions, group & sum, view partition counts
pass
if __name__ == "__main__":
optimize_shuffle().config("spark.sql.shuffle.partitions", "4").
customer_id and calculate the sum of transaction spent amounts.
grouped.rdd.getNumPartitions().