Search code examples
javajenasesamejson-ldblazegraph

Sesame/Jena SPARQL results to JSON-LD


I've seen the answer on How to return SPARQL results in JSON-LD?, but it's not satisfying/working. I used the JSON-LD Java Integration for Sesame, as well as the standalone version.

What I want to achieve: Send a SPARQL CONSTRUCT query to a SPARQL endpoint via Blazegraph RemoteRepository (based on Sesame/SAIL), get an RDF result, serialize that RDF to JSON-LD. The RDF result works perfectly fine.

The problem is, that the following code (with Sesame) is producing exactly no output:

StringWriter sw = new StringWriter();
final RDFWriter writer = Rio.createWriter( RDFFormat.JSONLD, sw );
//writer.getWriterConfig().set( JSONLDSettings.JSONLD_MODE, JSONLDMode.COMPACT );
GraphQueryResult queryResults;
Rio.write(QueryResults.asModel(queryResults), writer);

I also used a conversion to a Jena internal model, because I know that the Jena JSON-LD output worked fine in another side project of mine. Unfortunately, the same approach doesn't work for a conversion to Jena.

My code with a Sesame to Jena Adapter:

while(queryResults.hasNext()) {
    JenaUtils.asJenaStatement();
}
StringWriter sw = new StringWriter();
// JenaUtils.getModel() returns the Jena model with the added statements above
RDFDataMgr.write( sw, JenaUtils.getModel(), RDFFormat.JSONLD );

What can I do now?


Solution

  • Ok, the problem was not my process mentioned above.

    The issue was caused due to a supressed exception in the json-ld Sesame integration library by an incompatible version of the HTTP Client in Blazegraph.

    java.lang.NoClassDefFoundError: org/apache/http/impl/client/SystemDefaultHttpClient
    

    resulted in not piping the GraphQueryResults to the json-ld. The exception happened due to Blazegraph's incompatible HTTP Client Version (4.1.3), which overrode the json-ld HTTP Client Version (>4.1.3).

    You have to override your project's dependency on HTTP Client with the following:

    <!-- necessary for (sesame) json-ld integration, -->
    <!-- because BlazeGraph uses an older version. See https://github.com/jsonld-java/jsonld-java/issues/64 -->
      <dependency>
         <groupId>org.apache.httpcomponents</groupId>
         <artifactId>httpclient</artifactId>
         <version>4.4</version>
      </dependency>
    

    I hope that will safe someone's time!