Write GraphQueryResult in Java (Stardog 7)

Hello

I am updating my java program because i've updated stardog (From stardog 5 To stardog 7)

I have a few questions :
How can i write a GraphQuerryResult (used for describe querry)

GraphQuery aQuery = starDogConnection.graph(request);
GraphQueryResult aResult=null; ByteArrayOutputStream out=null;
aResult = aQuery.execute();
out = new ByteArrayOutputStream();

How can i write the result in the ByteArrayOutputStream ?

Since GraphQueryResult is a Set of RDF statements, you simply need to use one of the provided RDFWriter classes (depending on the format you want). These writers have constructors that accept OutputStreams, so it becomes quite elegant (especially with try-with-resources and method reference syntax):

try (Connection c = ConnectionConfiguration.to("myDb").connect();
		     GraphQueryResult r = c.graph("describe <urn:foobar>").execute();
		     ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
			TurtleWriter w = new TurtleWriter(baos, Options.empty());
			r.toGraph().forEach(w::handle);
		}

Sorry but it's not a good solution, it works but the result look likes that :
With Stardog 5 i was able to choose to receive the result as a JSON or a RDF Object. With Star dog 7 I have only these text and it's becoming really harder to parse it

Result example :
+----------------------------------------------------------------------------------+------------------------+
| human | patientID |
+----------------------------------------------------------------------------------+------------------------+
| http://medicis.univ-rennes1.fr/ontologies/ontospm/OntoMEDIRAD.owl#Human_66381143 | "ACRIN-FLT-Breast_006" |
| -490b-4b64-8385-6736a453c804 | |
+----------------------------------------------------------------------------------+------------------------+

What you pasted looks like tabular SELECT query results. If you are running a SELECT query instead of a CONSTRUCT/DESCRIBE, you would need to use a SelectQueryResult and not a GraphQueryResult. Could you post your query (obfuscated however) and what result you are expecting?

I have both Describe and Select Querries :
Select Querries Code look likes that :

SelectQueryResult aResult=null; ByteArrayOutputStream out=null;
aResult = aQuery.execute();
out = new ByteArrayOutputStream();
QueryResultWriters.write(aResult, out , TextTableQueryResultWriter.FORMAT);

Describe querries code look like that :

GraphQuery aQuery = starDogConnection.graph(request);
GraphQueryResult aResult=null;
aResult = aQuery.execute();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
TurtleWriter w = new TurtleWriter(baos, Options.empty());
aResult.toGraph().forEach(w::handle);
String resultats = baos.toString();
System.out.println("resultats : ");
System.out.println(resultats);

Graph querries i have an empty string, select querries i have results but i can't parse them

Your code actually reminded me that there's an even easier way to do what I had originally suggested. Just like SelectQueryResult has QueryResultWriters, we also provide an RDFWriters class for GraphQueryResults:

ByteArrayOutputStream baos = new ByteArrayOutputStream();

try (Connection c = ConnectionConfiguration.to("myDb").connect();
  GraphQueryResult aGraphQueryResult = c.graph("describe <urn:foobar>").execute();
  SelectQueryResult aSelectQueryResult = c.select("select * {?s ?p ?o}").execute()) {

  // Use one of the formats in `QueryResultFormats` instead of the `TextTableQueryResultWriter.FORMAT`
  QueryResultWriters.write(aSelectQueryResult, baos, QueryResultFormats.JSON);

  // Here you can pass in any of the `RDFFormats` along with the set of `Statement`s
  RDFWriters.write(baos, RDFFormats.JSONLD, aGraphQueryResult.toGraph());			
}
1 Like

I am testing
It seems to work

It works (almost because sometimes i am receiving an error : com.stardog.stark.query.QueryExecutionFailure: Unknown body atom type: AUXILIARY_CLASS_ASSERTION)

I am investigating, database content seems coherent for the reasoner in Protégé Software.

Thank you very much

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.