Export SPARQL results as trig

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

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

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) --

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:

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.

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. :grimacing:

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

The following query should work.

stardog query execute --format TRIG cyber-working export_all_2624_graphs.sparql

1 Like

I got the same error:

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" ...

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

Hi Peter,

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

Cheers,
Pavel

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

This worked like a breeze once I upgraded to v5.

Thanks All!
-P

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