Running Tentris Using Java
Requirements
Java>=11org.eclipse.rdf4j>=5.3.0(last tested with5.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;
import java.io.IOException;
import java.net.CookieManager;
import java.net.HttpCookie;
import java.net.URI;
import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) throws IOException, InterruptedException {
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");
String cookie = login(baseUrl, "my-username", "my-password");
// This replaces setUsernameAndPassword()
Map<String, String> headers = new HashMap<>();
headers.put("Cookie", cookie);
repo.setAdditionalHttpHeaders(headers);
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();
}
}
private static String login(String baseUrl, String username, String password) throws IOException, InterruptedException {
CookieManager cm = new CookieManager();
HttpClient client = HttpClient.newBuilder().cookieHandler(cm).build();
String formData = String.format("username=%s&password=%s", URLEncoder.encode(username, StandardCharsets.UTF_8), URLEncoder.encode(password, StandardCharsets.UTF_8));
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(baseUrl + "/login"))
.header("Content-Type", "application/x-www-form-urlencoded")
.POST(HttpRequest.BodyPublishers.ofString(formData))
.build();
client.send(request, HttpResponse.BodyHandlers.discarding());
HttpCookie cookie = cm.getCookieStore().getCookies().stream().filter(c -> c.getName().equals("tentris")).findFirst().get();
return String.format("%s=%s", cookie.getName(), cookie.getValue());
}
}