Skip to content

Query a SPARQL Endpoint and Retrieve Result as a TSV file

Notifications You must be signed in to change notification settings

GateNLP/SesameSPARQL

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tool for Accessing a SPARQL endpoint and fetching query results from there.

Simple API and command line tool to retrieve the result of SPARQL queries from a Sesame repository.

The tool can be used to retrieve large result sets without overwhelming the server by retrieving small batches of data (see option -b below) and allows to increase the maxmimum query timeout for queries over a large repository.

Setup

  • clone the repository and change into the directory
  • run mvn package

This creates a single jar in ./target which contains all the dependencies.

Usage

Run the command:

./bin/query.sh -u <SPARQL-endpoing-URL> -i <QUERY> [OTHEROPTIONS]

The result rows are written to standard output.

To get usage information:

./bin/query.sh -h

This outputs:

usage: ./bin/query.sh [options]
 -b <arg>    Batchsize for queries, default: retrieve all at once
 -ee <arg>   error on invalid strings: true or false, default=false
 -h          Show help information
 -i <arg>    Query input file (required)
 -ii <arg>   include inferred: true or false, default=true
 -mt <arg>   maximum query time in seconds, default=3600
 -ph <arg>   print headers: true or false, default=false
 -u <arg>    endpoint URL (required)

Examples

Query DBPedia and get the URI and English language film title of films starring Charlie Chaplin:

Query file examples/example2.sparql:

PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX res: <http://dbpedia.org/resource/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT ?uri ?string
WHERE {
        ?uri rdf:type dbo:Film .
        ?uri dbo:starring res:Charlie_Chaplin .
        OPTIONAL {?uri rdfs:label ?string . FILTER (lang(?string) = 'en') }
}

Run the command to retrieve the result, write to file chaplin_films.tsv with column headers:

./bin/query.sh -ph true -u  http://DBpedia.org/sparql -i examples/example2.sparql > chaplin_films.tsv