Large inputs¶
Three settings decide how CARML copes with an input that does not fit in memory: the evaluator, spill-to-disk, and the way you take the output.
Pick the evaluator¶
The DuckDB evaluator runs a view, joins included, as one query in an in-process database. The reactive evaluator streams the source of a run with one triples map and no join. With more than one triples map, or any join, it keeps the records of every source in memory.
For large CSV or SQL inputs, and for any large join, use DuckDB. JSON has its own section below. In the CLI that is the default wherever DuckDB
can run the view. In the library, add carml-logical-view-duckdb to the classpath.
The plan command reads a mapping and recommends an evaluator per triples map. See
the CLI page.
Large JSON files¶
DuckDB reads a JSON file of up to 50 MB as one value, in memory. A larger file is first copied to newline-delimited
JSON, one record per line, and DuckDB streams that copy. With --spill-to-disk the copy goes where DuckDB's
temporary files go, and to the JVM's temporary directory otherwise. Leave room there for a second copy of the file.
The copy is only made when the iterator walks to an array by property names, such as $[*] or $.orders[*], and the
records are JSON objects. With a recursive descent (..), a slice or a union in the iterator, DuckDB reads the whole
file in memory whatever its size.
JSON needs more memory than its size suggests, and more than DuckDB's memory limit. A 1 GB file with nested arrays failed with a 256 MB limit, and completed with a 1 GB limit while the process used 5.6 GB. The reactive evaluator streams a JSON file only in a run of one triples map without joins. For a JSON file much larger than memory, split the file, or export CSV, which DuckDB spills.
Spill to disk¶
CLI¶
--spill-to-disk changes both evaluators:
- DuckDB uses a database file instead of memory, and sets its memory limit and thread count from what the machine
has free.
--in-process-db-memory=4GBsets the memory limit yourself. - Reactive runs a join in DuckDB once the parent side has more than
--reactive-spill-thresholdrows. The default is 50000. Smaller joins stay in memory. This applies with-E autoand-E reactive.
Both options are ignored without --spill-to-disk.
The database file and the reactive join data go to the JVM's temporary directory. DuckDB's own temporary files go to
/carml-spill when that directory exists, as it does in the Docker image, and next to the database file otherwise.
In a container, give both places enough room. With an image built from the
carml-jar Dockerfile, whose working directory is /data:
docker run -v "$PWD":/data -v /big/disk:/carml-spill carml map --spill-to-disk -m mapping.ttl -o out.nq
Library¶
try (var duckDb = DuckDbLogicalViewEvaluatorFactory.createOnDisk()) {
RdfRmlMapper mapper = RdfRmlMapper.builder()
.mapping(mapping)
.logicalViewEvaluatorFactory(duckDb)
.joinExecutorFactory(new DuckDbJoinExecutorFactory(50_000, true, spillDirectory))
.build();
// run the mapping
}
DuckDbLogicalViewEvaluatorFactory is in carml-logical-view-duckdb. DuckDbJoinExecutorFactory is in
carml-logical-view-join-duckdb. Its arguments are the row threshold, whether to spill to a file, and the directory
for that file. Registering either one switches off classpath discovery of evaluators. With both registered as above,
DuckDB takes the views it can run and the reactive evaluator, with the spillable join, takes the rest.
Take the output as a stream¶
- In the library, use
map()or the byte methods, notmapToModel(). See Streaming the result. - In the CLI, write N-Triples or N-Quads.
-P(pretty printing) collects the whole output in memory first. - For eligible mappings the CLI skips the engine and lets DuckDB write the file. See Direct serialization.
Deduplication¶
--dedup full keeps every distinct statement in memory. On large outputs, prefer annotations on your views or
deduplicating the output file afterwards. See Deduplication.