Context
I have a Parquet-table stored in HDFS with two partitions, whereby each partition yields only one file.
parquet_table \
| year=2020 \
file_1.snappy.parquet
| year=2021 \
file_2.snappy.parquet
My plan is to only grap the latest partition and work on that.
df = spark.read.parquet("hdfs_path_to_table/parquet_table/year=2021/")
This works, I only retrieve the required data.
While I wrote that for pySpark I assume that pure Spark will be somehow analog.
Problem
Despite the fact that I retrieve the correct data, Spark still has two partitions connected to the DataFrame df:
df.rdd.getNumPartitions()
# -> 2
When I count the contents inside the partitions, I see that only one yields data:
df.rdd.mapPartitions(lambda partition: [len([row for row in partition])]).collect()
# -> [1450220, 0]
Of course I can now easily do a df.coalesce(1) and end up with the desired result.
Anyhow, I wonder why this happens and I'd actually rather do not want to have to coalesce but directly only retrieve the partition.
Question
Is there any solution how my DataFrame df will only have the corresponding correct .getNumPartitions()?
Thus, is there a way to load a single parquet-file and yield this file in a single partition?