Describe how PySpark handles data partitioning.
Instruction: Explain the concept of data partitioning in PySpark and its impact on distributed computing.
Updated
A Spark partition is a unit of distributed data processing. A stage normally runs a task per partition; available executor cores determine how many tasks can run concurrently. Input splits, transformations and SQL planning determine the partition layout, which can change between stages.
For DataFrames, distinguish these operations:
repartition(n, "key")shuffles rows into hash partitions. Equal keys go to the same partition, but skewed keys can make sizes very uneven. More partitions do not split a single hot key.repartitionByRange(n, "key")uses sampled key ranges. It does not promise rows are sorted within each partition; usesortWithinPartitionswhen that ordering is required.coalesce(n)can reduce partitions through a narrow dependency. It does not accept key columns, increase parallelism, or rebalance arbitrary skew. Excessive coalescing can create large tasks.
Repartition API, Range partitioning, Coalesce API
For daily unique users, define the calendar and time zone, derive a date, and aggregate distinct user IDs by that date. Repartitioning only by date can concentrate a large day's events into one task; it does not automatically eliminate the work needed for distinct aggregation. Inspect the physical plan before adding a manual shuffle.
File-directory partitioning, such as writing by date, is a separate storage decision that can help later partition pruning. It is not a promise of one file or one execution partition per day. Choose task sizes using measured shuffle volume, skew, spills and scheduling overhead. Adaptive Query Execution may change shuffle partitioning at runtime, so verify the executed plan and metrics. SQL performance tuning
Related Questions
-
easy
-
medium
-
hard
-
hard
-
hard
-
hard