# Write GraphQueryResult in Java (Stardog 7)

**URL:** <https://community.stardog.com/t/write-graphqueryresult-in-java-stardog-7/1858>\
**Category:** Support\
**Created:** [August 28, 2019, 4:18pm UTC](https://community.stardog.com/t/write-graphqueryresult-in-java-stardog-7/1858 "2019-08-28T16:18:27Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![witchBioinformatics](https://avatars.discourse-cdn.com/v4/letter/w/71e660/32.png) [@witchBioinformatics](https://community.stardog.com/u/witchBioinformatics)\
**Post date:** [August 28, 2019, 4:18pm UTC](https://community.stardog.com/t/write-graphqueryresult-in-java-stardog-7/1858/1 "2019-08-28T16:18:28Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![stephen](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/stephen/32/27_2.png) [@stephen](https://community.stardog.com/u/stephen)\
**Post date:** [August 28, 2019, 6:33pm UTC](https://community.stardog.com/t/write-graphqueryresult-in-java-stardog-7/1858/2 "2019-08-28T18:33:02Z")

</div>

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 `OutputStream`s, so it becomes quite elegant (especially with try-with-resources and method reference syntax):

```java
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);
		}

```

---

<div class="post-metadata">

**Author:** ![witchBioinformatics](https://avatars.discourse-cdn.com/v4/letter/w/71e660/32.png) [@witchBioinformatics](https://community.stardog.com/u/witchBioinformatics)\
**Post date:** [August 30, 2019, 12:51pm UTC](https://community.stardog.com/t/write-graphqueryresult-in-java-stardog-7/1858/3 "2019-08-30T12:51:26Z")

</div>

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 |  
+----------------------------------------------------------------------------------+------------------------+  
| [MediCIS - Inserm - UR1 - UMR 1099 LTSI](http://medicis.univ-rennes1.fr/ontologies/ontospm/OntoMEDIRAD.owl#Human_66381143) | "ACRIN-FLT-Breast\_006" |  
| -490b-4b64-8385-6736a453c804 | |  
+----------------------------------------------------------------------------------+------------------------+

---

<div class="post-metadata">

**Author:** ![stephen](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/stephen/32/27_2.png) [@stephen](https://community.stardog.com/u/stephen)\
**Post date:** [August 30, 2019, 12:55pm UTC](https://community.stardog.com/t/write-graphqueryresult-in-java-stardog-7/1858/4 "2019-08-30T12:55:22Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![witchBioinformatics](https://avatars.discourse-cdn.com/v4/letter/w/71e660/32.png) [@witchBioinformatics](https://community.stardog.com/u/witchBioinformatics)\
**Post date:** [August 30, 2019, 1:10pm UTC](https://community.stardog.com/t/write-graphqueryresult-in-java-stardog-7/1858/5 "2019-08-30T13:10:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stephen](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/stephen/32/27_2.png) [@stephen](https://community.stardog.com/u/stephen)\
**Post date:** [August 30, 2019, 1:23pm UTC](https://community.stardog.com/t/write-graphqueryresult-in-java-stardog-7/1858/6 "2019-08-30T13:23:44Z")

</div>

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`:

```java
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());			
}

```

---

<div class="post-metadata">

**Author:** ![witchBioinformatics](https://avatars.discourse-cdn.com/v4/letter/w/71e660/32.png) [@witchBioinformatics](https://community.stardog.com/u/witchBioinformatics)\
**Post date:** [August 30, 2019, 1:58pm UTC](https://community.stardog.com/t/write-graphqueryresult-in-java-stardog-7/1858/7 "2019-08-30T13:58:17Z")

</div>

I am testing  
It seems to work

---

<div class="post-metadata">

**Author:** ![witchBioinformatics](https://avatars.discourse-cdn.com/v4/letter/w/71e660/32.png) [@witchBioinformatics](https://community.stardog.com/u/witchBioinformatics)\
**Post date:** [August 30, 2019, 6:36pm UTC](https://community.stardog.com/t/write-graphqueryresult-in-java-stardog-7/1858/8 "2019-08-30T18:36:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/e/ed66b48a616f505106a4acde3c9cee7e6da9bc67.svg) [@system](https://community.stardog.com/u/system)\
**Post date:** [September 13, 2019, 6:36pm UTC](https://community.stardog.com/t/write-graphqueryresult-in-java-stardog-7/1858/9 "2019-09-13T18:36:40Z")

</div>

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