Skip to content

Logical targets

A logical target (RML-IO) sends part of the output to a file of its own, with its own format and compression. Use it to split one mapping run over several files.

Declaring a target

rml:logicalTarget goes on a subject, predicate, object or graph map. Every triple that the term map takes part in is written to that target.

@prefix rml:     <http://w3id.org/rml/> .
@prefix formats: <http://www.w3.org/ns/formats/> .

rml:subjectMap [
  rml:template "http://example.com/{id}" ;
  rml:logicalTarget [
    rml:target [ a rml:FilePath ; rml:root rml:MappingDirectory ; rml:path "out/people.nt" ] ;
    rml:serialization formats:N-Triples ;
    rml:compression rml:gzip ] ] .
Property Values When omitted
rml:target An rml:FilePath required
rml:serialization formats:N-Triples, formats:N-Quads, formats:Turtle, formats:TriG, formats:N3, formats:JSON-LD, formats:RDF_XML formats:N-Quads
rml:compression rml:gzip, rml:zip, rml:none no compression
rml:encoding rml:UTF-8, rml:UTF-16 UTF-8

Only file targets are supported.

Running a mapping with targets

From the CLI

Nothing to configure. When a mapping declares targets, the CLI writes them. Triples without a target go to -o, or to standard output. A relative target path is resolved against the working directory, or against the mapping's directory when the target has rml:root rml:MappingDirectory.

From the library

The library needs a TargetRouter, which you build from one TargetWriter per target:

Path mappingFile = Paths.get("mapping.ttl");
Mapping mapping = Mapping.of(RDFFormat.TURTLE, mappingFile);

TargetWriterFactory writerFactory = TargetWriterFactory.builder()
    .mappingDirectoryPath(mappingFile.toAbsolutePath().getParent())
    .build();

Map<LogicalTarget, TargetWriter> writers = mapping.getTriplesMaps().stream()
    .flatMap(triplesMap -> triplesMap.getAllLogicalTargets().stream())
    .distinct()
    .collect(Collectors.toMap(target -> target, target -> writerFactory.createFileWriter(
        (FilePath) target.getTarget(),
        target.getSerialization(),
        target.getEncoding(),
        target.getCompression())));

try (TargetRouter router = new TargetRouter(writers, null)) {
    RdfRmlMapper mapper = RdfRmlMapper.builder()
        .mapping(mapping)
        .targetRouter(router)
        .build();

    mapper.map().blockLast();
}

The second constructor argument is a writer for triples that have no target. With null the router skips them.

Three things to know:

  • map() still emits every statement. Writing to targets happens on the side.
  • Only map() and mapToModel() write to targets. mapToNTriplesBytes() and mapToNQuadsBytes() do not.
  • The router does not close itself when the mapping completes, hence the try-with-resources.