Sub topic 14
Arrow Flight and ADBC for Moving Data Between Processes and Databases
This chapter assumes you know what IPC streams, record batches and schemas are from earlier chapters, and that you have used a database driver such as sqlite3 or a pandas read_sql call. Arrow Flight and ADBC both take the IPC idea and apply it to network services and databases.
The problem they solve
An IPC stream is a schema followed by record batches. That works well when you control both ends and can pass bytes yourself. Two gaps remain:
- Between processes and machines: you need a protocol for requesting data, authenticating, and streaming batches over a network.
- Between your code and a database: classic drivers such as ODBC and JDBC return data row by row. A columnar database must turn its columns into rows, and your program then turns them back into columns.
Flight addresses the first gap. ADBC addresses the second.
Arrow Flight
Arrow Flight is an RPC framework (remote procedure call: calling a function on another machine) built on gRPC. It sends Arrow record batches in their IPC layout, so the receiver does not parse values one at a time.
A Flight service offers a small set of operations:
| Operation | Purpose |
|---|---|
| `list_flights` | List the datasets the server offers |
| `get_flight_info` | Describe a dataset: its schema and where to fetch it |
| `do_get` | Download a stream of record batches |
| `do_put` | Upload a stream of record batches |
| `do_exchange` | Send and receive batches in one call |
A FlightInfo response contains one or more endpoints, each with a ticket. A ticket is an opaque token the client presents to do_get. Different endpoints can live on different servers, so a client can fetch partitions of one dataset in parallel.
The following example starts a server in a background thread and reads a table from it. It uses only pyarrow.
import threading
import pyarrow as pa
import pyarrow.flight as flight
class TableServer(flight.FlightServerBase):
def __init__(self, location, table):
super().__init__(location)
self._table = table
def do_get(self, context, ticket):
return flight.RecordBatchStream(self._table)
table = pa.table({"id": [1, 2, 3], "name": ["a", "b", "c"]})
server = TableServer("grpc://127.0.0.1:0", table)
threading.Thread(target=server.serve, daemon=True).start()
client = flight.connect(f"grpc://127.0.0.1:{server.port}")
result = client.do_get(flight.Ticket(b"any")).read_all()
print(result.equals(table))
client.close()
server.shutdown()Port 0 asks the operating system for a free port, and server.port reports the one it chose. This toy server ignores the ticket. A real server uses it to decide what to return.
ADBC
ADBC (Arrow Database Connectivity) is a database API like ODBC or JDBC, except that results come back as Arrow data. A driver for a columnar database can pass its columns through with little conversion. A driver for a row-based database still converts, but only once, directly into Arrow.
ADBC drivers are installed as separate packages. This example uses SQLite:
# pip install adbc-driver-sqlite pyarrow
import pyarrow as pa
from adbc_driver_sqlite import dbapi
with dbapi.connect() as conn:
with conn.cursor() as cur:
cur.execute("CREATE TABLE t (id INTEGER, name TEXT)")
cur.execute("INSERT INTO t VALUES (1, 'a'), (2, 'b')")
cur.execute("SELECT id, name FROM t")
result = cur.fetch_arrow_table()
print(result.num_rows)
print(result.schema)fetch_arrow_table() returns a pyarrow.Table directly. Without an argument, connect() opens an in-memory SQLite database.
Choosing between them
- Use Flight when you run a data service and clients need to pull or push Arrow data over the network, possibly in parallel.
- Use ADBC when your code queries a database and you want Arrow results without a row-to-column step.
- Flight SQL combines the two: it is a protocol for running SQL over Flight, and ADBC has a Flight SQL driver.
Common errors
- Assuming zero-copy over a network. Bytes still travel through gRPC. The saving is that no per-value serialization happens, not that no copying happens.
- Expecting a fast result from a row-based database. The database still produces rows internally, so the benefit is smaller than with a columnar engine.
- Leaving servers and clients open. Close clients and shut servers down, as in the example, or the process may not exit cleanly.
Exercise
A client calls getflightinfo and receives three endpoints, each with its own ticket. How should it fetch the whole dataset, and why does the design allow this?
Show answer
It calls do_get once per ticket and combines the resulting tables, possibly in parallel. Each endpoint can point to a different server, so a dataset can be split into partitions that are served and downloaded independently.
Exercise
Why can an ADBC query against a columnar database be faster than the same query through ODBC?
Show answer
ODBC returns rows, so the database converts columns to rows and the client converts them back to columns. ADBC returns Arrow batches, which are already columnar, so one or both conversions are avoided.