Bulk Import and Large Files¶
This page is for big jobs. It covers two things:
- Bulk import.
ddigraph export --format neo4j-importwrites the files thatneo4j-admin database import fullreads. The import builds a new database straight from them, with no transactions. - Large files. A load, an export or a preview reads a file of any size in a bounded amount of memory. This part says what the memory limit does, what goes to disk, and what the benchmarks show.
Bulk import with neo4j-admin¶
ddigraph load writes through Cypher, in one transaction per chunk, and it
merges each node on its key. That is what you want to update a database.
To build a new one from a large file, use neo4j-admin database import
full, which Neo4j calls its high-speed initial import. It writes the
database files directly, with no transactions and no lookups. But it can
only create a database, or overwrite one that is stopped.
Export the files¶
$ ddigraph export survey.xml --format neo4j-import -o survey-import
Wrote survey-import
Format: neo4j-import
Nodes: 7 Relationships: 6
$ ls survey-import
import.sh
nodes.csv
relationships.csv
The files hold the same graph ddigraph load writes, down to each
property's type. A test imports every test fixture this way, starts the
database, and checks that it holds the same nodes, labels, properties and
relationships as a load of the same files.
| File | What it holds |
|---|---|
nodes.csv |
One row per node: key:ID, its labels in :LABEL (DDINode, its type label, and Unresolved on a stub), identity, content_hash or a stub's unresolved_reason, the Dataset node's own source, flavor and line, and a typed column for each property, such as line:long or fielded_languages:string[] |
relationships.csv |
One row per relationship: :START_ID, :END_ID, :TYPE, ordinal and resolution. It starts with an IN_DATASET edge from each node to the Dataset node, with the node's source, flavor and line. Then come the file's relationships, HAS_ENTRY_POINT included |
import.sh |
The neo4j-admin command that imports the two files |
A text property p is two columns, p and p_lang, as in Neo4j. A list
is a string array. The labels and the list items are joined with the
character U+001F. XML does not allow that character. But ddigraph reads
past XML errors by default, so it reads  as U+001F. The export
refuses a list item that holds it, since the import would split the item
in two. A text may hold a line break, so the import needs
--multiline-fields.
Unlike the csv and json formats, this one writes the rows as the file is
read. Its columns come from the mapping spec, not from the data. So the
export uses as little memory as a load, however large the file.
$ cat survey-import/import.sh
#!/bin/sh
# Import the graph in this folder into a new Neo4j 5 database with neo4j-admin.
# Written by ddigraph export --format neo4j-import.
# Usage: sh import.sh [DATABASE] (default: neo4j)
start=$PWD
cd "$(dirname "$0")" || exit 1
exec neo4j-admin database import full \
--nodes=nodes.csv \
--relationships=relationships.csv \
--report-file="$start/import.report" \
--array-delimiter=U+001F --multiline-fields=true \
"${1:-neo4j}"
The script first moves to its own folder, since neo4j-admin reads an =
or a , in a --nodes path as a label or a second file. The import report
still goes to the folder you run the script from.
neo4j-admin is in the bin folder of the Neo4j install. On Windows, run
the same command with bin\neo4j-admin.bat, from the folder that holds the
files:
neo4j-admin.bat database import full --nodes=nodes.csv --relationships=relationships.csv --array-delimiter=U+001F --multiline-fields=true green
A blue/green switch¶
Neo4j Community Edition serves one user database at a time. So you cannot
create a second database and move to it while the first one serves. You
can still switch with only a restart. Call the database that serves now
blue (its name is neo4j on a new install), and the new one green:
-
Import green while blue serves. Run the import into a new database name.
neo4j-admincan do this while the server runs. Run it as the user that runs Neo4j, so that user owns the new files:$ sh survey-import/import.sh green ... IMPORT DONE in 1s 471ms. Imported: 7 nodes 12 relationships 86 properties Peak memory usage: 517.6MiBThe 12 relationships are the file's 6, plus an
IN_DATASETedge for each node but the Dataset node. -
Switch. In
conf/neo4j.conf, set the default database to green, then restart Neo4j:initial.dbms.default_database=greenGreen now serves. Blue stays on disk, stopped:
$ cypher-shell -u neo4j -p password --format plain "SHOW DATABASES YIELD name, currentStatus, default" name, currentStatus, default "green", "online", TRUE "neo4j", "unknown", FALSE "system", "online", FALSE -
Point ddigraph at green. Each ddigraph command names its database,
neo4jby default, so set it to the new name. Then create the key constraint and the indexes, whichneo4j-admindoes not create:$ export DDIGRAPH_NEO4J_DATABASE=green $ ddigraph bootstrap Schema ensured for database 'green'Without that setting, a command fails with
Database neo4j not found. -
Roll back if you need to. Set the default database back to
neo4jand restart. Blue serves again, as it was.
For the next round, import into the stopped database, the old blue, with
--overwrite-destination. neo4j-admin refuses to overwrite the database
that serves:
The database is in use. Stop database 'neo4j' and try again.
With Docker, copy the folder into the container, run the script there with
docker exec, edit /var/lib/neo4j/conf/neo4j.conf the same way, and run
docker restart. Enterprise Edition can serve both databases at once, so
there you can create green with CREATE DATABASE and switch with an alias
instead of a restart.
What to know¶
- One file per database. An import builds a whole database, so it holds
the graph of the files you give it. To add more files, load them with
ddigraph loadonce green serves; they merge as usual. You can also import several export folders in one command, by giving each folder's--nodesand--relationships, with no=or,in the paths. This works only when no two files hold the same node. A node two files share makes the import fail. - No load marker. The import writes no
:DDILoadmarker. So the first load through the store of that dataset writes it whole, once. - A first load with --fresh. For a file too small to need
neo4j-admin,ddigraph load FILE --store --freshwrites into an empty database withCREATE, which skips the lookup a plain load makes for each node. - Lists hold strings. A list property becomes a string array. Every list a DDI file gives is a list of strings, so this only matters for a stream you build yourself. The export refuses a list of numbers rather than change its type.
Large files¶
ddigraph 0.6 reads a DDI file of any size in a bounded amount of memory. It reads the whole file before the first chunk comes out. While it reads, it sorts what it has seen, to settle keys and references. A sort that goes past its share of memory writes the rest to temporary files.
The memory limit¶
--memory-limit MB (DDIGRAPH_MEMORY_LIMIT_MB, memory_limit_mb) sets the
memory a load may use for sorting. It defaults to 256 MB and must be at
least 16. load, export, preview and store add take it.
ddigraph load big-survey.xml --memory-limit 64
DDIGRAPH_MEMORY_LIMIT_MB=64 ddigraph export big-survey.xml --format neo4j-import -o big-import
The parse runs at most two sorts at once. Each gets 16 MB or half the
limit, whichever is less. So a limit above 32 MB leaves the parse as it is.
The limit matters on a small machine, and for the store: store add and
load --store give half the limit to the parse and half to the store's own
sorts.
The limit covers the sorts, not the whole process. Python, lxml and the rest of ddigraph take memory too: the benchmarks below peak at about 70 MB with the default limit.
Temporary files¶
What goes past its share of memory is written to ddigraph-parse-* and
ddigraph-sort-* folders in the system temporary folder: TMPDIR, or
TEMP or TMP on Windows. The folders grow with the number of elements,
at about 1 KB each at their peak. For the benchmark files of 200,000
elements, the two kinds together peaked at:
| File | Size | Temporary files at peak |
|---|---|---|
| DDI-L | 50 MB | 189 MB, 3.8 times the file |
| DDI-CDI | 105 MB | 221 MB, 2.1 times the file |
| Codebook | 13 MB | 249 MB, 19 times the file |
A Codebook file with short elements needs many times its own size. The
folders are removed when the parse ends, or when it stops on an error. A
process that is killed leaves them behind, so delete any old
ddigraph-parse-* and ddigraph-sort-* folders by hand. Make sure the
temporary folder has room, or point TMPDIR at a disk that does.
The store's own sorts spill into the new generation's folder instead.
One table still grows with the file: about 200 bytes for each Codebook
element with no ID at the top level, such as a <var> with no ID. A
million of them take about 200 MB. Elements with an ID, and every element of
DDI-L and DDI-CDI, cost nothing there.
Benchmarks¶
ddigraph.bench writes synthetic DDI files and times how ddigraph reads
them:
python -m ddigraph.bench run --before 0.5.1
For each flavor and size (by default Codebook, DDI-L and DDI-CDI, at 50,000
to 400,000 elements), it writes a valid file. It reads the file with
ddigraph.ir.parse() in a fresh process, and records the wall time and the
peak memory. It then checks them against benchmarks/budgets.toml, and
exits with 1 if a budget fails. --before 0.5.1 also measures that
release from PyPI, in a throwaway environment; it needs
uv. The runner needs Unix: it does not run on Windows.
Run it from a clone of the repository, or pass --budgets FILE.
--engine names the engines that read each file: lxml, the default,
rust, or both, as in --engine lxml rust. The Rust engine needs
ddigraph-core (see The Rust engine). Each engine reads
each file in a fresh process of its own, and lxml goes first. The budgets
hold for lxml, so only its runs are checked. With both engines, each Rust
run also records how many times as fast it was (speedup) and its peak
over the lxml peak (peak_ratio). An older build of ddigraph-core may not
read a flavor, such as Codebook. That flavor is then skipped with a note,
and it does not count as a failure.
These are the numbers of one run, on a Linux machine with 4 CPUs, Python 3.13 and the default 256 MB limit, while other work ran beside it. Between two runs here, a time changed by up to half; memory changed far less.
| Flavor | Elements | 0.6 time | 0.6 peak | 0.5.1 time | 0.5.1 peak |
|---|---|---|---|---|---|
| Codebook | 50,000 | 8.8 s | 66 MB | 8.8 s | 84 MB |
| Codebook | 100,000 | 16.6 s | 69 MB | 10.2 s | 88 MB |
| Codebook | 200,000 | 30.1 s | 66 MB | 20.2 s | 102 MB |
| Codebook | 400,000 | 62.6 s | 69 MB | 42.5 s | 132 MB |
| DDI-L | 50,000 | 7.5 s | 75 MB | 2.4 s | 113 MB |
| DDI-L | 100,000 | 15.1 s | 74 MB | 4.7 s | 153 MB |
| DDI-L | 200,000 | 30.8 s | 73 MB | 9.5 s | 231 MB |
| DDI-L | 400,000 | 63.6 s | 70 MB | 24.1 s | 393 MB |
| DDI-CDI | 50,000 | 13.1 s | 70 MB | ||
| DDI-CDI | 100,000 | 21.5 s | 74 MB | ||
| DDI-CDI | 200,000 | 41.0 s | 73 MB | ||
| DDI-CDI | 400,000 | 80.3 s | 73 MB |
ddigraph 0.5.1 reads none of the elements of these DDI-CDI files: they follow the DDI-CDI XML schema, which 0.5.1 does not read. So it has no time to compare there. For Codebook and DDI-L, 0.5.1 is timed on a dry-run load. The two versions also build somewhat different graphs, so compare the trends more than single numbers.
Every budget in benchmarks/budgets.toml holds:
- the peak at 400,000 elements is at most 450 MB (it is 69 to 73 MB);
- the peak at four times the elements is at most 1.10 times the peak (it is 0.95 to 1.04 times);
- the time for twice the elements is at most 2.4 times the time (it is 1.6 to 2.1 times).
Memory stays flat: the peak at 400,000 elements is the same as at 50,000. ddigraph 0.5.1 kept every node in memory, so its peak grew with the file, to 393 MB for DDI-L. The cost is speed. Sorting on disk makes 0.6 slower than 0.5.1 at 400,000 elements: 1.5 times on Codebook and 2.6 times on DDI-L. The Rust engine, below, reads much faster, at a cost in memory.
The Rust engine¶
python -m ddigraph.bench run --engine lxml rust gave these numbers in one
run, on the same kind of machine, with Python 3.14 and a release build of
ddigraph-core, while other work ran beside it:
| Flavor | Elements | lxml time | Rust time | Times as fast | lxml peak | Rust peak |
|---|---|---|---|---|---|---|
| Codebook | 50,000 | 6.3 s | 1.1 s | 5.8 | 71 MB | 183 MB |
| Codebook | 100,000 | 12.6 s | 1.8 s | 7.1 | 74 MB | 314 MB |
| Codebook | 200,000 | 24.4 s | 3.4 s | 7.1 | 73 MB | 581 MB |
| Codebook | 400,000 | 49.1 s | 6.5 s | 7.5 | 79 MB | 1,115 MB |
| DDI-L | 50,000 | 6.1 s | 1.2 s | 5.2 | 80 MB | 183 MB |
| DDI-L | 100,000 | 11.8 s | 2.0 s | 5.8 | 80 MB | 314 MB |
| DDI-L | 200,000 | 25.1 s | 3.8 s | 6.6 | 76 MB | 571 MB |
| DDI-L | 400,000 | 47.4 s | 7.5 s | 6.4 | 77 MB | 1,091 MB |
| DDI-CDI | 50,000 | 7.9 s | 1.2 s | 6.5 | 77 MB | 191 MB |
| DDI-CDI | 100,000 | 15.4 s | 2.3 s | 6.6 | 80 MB | 327 MB |
| DDI-CDI | 200,000 | 32.0 s | 4.4 s | 7.2 | 81 MB | 610 MB |
| DDI-CDI | 400,000 | 63.1 s | 8.7 s | 7.3 | 83 MB | 1,172 MB |
Each time counts the start of the process, which loads Python and
ddigraph. Both engines give the same node counts for each file, and
ddigraph.conform checks that they
give the same graph.
The Rust engine is 5.2 to 7.5 times as fast as lxml here. It pays for this in memory. lxml keeps its peak near 80 MB at every size. The Rust engine holds the whole parse in memory, so its peak grows with the file: at 400,000 elements it is about 1.1 GB. That is about 5.6 times the size of the file for DDI-CDI, 11 times for DDI-L, and 43 times for Codebook, whose elements are short. Make sure the machine has that much memory free for the file you read. For a file that does not fit, use the lxml engine.