Search code examples
jenagraphdb

Writing to Ontotext GraphDB using Jena


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.


Solution

  • 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.