The Data Foundry

Built by Data with Pranjal

PySparkBeginnerBroken PySpark FixFree

The Small Files Avalanche

A bronze-to-silver pipeline writes partitioned Parquet to a data lake every 15 minutes.

Scenario context

The job technically succeeds, but downstream reads get slower over time and cloud object store listings become painfully expensive. Each partition contains hundreds or thousands of tiny files.

Business requirement

Fix the PySpark code so the pipeline is correct, scalable, and safe to rerun.

Schema

DataFrames depend on the scenario. Assume large production-scale inputs, skewed keys, retries, and partitioned lake storage.

Sample input

Use the code comments and logs to infer the input shape. Focus on the production failure mode, not local toy execution.

Broken logic / code

from pyspark.sql import functions as F

hourly_df = spark.read.parquet(hourly_input_path)

# Broken: too many partitions and append-only hourly writes create thousands of tiny files.
(hourly_df
  .repartition(2000)
  .write
  .mode('append')
  .partitionBy('event_date', 'event_hour')
  .parquet(silver_path))

Logs / error

[Spark] The Small Files Avalanche
Stage progress: most tasks finished, one or more tasks are long-running.
Metrics to inspect: shuffle read, spill, skew ratio, executor lost count, file count.
The job technically succeeds, but downstream reads get slower over time and cloud object store
listings become painfully expensive. Each partition contains hundreds or thousands of tiny files.

Expected output / expected logic

Corrected PySpark code or approach should reduce the failure mode, preserve correctness, and include validation/monitoring.

Your attempt

Write the corrected PySpark approach

Think before revealing the answer. A partial but honest attempt is better practice than reading the model solution first.

Saved locally

Interview-style explanation

Now explain your solution as if you are in an interview: symptom, root cause, fix, edge cases, trade-offs, monitoring, and prevention.