Sub topic 3
Memory-Mapped Arrow Files and Data Exchange
Assume you know basic Python, tables, and the difference between memory and disk. This chapter connects Arrow files to Parquet, dataset scans, cross-language exchange, and database access.
Memory-mapped reads of Arrow files
A memory map lets a program access a file through virtual memory. The operating system loads pages when needed. PyArrow can therefore expose Arrow file buffers without first copying the entire file into new memory.
import pyarrow as pa
table = pa.table({"id": [1, 2, 3], "name": ["a", "b", "c"]})
with pa.OSFile("example.arrow", "wb") as sink:
with pa.ipc.new_file(sink, table.schema) as writer:
writer.write_table(table)
with pa.memory_map("example.arrow", "r") as source:
mapped = pa.ipc.open_file(source).read_all()
print(mapped.num_rows)Memory mapping works best for local, immutable files that are read repeatedly. It does not make the file smaller, and accessing many scattered pages can still be slow.
Arrow and Parquet: in-memory versus on-disk
Arrow defines an in-memory layout. Parquet is a compressed, column-oriented file format. A Parquet reader usually decodes compressed pages into Arrow arrays. Use Arrow for processing and exchange; use Parquet for compact storage and long-term files.
Scanning datasets with filters and projection
A dataset is a collection of files treated as one logical table. A filter selects rows. Projection selects columns. Applying both during the scan avoids reading unnecessary data.
import pyarrow.dataset as ds
dataset = ds.dataset("data/", format="parquet")
result = dataset.to_table(
columns=["id", "name"],
filter=ds.field("id") > 1,
)Partitioning and file statistics can let the dataset scanner skip whole files.
Handing arrays between libraries
The Arrow C Data Interface defines a language-independent way to share an array and its schema. Libraries exchange pointers to buffers, validity information, offsets, and type metadata. This can avoid serialization, but the receiving library must keep the exported memory alive.
Moving data between processes and databases
Arrow Flight is a network protocol for moving Arrow record batches between services. ADBC is a database connectivity API that can expose query results in Arrow-oriented form. Flight suits Arrow-to-Arrow services; ADBC suits database access.
Limits and trade-offs
Arrow may not help when data is tiny, when a library requires Python objects, or when compression and storage size matter more than processing speed. Conversions involving nulls, strings, incompatible types, or mutable data may copy. Always measure the complete path, including reading, conversion, computation, and network transfer.
Exercise
Should you choose Parquet or Arrow IPC for a compressed analytical data lake?
Show answer
Choose Parquet for compact, durable storage. Decode it into Arrow for in-memory processing.