Skip to content

SPARQL Scriptsยค

By prepending a Shebang line to a SPARQL query file and making the file executable, it can be treated as an executable script. To give an example, below is a simple text file with a generic SPARQL query that counts all triples in all graphs and outputs an ordered list.
SELECT DISTINCT ?graph (COUNT(?graph) AS ?triples)
    GRAPH ?graph
        ?s ?p ?o
GROUP BY ?graph
ORDER BY DESC(?triples)

In order to convert this text file into a SPARQL script you need to add the following line to the top of the file:

shebang line for SPARQL scripts
#!/usr/bin/env -S cmemc query execute --accept text/csv

This will set cmemc as an interpreter for the rest of the file, and by using the query execute command, the rest of the file will be used as a SPARQL query.

Now you need to define your SPARQL file as executable and run it:

$ chmod a+x ./
$ ./