Skip to content

Titanium RDF API integration

The jelly-titanium-rdf-api module integrates Jelly with the minimalistic Titanium RDF API. This API is by itself not a fully-fledged RDF library, but is rather intended as an interoperability bridge.

If you are already using Jena or RDF4J, you should use the jelly-jena or jelly-rdf4j module instead. This way you'll get better performance and more features.

Installation

jelly-titanium-rdf-api has a pure Java API and is available on Maven Central. To include it in your project, add the following dependency to your pom.xml:

<dependency>
    <groupId>eu.ostrzyciel.jelly</groupId>
    <artifactId>jelly-titanium-rdf-api_3</artifactId>
    <version>2.9.1</version>
</dependency>

Note that while the API is in Java, the code does depend on Scala. The dependency on Scala is expected to be removed in Jelly-JVM 3.0.0.

Basic I/O operations

The module implements a simple Jelly file reader and writer for Titanium. See the classes eu.ostrzyciel.jelly.titanium.JellyTitaniumReader and eu.ostrzyciel.jelly.titanium.JellyTitaniumWriter . You should simply instantiate them using the .factory static method and then either push quads into the writer, or instruct the reader to push quads into another quad consumer.

Full example of integration with the titanium-rdf-n-quads library:

Example: TitaniumRdfApi.java (click to expand)

Source code on GitHub

TitaniumRdfApi.java
package eu.ostrzyciel.jelly.examples;

import com.apicatalog.rdf.nquads.NQuadsReader;
import com.apicatalog.rdf.nquads.NQuadsWriter;
import eu.ostrzyciel.jelly.convert.titanium.TitaniumJellyReader;
import eu.ostrzyciel.jelly.convert.titanium.TitaniumJellyWriter;
import eu.ostrzyciel.jelly.examples.shared.Example;

import java.io.*;
import java.net.URISyntaxException;

public class TitaniumRdfApi implements Example {
    public static void main(String[] args) {
        try {
            new TitaniumRdfApi().run(args);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void run(String[] args) throws Exception {
        // Obtain the input file name (Jelly) and output file (N-Quads)
        var inputFile = new File(getClass().getResource("/jelly/weather.jelly").toURI());
        var nquadsOutput = new File("weather-titanium.nq");

        System.out.println("Converting " + inputFile + " to " + nquadsOutput + " ...");

        // Open the I/O streams
        try (
            var fis = new FileInputStream(inputFile);
            var fos = new FileWriter(nquadsOutput)
        ) {
            var jellyReader = TitaniumJellyReader.factory();
            // Parse the entire Jelly file and immediately write the N-Quads to the output file
            jellyReader.parseAll(new NQuadsWriter(fos), fis);
        }

        System.out.println("Conversion complete, N-Quads file saved.");

        // Now let's try the reverse – parse an N-Quads file and write it as Jelly
        var nquadsInput = new File("weather-titanium.nq");
        var jellyOutput = new File("weather-titanium.jelly");

        System.out.println("Converting " + nquadsInput + " to " + jellyOutput + " ...");

        // Open the I/O streams
        try (
            var fis = new FileReader(nquadsInput);
            var fos = new FileOutputStream(jellyOutput);
            // IMPORTANT: the Jelly writer must be closed before the output stream is closed,
            // otherwise the Jelly file will be incomplete. You can also do this manually with
            // the jellyWriter.close() method.
            var jellyWriter = TitaniumJellyWriter.factory(fos)
        ) {
            // Parse the entire N-Quads file and immediately write the Jelly to the output file
            new NQuadsReader(fis).provide(jellyWriter);
        }

        System.out.println("Conversion complete, Jelly file saved.");
    }
}

Low-level usage

Titanium RDF API does not implement types for RDF primitives, so the Jelly integration with it is a bit different from the ones for Jena and RDF4J. Currently, the Pekko Streams API is not supported, and the ConverterFactory for Titanium is not part of the public API.

But, you can still access a part of the low-level API directly. This would be useful if you wanted to integrate Titanium with Kafka or some other custom serialization pipeline.

To do this, use the eu.ostrzyciel.jelly.titanium.JellyTitaniumDecoder and eu.ostrzyciel.jelly.titanium.JellyTitaniumEncoder classes directly.

Integrations

Jelly-JVM implements the RdfQuadConsumer interface, so you can hook it up to any library that does the same. This includes formats like: JSON-LD, CBOR-LD, N-Quads.

See also