Sub topic 16
Working with remote file systems such as S3
This chapter assumes you can already write and read a Hive-partitioned dataset locally, and that you know how filters prune partitions. Moving to S3 changes where the files live. It does not change the dataset API.
The filesystem is a parameter
pyarrow.dataset reads and writes through a filesystem object. For local paths PyArrow picks a local one for you. For S3 you either pass a pyarrow.fs.S3FileSystem or give a URI and let PyArrow infer it.
import pyarrow.dataset as ds
from pyarrow import fs
s3 = fs.S3FileSystem(region="eu-west-1")
dataset = ds.dataset(
"my-bucket/sales",
filesystem=s3,
format="parquet",
partitioning="hive",
)
print(dataset.schema)When you pass filesystem=, the path has no s3:// prefix. It starts with the bucket name. With a URI, the scheme selects the filesystem:
import pyarrow.dataset as ds
dataset = ds.dataset(
"s3://my-bucket/sales",
format="parquet",
partitioning="hive",
)Credentials come from the usual AWS sources: environment variables, the shared credentials file, or an instance role. To read a public bucket without credentials, use fs.S3FileSystem(anonymous=True, region="us-east-1").
Filtering and writing work the same way
Filters, projection and scanners need no changes. The partition values are still in the object keys, so pruning still avoids reading whole prefixes.
import pyarrow.dataset as ds
from pyarrow import fs
s3 = fs.S3FileSystem(region="eu-west-1")
dataset = ds.dataset("my-bucket/sales", filesystem=s3,
format="parquet", partitioning="hive")
table = dataset.to_table(
filter=ds.field("year") == 2024,
columns=["country", "amount"],
)
print(table.num_rows)Writing passes the same argument:
import pyarrow as pa
import pyarrow.dataset as ds
from pyarrow import fs
s3 = fs.S3FileSystem(region="eu-west-1")
table = pa.table({"year": [2024, 2024], "amount": [1.0, 2.0]})
ds.write_dataset(
table,
"my-bucket/sales",
filesystem=s3,
format="parquet",
partitioning=["year"],
partitioning_flavor="hive",
existing_data_behavior="overwrite_or_ignore",
)What is different on S3
- Listing costs time. Discovery lists every object under the prefix, and each request has latency. Many small files or deep partition trees make
ds.dataset(...)slow even when the filter is selective. - Pruning saves the data reads, not the listing. Choose partition keys that keep the file count moderate, as covered in the earlier chapter on small files.
- S3 has no real directories. A "partition directory" is a shared key prefix. Empty prefixes do not exist.
- Overwrite is not atomic. With
delete_matching, a failure part-way can leave a partition partly deleted. Avoid concurrent readers during such writes.
Common errors
| Symptom | Likely cause |
|---|---|
| `Bucket not found` or redirect errors | Wrong or missing `region` |
| `Access Denied` | Missing credentials, or no `s3:ListBucket` permission on the bucket |
| `Path ... does not exist` with a URI-style path | You passed `s3://` together with `filesystem=` |
Exercise
You call ds.dataset("s3://my-bucket/sales", partitioning="hive", format="parquet") with a filter on year, but opening the dataset is slow. Why does the filter not help with that step, and what would you change?
Show answer
Discovery lists all objects under the prefix before any filter is applied, so listing time depends on the number of files. Reduce the file count by using fewer or coarser partition keys and larger files, or point the dataset at a narrower prefix such as s3://my-bucket/sales/year=2024.