Use CARML as a library¶
1. Add dependencies¶
CARML is published to Maven Central under the io.carml group. Add the engine and one resolver per source format you
map.
<dependency>
<groupId>io.carml</groupId>
<artifactId>carml-engine</artifactId>
<version>${carml.version}</version>
</dependency>
<!-- one resolver per source format -->
<dependency>
<groupId>io.carml</groupId>
<artifactId>carml-logical-source-resolver-jsonpath</artifactId>
<version>${carml.version}</version>
</dependency>
| Format | Resolver module |
|---|---|
| JSON | carml-logical-source-resolver-jsonpath |
| XML | carml-logical-source-resolver-xpath |
| CSV | carml-logical-source-resolver-csv |
Resolvers register themselves through ServiceLoader. Databases need an evaluator module instead of a resolver.
See Evaluators.
2. Write a mapping¶
@prefix rml: <http://w3id.org/rml/> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
<TriplesMap1> a rml:TriplesMap ;
rml:logicalSource [ a rml:LogicalSource ;
rml:source [ a rml:RelativePathSource ;
rml:root rml:MappingDirectory ;
rml:path "student.json" ] ;
rml:referenceFormulation rml:JSONPath ;
rml:iterator "$.students[*]" ] ;
rml:subjectMap [ rml:template "http://example.com/{$.Name}" ] ;
rml:predicateObjectMap [
rml:predicate foaf:name ;
rml:objectMap [ rml:reference "$.Name" ] ] .
3. Run it¶
import io.carml.engine.rdf.RdfRmlMapper;
import io.carml.model.Mapping;
import java.nio.file.Paths;
import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.rio.RDFFormat;
Mapping mapping = Mapping.of(RDFFormat.TURTLE, Paths.get("mapping.ttl"));
RdfRmlMapper mapper = RdfRmlMapper.builder()
.mapping(mapping)
.build();
Model result = mapper.mapToModel();
Mapping.of(RDFFormat, Path...) remembers where the mapping file is. That is what lets
rml:root rml:MappingDirectory resolve student.json next to the mapping. It needs exactly one mapping file. The
Mapping.of overloads that take an InputStream or a Model have no file location, so use a different root with
those.
If you already have a Set<TriplesMap>, for example from RmlMappingLoader.build().load(...), pass it with
.triplesMaps(...) instead of .mapping(...).
Streaming the result¶
mapToModel() waits for the mapping to complete and collects every statement into an in-memory RDF4J Model, so use
it only for results that fit in memory.
For anything larger, map() returns a Flux<Statement>:
import org.eclipse.rdf4j.model.util.ModelCollector;
Flux<Statement> statements = mapper.map();
Model result = statements.collect(ModelCollector.toModel()).block();
mapToModel() has no timeout. To add one, apply it to map():
// fail if the whole mapping takes longer than ten minutes
Model withDeadline = mapper.map().collect(ModelCollector.toModel()).block(Duration.ofMinutes(10));
// fail only when no statement arrives for 30 seconds, for example on a source that hangs
Model withIdleLimit = mapper.map().timeout(Duration.ofSeconds(30)).collect(ModelCollector.toModel()).block();
To write N-Triples or N-Quads without building statement objects, use the byte methods:
Each byte[] is a chunk of about 64 KB, not one line. Set
byteChunkSize(0) on the builder if you need one array per line.
Builder options¶
Base IRI, source locations, functions, deduplication and the rest are set on the builder. They are all listed in Configuration.
Upgrading from an older CARML
setLogicalSourceResolver(...) is gone. Resolvers are found on the classpath. To leave one out, call
excludeLogicalSourceResolver("CsvResolver") with the resolver's name: CsvResolver, JsonPathResolver or
XPathResolver.