Helper for handling PySpark DataFrame partition size 📑🎛️
Slowing down. Last release over 2 years ago, little activity since. Fine for existing code; think twice for a new project.
pip install repartipyRepartiPy
RepartiPy helps you to elaborately handle PySpark DataFrame partition size.
Possible Use Cases
- Repartition your DataFrame precisely, without knowing the whole DataFrame size (i.e.
Dynamic Repartition) - Estimate your DataFrame size with more accuracy
Why RepartiPy
Although Spark SizeEstimator can be used to estimate a DataFrame size, it is not accurate sometimes. RepartiPy uses Spark's execution plan statistics in order to provide a roundabout way. It suggests two approaches to achieve this:
reaprtipy.SizeEstimatorreaprtipy.SamplingSizeEstimator
reaprtipy.SizeEstimator
Recommended when your executor resource (memory) is affordable to cache the whole DataFrame.
SizeEstimator just simply caches the whole Dataframe into the memory and extract the execution plan statistics.
repartipy.SamplingSizeEstimator
Recommended when your executor resource (memory) is NOT affordable to cache the whole dataframe.
SamplingSizeEstimator uses 'disk write and re-read (HDFS)' approach behind the scene for two reasons:
- Prevent double read from the source like S3, which might be inefficient -> better performance
- Reduce partition skewness by reading data again on purpose (leverage MaxPartitionBytes) -> better sampling result
Therefore, you must have HDFS settings on your cluster and enough disk space.
This may not be accurate compared to SizeEstimator due to sampling.
If you want more accurate results, tune the sample_count option properly.
Additionally, this approach will be slower than SizeEstimator as SamplingSizeEstimator requires disk I/O and additional logics.
How To Use
Setup
pip install repartipy
Prerequisite
from pyspark.sql import SparkSession
spark = SparkSession.builder.getOrCreate()
input_data = [
(1, "Seoul"),
(2, "Busan"),
]
df = spark.createDataFrame(data=input_data, schema=["id", "location"])