Building pipelines
This page explains how the parts of the library are meant to be combined. The reference pages describe each class on its own. This page describes the whole.
What a pipeline is made of
There are two axes of composition. One for the pipeline as a whole and one for each source to sink chain.
A TaskGroup sequences work. Its tasks run in the order given, and a group can contain other groups.
A pipeline is a tree of tasks with a group at the root, started by calling execute() on the root.
Statistics from each TaskReturn are merged upwards, so grouping also decides how the reporting reads.
A chain of BatchProcessor instances moves data within one task.
Each processor pulls batches from its predecessor: a source at the start, optional stages such as validation in the middle, a sink at the end.
The chain is how data flows in batches from the source to the sink. A chain is implemented as a Task.
Use a group to order tasks. Use a processor chain to stream one load. The load tasks described below already assemble the chain for you.
How a load task works
For each source format there is a ready made task, for example CSVLoad2Neo4jTask. Building a load means subclassing it and filling in two things: the Cypher that writes a batch, and optionally a Pydantic model that validates each row. The SQL task is the exception: it implements _sql_query() and _cypher_query() instead of _query(), and takes no validation model.
class LoadStopsTask(CSVLoad2Neo4jTask): # (1)
class Stop(BaseModel): # (2)
id: str = Field(alias="stop_id")
name: str = Field(alias="stop_name")
def __init__(self, context: ETLContext, file: Path):
super().__init__(context, file, model=LoadStopsTask.Stop, batch_size=1000) # (3)
def _query(self): # (4)
return """
UNWIND $batch AS row
MERGE (s:Stop {id: row.id})
SET s.name = row.name
"""
Subclass the task matching the source format.
The Pydantic model is optional. Rows that fail validation are written to the error file and do not reach the database. See Validation.
File, Pydantic model and
batch_size. The error file location comes from theETL_ERROR_PATHcontext setting or the input file name, see Validation.The Cypher query is the mapping. It is written as a literal next to the task, and it operates on
$batch, the list of rows for the current batch.
Where does the logic live
When a row is not in the shape the graph needs, there are three places to fix it. In order of preference:
In the Cypher query. Deduplication, lookups, defaults that depend on graph state.
In the Pydantic model. Row shape, types, aliases, defaults.
In a custom
BatchProcessorstage. Only when neither of the above can express it.
Loading in the right order
A relationship task that MATCH``es nodes created by other tasks must run after them.
Otherwise the ``MATCH finds nothing and the row writes nothing, without an error.
A task that creates or merges the nodes in the same statement has no such constraint and can write nodes and relationships in one go.
The gtfs example uses separate groups: one for the node files, one for the relationship files:
context = ETLContext(env_vars=dict(os.environ))
setup_logging()
nodes = TaskGroup(context=context, name="nodes",
tasks=[LoadStopsTask(context, stops_file),
LoadRoutesTask(context, routes_file)])
edges = TaskGroup(context=context, name="relationships",
tasks=[LoadStopTimesTask(context, stop_times_file)])
pipeline = TaskGroup(context=context, name="import", tasks=[nodes, edges])
context.reporter.register_tasks(pipeline)
result = pipeline.execute()
Two calls precede execute(). setup_logging() configures logging to the console and an optional file. register_tasks() registers the task tree with the reporter. Without them the pipeline still runs, but there is no run record and no logging beyond the defaults, see Reporting.
Reporting goes to the log by default. When REPORTER_DATABASE is set, each run is additionally persisted in a Neo4j database, see Reporting.
A complete example of this shape, including the click based CLI around it, is the gtfs example.
Loading relationships
Relationships that share nodes can not be written in parallel. Concurrent transactions lock the same nodes and deadlock. Parallel Processing describes the mix and batch strategy that works around this.
Testing a pipeline
The library ships its own test support. etl_lib.test_utils provides the fixtures and mocks to stand up an ETLContext against a test database, including a throwaway database per run via NEO4J_TEST_DATABASE. See Testing.