Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Running Tentris Using Java

Requirements

  • Java>=11
  • org.eclipse.rdf4j>=5.3.0 (last tested with 5.3.2)

Install

Add rdf4j as dependency to your Java project (e.g. in the pom.xml for Maven).

Getting Started

Compatibility with rdf4j is currently limited to the SPARQLRepository HTTP-connector.

import org.eclipse.rdf4j.repository.sparql.SPARQLRepository;
import org.eclipse.rdf4j.repository.RepositoryConnection;
import org.eclipse.rdf4j.query.TupleQuery;
import org.eclipse.rdf4j.query.TupleQueryResult;
import org.eclipse.rdf4j.query.BindingSet;

public class Main {
    public static void main(String[] args) {
        String queryUrl = "https://dbpedia.data.dice-research.org/sparql";

        // Note: updates are also supported (on servers that have them enabled).
        SPARQLRepository repo = new SPARQLRepository(queryUrl);

        repo.init();

        try (RepositoryConnection conn = repo.getConnection()) {
            TupleQuery query = conn.prepareTupleQuery("SELECT * WHERE { ?s ?p ?o } LIMIT 10");
            try (TupleQueryResult result = query.evaluate()) {
                while (result.hasNext()) {
                    BindingSet solution = result.next();
                    System.out.printf("%s %s %s .\n", solution.getValue("s"), solution.getValue("p"), solution.getValue("o"));
                }
            }
        } finally {
            repo.shutDown();
        }
    }
}

For more information about setting up a disk-based instance of Tentris, please refer to the chapter Binary.

Remote Login

To log into a remote instance of Tentris that has authentication enabled, the following can be used.

import org.eclipse.rdf4j.query.BindingSet;
import org.eclipse.rdf4j.query.TupleQuery;
import org.eclipse.rdf4j.query.TupleQueryResult;
import org.eclipse.rdf4j.repository.RepositoryConnection;
import org.eclipse.rdf4j.repository.sparql.SPARQLRepository;

public class Main {
    public static void main(String[] args) {
        String baseUrl = "https://remote-server-with-auth.example.com";

        // Note: updates are also supported (on servers that have them enabled).
        SPARQLRepository repo = new SPARQLRepository(baseUrl + "/sparql");
        repo.setUsernameAndPassword("my-username", "my-password");
        repo.init();

        try (RepositoryConnection conn = repo.getConnection()) {
            TupleQuery query = conn.prepareTupleQuery("SELECT * WHERE { ?s ?p ?o } LIMIT 10");
            try (TupleQueryResult result = query.evaluate()) {
                while (result.hasNext()) {
                    BindingSet solution = result.next();
                    System.out.printf("%s %s %s .\n", solution.getValue("s"), solution.getValue("p"), solution.getValue("o"));
                }
            }
        } finally {
            repo.shutDown();
        }
    }
}