This document describes AllegroGraph's SPARQL Protocol client. This module allows you to run queries against SPARQL servers (termed "endpoints") via HTTP. AllegroGraph seamlessly translates the protocol responses into native results, so you can query remote triple stores without changing your code.

Exported from the sparql.client package is run-sparql-remote. This gives you access to remote SPARQL endpoints (for SELECT and ASK queries) just as run-sparql lets you query local triple stores. Here's an example, querying the DBpedia.org SPARQL endpoint:

sparql.client(2): (run-sparql-remote "http://dbpedia.org/sparql" "  
 PREFIX dbpedia: <http://dbpedia.org/ontology/>  
 PREFIX foaf: <http://xmlns.com/foaf/0.1/>  
 PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>  
 SELECT ?person {  
   ?person dbpedia:birthPlace <http://dbpedia.org/resource/Berlin> .  
 }  
 LIMIT 10" :results-format :alists)  
 
(((?person . !<http://dbpedia.org/resource/Adolf_Otto_Reinhold_Windaus>))  
 ((?person . !<http://dbpedia.org/resource/Benjamin_K%C3%B6hler>))  
 ((?person . !<http://dbpedia.org/resource/Ernst_Lubitsch>))  
 ((?person . !<http://dbpedia.org/resource/Adolf_Otto_Reinhold_Windaus>))  
 ((?person . !<http://dbpedia.org/resource/Alexander_von_Humboldt>))  
 ((?person . !<http://dbpedia.org/resource/Benjamin_K%C3%B6hler>))  
 ((?person . !<http://dbpedia.org/resource/Ernst_Boris_Chain>))  
 ((?person . !<http://dbpedia.org/resource/Ernst_Lubitsch>))  
 ((?person . !<http://dbpedia.org/resource/Mike_Nichols>))  
 ((?person . !<http://dbpedia.org/resource/Paul_Johann_Ludwig_von_Heyse>)))  
:select  
#(?person)  

Note that you don't need to have a database open to issue a remote query:

sparql.client(3): *db*  
nil  

All of the SPARQL results formats that are natively supported for local AllegroGraph ASK and SELECT queries are supported for remote queries, and the values returned by run-sparql-remote are compatible with run-sparql. If you don't rely on any extended features, you can seamlessly switch between local and remote queries. You can obtain a list of possible results-formats by calling get-allowed-remote-results-formats.

Because of the similarity between the local and remote query functions, you can easily hide the distinction if the performance difference is of no concern:

(defun query-wherever (uri &rest args)  
  (if uri  
    (apply #'sparql.client:run-sparql-remote uri args)  
    (apply #'sparql:run-sparql args)))  
 
(defun get-people (&optional uri)  
  "Return a list of people in the store.  
   If a URI is provided, the remote store is used instead."    
  (let ((q "  
           PREFIX foaf: <http://xmlns.com/foaf/0.1/>  
           SELECT ?person {  
             ?person a foaf:Person .  
           }"))  
    (mapcar #'car  
      (query-wherever uri q :results-format :lists))))  

Be aware

There are a few points to bear in mind when using remote queries.

API reference

run-sparql-remote uri  query  &rest  args  &key  results-format  rdf-format  output-stream  id
function

Run a SPARQL query against a remote endpoint, locally serializing the results to 'output-stream' in the appropriate format (as with run-sparql).

The value of the uri argument is provided to net.aserve.client:do-http-request; it can be a string or a net.uri:uri.

If output-stream is null, and the results format is one that generates textual output, then the serialized output is returned as a string.

Other arguments (such as :from and :from-named, and arguments to net.aserve.client:do-http-request) are passed to run-sparql-remote-with-results-functions.

You can use this opportunity to provide :method :post to encode large queries in the request body.

If :function or :passthrough-function are provided, they will be discarded.

You may only make requests that yield legal SPARQL XML or RDF output; the AllegroGraph extended syntax is therefore not available.

At present, the only supported verbs are SELECT and ASK.

get-allowed-remote-results-formats &optional  verb
function
Returns a list of keyword symbols that are valid when applied as values of results-format to a remote query with the given verb. if verb is not provided, the intersection of :ask and :select (the two permitted values) is returned.

Indices

Full symbol index