Writing to Ontotext GraphDB using Jena

半腔热情 提交于 2019-12-11 00:00:02

问题


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.


回答1:


GraphDB implements standard SPARQL 1.1 endpoints according the RDF4J protocol.

  • http://localhost:7200/repositories/PersonData - SPARQL query endpoint, which does not support "application/sparql-update"
  • http://localhost:7200/repositories/PersonData/statements - SPARQL update endpoint

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.




回答2:


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.



来源:https://stackoverflow.com/questions/51098996/writing-to-ontotext-graphdb-using-jena

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!