# Export SPARQL results as trig

**URL:** <https://community.stardog.com/t/export-sparql-results-as-trig/468>\
**Category:** Support\
**Created:** [July 19, 2017, 1:03am UTC](https://community.stardog.com/t/export-sparql-results-as-trig/468 "2017-07-19T01:03:28Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![peterneorr](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/peterneorr/32/31_2.png) [@peterneorr](https://community.stardog.com/u/peterneorr)\
**Post date:** [July 19, 2017, 1:03am UTC](https://community.stardog.com/t/export-sparql-results-as-trig/468/1 "2017-07-19T01:03:28Z")

</div>

Is it possible to export the results of a SPARQL query to a TRIG file? I’ve used the Stardog data export command to export specific named graphs before, but I have a situation where I have many more named graphs than I care to explicitly name. I want to use something like sparql to specify which graphs should be exported.

I’m thinking of something like this:

```
    export to foo.trig
    construct{
      graph ?g { 
        ?s ?p ?o
      }
    }
    where {
       graph ?g {
           ... some criteria here....
          ?s ?p ?o
       }
    }

```

Does any such functionality exist?

Thank You,  
Peter Neorr

---

<div class="post-metadata">

**Author:** ![pavel](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/pavel/32/18_2.png) [@pavel](https://community.stardog.com/u/pavel)\
**Post date:** [July 19, 2017, 6:53am UTC](https://community.stardog.com/t/export-sparql-results-as-trig/468/2 "2017-07-19T06:53:26Z")

</div>

Hi Peter,

Stardog supports the `graph` keyword in the template so it’s possible. Just use `--format TRIG` when passing this to the `query` CLI command.

Cheers,  
Pavel

---

<div class="post-metadata">

**Author:** ![zachary.whitley](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/zachary.whitley/32/94_2.png) [@zachary.whitley](https://community.stardog.com/u/zachary.whitley)\
**Post date:** [July 19, 2017, 12:55pm UTC](https://community.stardog.com/t/export-sparql-results-as-trig/468/3 "2017-07-19T12:55:07Z")

</div>

Here’s a quick bash script for ya…

```
$> DB=myDb
$> stardog data export $DB --format TRIG --named-graph \
      $(stardog query $DB --format CSV \
      "select distinct ?g where { graph ?g { ... some criteria here ... ?s ?p ?o}}" | tail -n +2) --
```

---

<div class="post-metadata">

**Author:** ![peterneorr](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/peterneorr/32/31_2.png) [@peterneorr](https://community.stardog.com/u/peterneorr)\
**Post date:** [July 31, 2017, 4:52pm UTC](https://community.stardog.com/t/export-sparql-results-as-trig/468/4 "2017-07-31T16:52:39Z")

</div>

> [@pavel](#):
>
> template

Thanks for the suggestion Pavel. I finally got around to trying this today and the CLI command complained whenever I use the graph keyword in my construct statement. Here's some console output:

```auto
c:\Users\pneorr\Desktop\SPARQL-FU>stardog query execute cyber-working export_all_4624_graphs.sparql -- --format TRIG
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
Encountered " "where" "where "" at line 6, column 1.
Was expecting one of:
    <EOF>
    "limit" ...
    "order" ...
    "group" ...
    "having" ...
    "offset" ...
    "values" ...
    "bindings" ...

c:\Users\pneorr\Desktop\SPARQL-FU>cat export_all_4624_graphs.sparql
construct {
    graph ?g {
        ?s ?p ?o
    }
}
where {
   graph ?g {
      ?x a <http://www.champtc.com/ontologies/winevent#T4624> .
      ?s ?p ?o
   }
}

```

Removing the graph keyword produces output, but I lose any named-graph context in the results.

---

<div class="post-metadata">

**Author:** ![peterneorr](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/peterneorr/32/31_2.png) [@peterneorr](https://community.stardog.com/u/peterneorr)\
**Post date:** [July 31, 2017, 5:10pm UTC](https://community.stardog.com/t/export-sparql-results-as-trig/468/5 "2017-07-31T17:10:44Z")

</div>

Thanks Zachary,  
This makes sense to me except that I’m stuck on a windows box and need something that works the same way on different platforms. (windows,bash etc…) I’ll probably end up doing something in Java. 😬

---

<div class="post-metadata">

**Author:** ![zachary.whitley](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/zachary.whitley/32/94_2.png) [@zachary.whitley](https://community.stardog.com/u/zachary.whitley)\
**Post date:** [July 31, 2017, 5:11pm UTC](https://community.stardog.com/t/export-sparql-results-as-trig/468/6 "2017-07-31T17:11:38Z")

</div>

The format flag is expected to come before the database name and query and is probably being ignored

> [@peterneorr](#):
>
> c:\Users\pneorr\Desktop\SPARQL-FU\>stardog query execute cyber-working export\_all\_4624\_graphs.sparql -- --format TRIG

The following query should work.

stardog query execute --format TRIG cyber-working export\_all\_2624\_graphs.sparql

---

<div class="post-metadata">

**Author:** ![peterneorr](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/peterneorr/32/31_2.png) [@peterneorr](https://community.stardog.com/u/peterneorr)\
**Post date:** [July 31, 2017, 5:16pm UTC](https://community.stardog.com/t/export-sparql-results-as-trig/468/7 "2017-07-31T17:16:26Z")

</div>

I got the same error:

```auto
c:\Users\pneorr\Desktop\SPARQL-FU>stardog query execute --format TRIG cyber-working export_all_4624_graphs.sparql
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
Encountered " "where" "where "" at line 6, column 1.
Was expecting one of:
    <EOF>
    "limit" ...
    "order" ...
    "group" ...
    "having" ...
    "offset" ...
    "values" ...
    "bindings" ...

```

---

<div class="post-metadata">

**Author:** ![peterneorr](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/peterneorr/32/31_2.png) [@peterneorr](https://community.stardog.com/u/peterneorr)\
**Post date:** [July 31, 2017, 5:18pm UTC](https://community.stardog.com/t/export-sparql-results-as-trig/468/8 "2017-07-31T17:18:19Z")

</div>

Aha… I’m running v 4.1. That may be my issue.

---

<div class="post-metadata">

**Author:** ![pavel](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/pavel/32/18_2.png) [@pavel](https://community.stardog.com/u/pavel)\
**Post date:** [July 31, 2017, 5:18pm UTC](https://community.stardog.com/t/export-sparql-results-as-trig/468/9 "2017-07-31T17:18:22Z")

</div>

Hi Peter,

Which version of Stardog do you use? Quad templates in `CONSTRUCT` are supported only since 5.0.

Cheers,  
Pavel

---

<div class="post-metadata">

**Author:** ![zachary.whitley](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/zachary.whitley/32/94_2.png) [@zachary.whitley](https://community.stardog.com/u/zachary.whitley)\
**Post date:** [July 31, 2017, 5:18pm UTC](https://community.stardog.com/t/export-sparql-results-as-trig/468/10 "2017-07-31T17:18:29Z")

</div>

What version are you running? You will need to be running 5.0-RC1 or newer for that feature.

---

<div class="post-metadata">

**Author:** ![peterneorr](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/peterneorr/32/31_2.png) [@peterneorr](https://community.stardog.com/u/peterneorr)\
**Post date:** [July 31, 2017, 9:07pm UTC](https://community.stardog.com/t/export-sparql-results-as-trig/468/11 "2017-07-31T21:07:36Z")

</div>

> [@zachary.whitley](#):
>
> stardog query execute --format TRIG cyber-working export\_all\_2624\_graphs.sparql

This worked like a breeze once I upgraded to v5.

Thanks All!  
-P

---

<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:** [August 14, 2017, 9:08pm UTC](https://community.stardog.com/t/export-sparql-results-as-trig/468/12 "2017-08-14T21:08:04Z")

</div>

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