4

I am trying to use Jena to write to a local free standalone GraphDB (version 8.5.0) repository.

What I have tried

(1) Direct use from Jena

I used this Jena 3.7.0 code snippet:

String strInsert =  
  "INSERT DATA {"
    + "<http://dbpedia.org/resource/Grace_Hopper> " 
    + "<http://dbpedia.org/ontology/birthDate>" 
    + " \"1906-12-9\"^^<http://www.w3.org/2001/XMLSchema#date> .}";

UpdateRequest updateRequest = UpdateFactory.create(strInsert);

UpdateProcessor updateProcessor = UpdateExecutionFactory.createRemote(updateRequest, 
  "http://localhost:7200/repositories/PersonData");

updateProcessor.execute();

which results in the following exception

org.apache.jena.atlas.web.HttpException: 415 - 
at org.apache.jena.riot.web.HttpOp.exec(HttpOp.java:1091)
at org.apache.jena.riot.web.HttpOp.execHttpPost(HttpOp.java:718)
at org.apache.jena.riot.web.HttpOp.execHttpPost(HttpOp.java:501)
at org.apache.jena.riot.web.HttpOp.execHttpPost(HttpOp.java:459)
at org.apache.jena.sparql.modify.UpdateProcessRemote.execute(UpdateProcessRemote.java:81)
at org.graphdb.jena.tutorial.SimpleInsertQueryExample.main(SimpleInsertQueryExample.java:91)

On the GraphDB side I get the following error:

[INFO ] 2018-06-29 11:33:05,605 [repositories/PersonData | o.e.r.h.s.ProtocolExceptionResolver] Client sent bad request ( 415)
org.eclipse.rdf4j.http.server.ClientHTTPException: Unsupported MIME type: application/sparql-update

(2) GraphDB via Jena Fuseki

As an alternative I explored the GraphDB documentation, which states that it is possible to access GraphDB using the Jena Joseki, now Fuseki, server. But for that Fuseki needs to be configured to read the GraphDB as a Jena dataset and then accessed via a Ontotext Jena adapter com.ontotext.jena.SesameDataset. But I can find no GraphDB libraries that inlude this class.

(3) Accessing GraphDB using RDF4J

Accessing GraphDB from RDF4J works without issues:

Repository repository = new HTTPRepository(GRAPHDB_SERVER, REPOSITORY_ID);
repository.initialize();
RepositoryConnection repositoryConnection = repository.getConnection();
repositoryConnection.begin();

Update updateOperation = repositoryConnection.prepareUpdate(QueryLanguage.SPARQL, strInsert);
updateOperation.execute();

try {
  repositoryConnection.commit();
} catch (Exception e) {
  if (repositoryConnection.isActive())
    repositoryConnection.rollback();
}

My Question

Is there a way to access GraphDB efficiently from Jena? I have seen this related SO question, but I was hoping for a better approach.

Henriette Harmse
  • 4,167
  • 1
  • 13
  • 22

2 Answers2

5

GraphDB implements standard SPARQL 1.1 endpoints according the RDF4J protocol.

Try changing your code to point to the update endpoint:

UpdateProcessor updateProcessor = UpdateExecutionFactory.createRemote(updateRequest, 
    "http://localhost:7200/repositories/PersonData/statements");

The Jena adapter to GraphDB is no longer supported.

vassil_momtchev
  • 1,173
  • 5
  • 11
1

FWIW not an answer to "how to connect with Jena", but the code you use to access GraphDB via the RDF4J API is more complicated than it needs to be. You can simply do this:

 Repository repository = new HTTPRepository(GRAPHDB_SERVER, REPOSITORY_ID);
 repository.initialize();

 try(RepositoryConnection conn = repository.getConnection()) {
    conn.prepareUpdate(strInsert).execute();
 }

It will auto-commit and also automatically roll back on connection close if necessary.

Jeen Broekstra
  • 21,642
  • 4
  • 51
  • 73
  • Thanks for your info Jeen! I just want to check: In the trivial example I have given, auto-commit is surely not a problem. However, in the case where you have multiple `conn.prepareUpdate(strInsertNNN).execute();` statements, I assume using a transaction will be the more robust approach? Is that correct? – Henriette Harmse Jul 04 '18 at 14:55
  • Yes, doing multiple updates in a single transaction will usually be faster, and you get all-or-nothing behaviour. Note though that even for that situation there's utilities to make handling this easier. See the rdf4j docs for details. – Jeen Broekstra Jul 04 '18 at 20:45