Sub topic 9
Sending Arrow data: IPC streams and files
This chapter assumes you can build Arrow tables and schemas in pyarrow, and that you know what a record batch is. You should also know the difference between in-memory and on-disk data.
Why an IPC format exists
Arrow arrays live in memory as buffers. To send a table to another process, or save it to disk, you need a byte layout that both sides agree on. Arrow's inter-process communication (IPC) format is that layout. It copies the buffers as they are, so there is no per-value encoding step like the one a text format such as CSV needs.
The format carries the schema first and then the data as record batches. A reader in any Arrow implementation (C++, Java, Rust, Go, JavaScript) can rebuild the same table from those bytes.
Stream format and file format
Arrow defines two variants of IPC.
| Aspect | Stream format | File format |
|---|---|---|
| Layout | Schema, then record batches, then an end marker | Same messages, plus a footer with batch locations |
| Access | Sequential, front to back | Random access to any batch |
| Total size known up front | No | Yes, from the footer |
| Typical use | Sockets, pipes, network responses | Files on disk, memory mapping |
Use a stream when data arrives over time or you do not know how many batches will follow. Use a file when you want to jump to a particular batch or memory-map the data.
Writing and reading a stream
The writer sends the schema once, then each batch. The reader reads the schema first, so it knows the column types before any data arrives.
import pyarrow as pa
table = pa.table({"id": [1, 2, 3], "name": ["a", "b", "c"]})
sink = pa.BufferOutputStream()
with pa.ipc.new_stream(sink, table.schema) as writer:
writer.write_table(table)
buf = sink.getvalue()
reader = pa.ipc.open_stream(buf)
result = reader.read_all()
print(result.equals(table))BufferOutputStream collects the bytes in memory. In a real program the sink could be a socket or a file. read_all() returns a table. You can also loop over reader to get one record batch at a time, which keeps memory use low for long streams.
Writing and reading a file
The file format uses new_file and open_file. The file reader can report how many batches the file holds and fetch any one of them.
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.OSFile("example.arrow", "rb") as source:
reader = pa.ipc.open_file(source)
print(reader.num_record_batches)
print(reader.get_batch(0).num_rows)Common errors
- Schema mismatch. The writer is created with one schema. Writing a table whose schema differs raises an error. Cast the table first, or create the writer from the right schema.
- Reading a file as a stream, or the reverse. The two variants are not interchangeable.
open_streamon a file written withnew_file(or the reverse) fails. Check which writer produced the bytes. - Forgetting to close the writer. The stream's end marker and the file's footer are written on close. Use
with, or a file may be unreadable. - Assuming IPC is compressed. By default the buffers are written uncompressed. Files are often larger than the same data in Parquet. Compression is an option on the writer, not the default.
Exercise
You are sending record batches over a network socket, and the sender does not know how many batches there will be. Which IPC variant should it use, and why?
Show answer
The stream format. It needs only a schema followed by batches and an end marker, so the writer does not need to know the total count. The file format writes a footer listing batch locations, which requires the writer to finish first and suits random access on disk.
Exercise
A colleague writes a table with pa.ipc.new_file and then calls pa.ipc.open_stream on the result. What happens, and what is the fix?
Show answer
The read fails because the file format and the stream format have different layouts. The fix is to call pa.ipc.open_file on data written with new_file, or to write with new_stream if a stream is wanted.