Sub topic 5
Hive versus directory partitioning
This chapter assumes you can write basic Python, know what a Parquet file is, and have seen pyarrow.dataset write and read a table. Both partitioning flavors put column values into directory names. They differ in whether the directory name also carries the column name.
The two layouts
With Hive partitioning, each directory is named column=value. With directory partitioning, each directory holds only the value. The column name lives in the schema you pass to the reader.
| Aspect | Hive | Directory |
|---|---|---|
| Path example | `sales/year=2023/country=DE/part-0.parquet` | `sales/2023/DE/part-0.parquet` |
| Column names in paths | Yes | No |
| Reader needs a schema | Optional, names and types are inferred | Required, to name and type each level |
| Level order matters | No, names identify fields | Yes, position identifies fields |
Writing and reading Hive layout
Passing flavor="hive" to ds.partitioning produces column=value directories. On reading, partitioning="hive" is enough, and PyArrow infers the field names and types from the paths.
import pyarrow as pa
import pyarrow.dataset as ds
table = pa.table({"year": [2023, 2024], "amount": [1.0, 2.0]})
part = ds.partitioning(pa.schema([("year", pa.int64())]), flavor="hive")
ds.write_dataset(table, "sales_hive", format="parquet", partitioning=part)
dataset = ds.dataset("sales_hive", format="parquet", partitioning="hive")
print(dataset.schema)Writing and reading directory layout
Without a flavor, ds.partitioning builds a directory partitioning. The paths then contain only 2023 and 2024. A reader cannot tell what those numbers mean, so you must hand it the same schema.
import pyarrow as pa
import pyarrow.dataset as ds
table = pa.table({"year": [2023, 2024], "amount": [1.0, 2.0]})
part = ds.partitioning(pa.schema([("year", pa.int16())]))
ds.write_dataset(table, "sales_dir", format="parquet", partitioning=part)
dataset = ds.dataset("sales_dir", format="parquet", partitioning=part)
print(dataset.to_table().schema)The paths are sales_dir/2023/part-0.parquet and sales_dir/2024/part-0.parquet. The schema you supply decides the column name year and its type int16.
Choosing between them
Prefer Hive when other tools such as Spark, DuckDB or pandas will read the data. They all understand column=value paths, and the paths describe themselves. Directory partitioning gives shorter paths, but it works only if every reader knows the schema. Use it when you control all readers and want compact paths.
A common error is opening a Hive dataset without partitioning="hive". The files are found, but the partition columns are missing from the schema and filters on them fail.
Exercise
A dataset has paths like logs/2024/05/app.parquet. You open it with ds.dataset("logs", format="parquet", partitioning="hive"). Why does it not produce year and month columns, and how do you fix it?
Show answer
The paths contain no column=value pairs, so Hive discovery finds nothing to parse. This is a directory layout. Pass a schema, for example ds.partitioning(pa.schema([("year", pa.int16()), ("month", pa.int8())])), in the order the directory levels appear.