Arrow Flight¶
Arrow Flight sends Arrow
tables over the network. ddigraph serve --flight uses it to serve the
store. A program asks for a dataset, and gets its
nodes and relationships as Arrow record batches. It needs no file and no
database. Clients exist for Python, Java, Go, Rust, C++, R and others.
The server only reads the store. It never writes to it.
Start the server¶
Flight comes with the arrow extra:
pip install "ddigraph[arrow]"
Add a file to the store, then start the server:
$ ddigraph store add survey.xml
Generation 1 of dataset 'survey': 7 nodes, 6 relationships
$ ddigraph serve --flight
Serving the store in ddigraph-store with Arrow Flight on grpc://127.0.0.1:8815; stop it with Ctrl-C
| Option | What it does |
|---|---|
--store DIR |
The store's folder (default: ddigraph-store, or DDIGRAPH_STORE_DIR) |
--host HOST |
The address to listen on (default: 127.0.0.1) |
--port PORT |
The port to listen on (default: 8815); 0 picks a free one |
--allow-remote |
Allow a --host that other machines can reach |
Without the extra, it stops with
Arrow Flight needs the arrow extra: pip install 'ddigraph[arrow]'.
Keep it private¶
The server checks no client. It has no password, no token and no TLS. So anyone who can reach its port can read every dataset of the store.
By default it listens on 127.0.0.1, so only programs on the same machine
can reach it. Any other address is refused, unless you add --allow-remote:
$ ddigraph serve --flight --host 0.0.0.0
--host 0.0.0.0 lets other machines read the store, and Arrow Flight checks no client: put it behind TLS and authentication, then add --allow-remote
Before you open it to a network, put it behind a proxy that adds TLS and checks who connects, as for the HTTP API. A proxy that speaks gRPC, such as nginx or Envoy, can do both.
Flights¶
The server gives one flight per dataset of the store. A flight has:
- a descriptor: a path of one element, the dataset ID;
app_metadata: JSON that gives the dataset, HEAD's number, and the number of nodes and relationships HEAD holds, such as{"dataset":"survey","generation":1,"nodes":7,"relationships":6};total_records: the nodes and the relationships, added up;- two endpoints, in order: one for HEAD's nodes and one for its relationships. Each has a ticket that names HEAD's number.
list_flights lists them all, sorted by dataset ID. get_flight_info gives
one, for the descriptor of its dataset.
Tickets¶
A ticket says what to read. It is JSON with three fields:
| Field | What it holds |
|---|---|
dataset |
The dataset ID |
generation |
The generation's number, or null for HEAD |
part |
nodes or relationships |
The tickets of a flight name HEAD's number. So the nodes and relationships you read come from one generation, even if a new one is added in between. To read an older generation, write the ticket yourself:
{"dataset": "survey", "generation": 1, "part": "relationships"}
A ticket that is not valid JSON, or names a generation the store does not
hold, gets a FlightServerError that says why.
Rows and batches¶
Each row has the columns of
export --format arrow, with the
same types. A node is its key, node_label, identity, source,
flavor, line and unresolved, then one column per property, sorted. A
relationship is its start, type, end, ordinal and resolution,
then its properties. A text property p is two columns, p and p_lang.
A list stays a list, and a number a number.
So a part, read whole, is the table the export would write for the same
graph. The rows come in the order of Store.read. The nodes come sorted by
key, with the Dataset node first, in a batch of its own. The relationships
come sorted by start, type, end and ordinal. Each later batch is one block of
the store, of up to 1,000 rows.
To find the columns and their types, the server reads the part once before it sends the first batch. Then it reads it again as it sends. So memory stays at about one block, however large the dataset.
A client in Python¶
The example below starts a server in its own process, so it runs on its own.
With ddigraph serve --flight running, you would only connect to
grpc://127.0.0.1:8815.
import json
import pyarrow.flight as flight
from ddigraph import ir
from ddigraph.flight import server
from ddigraph.store import Store
store = Store("ddigraph-store")
store.write("survey", ir.parse("survey.xml", dataset_id="survey"), memory_limit=2**26)
running = server(store, port=0) # port 0: any free port
client = flight.connect(f"grpc://127.0.0.1:{running.port}")
for info in client.list_flights():
print(json.loads(info.app_metadata))
nodes = client.do_get(info.endpoints[0].ticket).read_all()
print(nodes.num_rows, nodes.column_names[:3])
ticket = {"dataset": "survey", "generation": None, "part": "relationships"}
relationships = client.do_get(flight.Ticket(json.dumps(ticket))).read_all()
print(relationships.column("type").to_pylist())
running.shutdown()
{'dataset': 'survey', 'generation': 1, 'nodes': 7, 'relationships': 6}
7 ['key', 'node_label', 'identity']
['HAS_ENTRY_POINT', 'REFERENCES_QUESTION', 'HAS_CONSTRUCT', 'HAS_CONSTRUCT', 'USES_CODELIST', 'HAS_CATEGORY']
read_all gives a pyarrow.Table. Its to_pandas() turns it into a
DataFrame, and Polars and DuckDB read it as it is. To handle one batch at a
time, loop over the reader instead: each item's data is a record batch.
ddigraph.flight.ticket(dataset, generation, part) writes a ticket's JSON
for you, and ddigraph.flight.server(store, host=..., port=...) makes a
server, as above. Call its serve() to wait until it stops.