Skip to main content
Version: 0.7.x

Tutorial: your first Python SDK pipeline

Build a streaming pipeline inside a Python process, run it two ways, and export it for deployment on arkflow-server. Takes about 15 minutes.

Prerequisites. Python 3.9+ in a virtual environment; no cluster and no other services required.

1. Install the SDK​

python3 -m venv .venv && source .venv/bin/activate
pip install arkflow

First import emits an experimental warning — expected.

2. Build and sample the pipeline​

Create pipeline.py:

import arkflow

ROWS = [
{"user": "a", "amount": 10},
{"user": "a", "amount": 32},
{"user": "b", "amount": 25},
{"user": "b", "amount": 7},
]

flow = (
arkflow.Pipeline()
.from_iter(ROWS)
.sql("SELECT user, SUM(amount) AS total FROM flow GROUP BY user")
.to_stdout()
)

# Bounded sampling: up to 3 batches, then cancel.
for batch in flow.take(3):
print("batch:", batch)

Expected result: two batches (one per aggregate group), printed as Arrow PyCapsule objects. take is sampling semantics — offsets are not tracked.

3. Interoperate with Polars and DuckDB​

Add to the script:

import polars, duckdb

for batch in flow.take(3):
df = polars.DataFrame(batch)
print(df)
print(duckdb.sql("SELECT user, total FROM batch ORDER BY total DESC").fetchall())

Expected result: a Polars DataFrame per batch and DuckDB rows ordered by total — no pyarrow installed anywhere.

4. Run to completion​

Replace the sampling calls with:

flow.run()   # blocks until end-of-stream; Ctrl-C cancels gracefully

Expected result: each group's totals printed once by the stdout sink. run() is the only primitive with end-to-end delivery semantics; running it twice is fine — the pipeline rebuilds per execution.

5. Inspect errors​

Break something on purpose — drop the sink and run:

broken = arkflow.Pipeline().from_iter(ROWS)
try:
broken.run()
except arkflow.ConfigError as error:
print("config error:", error)

Expected result: ConfigError naming the missing sink. The interpreter survives engine errors; UDF tracebacks arrive inside ExecutionError.

6. Graduate to a deployable config​

Swap the in-memory source for a connector and export:

deployable = (
arkflow.Pipeline()
.from_kafka(brokers=["localhost:9092"], topics=["payments"], group_id="tut")
.sql("SELECT user, SUM(amount) AS total FROM flow GROUP BY user")
.to_stdout()
)
print(deployable.to_yaml())

Expected result: a YAML config you can validate and run standalone:

arkflow --config flow.yaml --validate
arkflow --config flow.yaml

A map_batches callable exports as a python processor placeholder; restore it in-process with arkflow.run_yaml("flow.yaml", python_processors={"fn": fn}).

What you learned​

  • The builder compiles to the same config the binary consumes.
  • take/collect are sampling; run() is the durable path.
  • Batches are Arrow PyCapsules — Polars/DuckDB interop without pyarrow.
  • to_yaml graduates a connector-backed pipeline to deployment.

Next steps​