Retrieving SPARQL Results

LOGD 101
This tutorial describes how to access SPARQL Endpoint, and how to retrieve and format query results using SparqlProxy in Javascrip, PHP and Python.

What to Expect

By the end of this tutorial you should be able to retrieve SPARQL query results in your applications, how to use TWC's SparqlProxy to format the SPARQL results, and how to use your favorite programming language (e.g. PHP, Python, Javascript) to retrieve SPARQL query results.

What You Need to Know

This tutorial assumes you are familiar with concepts found in the following resources:
  • Resource Description Framework (RDF) is a standard model for data interchange on the Web. See [1]
  • SPARQL Protocol and RDF Query Language (SPARQL) is an RDF query language. See [2]
  • RESTful Web Service is a simple web service protocol. see [3]

Talking to a Standard SPARQL Endpoint

There are many ways to get results from a SPARQL endpoint. Most endpoints provide a web form in which you can enter a query and get back the results in HTML or some other format. In this tutorial we'll focus on the SPARQL endpoint hosted by, found at .
In the above screen shot, we're presented with a form and a drop-down box of formats from which we can select to have our results returned to us. The query text area contains the following SPARQL query that is used to to discover the different government datasets that have been loaded in the triple store, along with the number of triples that are in that dataset.
PREFIX rdf: <> 
SELECT ?g ?number_of_triples
 GRAPH ?g {
   ?s a <> .
   ?s <> ?number_of_triples.
The SPARQL query is also available at this URL: Retrieving SPARQL results in HTML: You can see the results in HTML. Here is the link to the query results Retrieving SPARQL results in XML: As we can see in the drop down menu, we can get the results in a number of different formats, some for human readability, and other that are more machine consumable. When you run the query, you can see in your address bar your SPARQL query being used as a parameter. We can use this as a RESTful Web service Interface to the SPARQL endpoint as well. Here's an example of the same query above returning the result in "XML". Here is the link to the query results<>+%0D%0A+SELECT+%3Fg+%3Fnumber_of_triples%0D%0A+WHERE+{%0D%0A++GRAPH+%3Fg+{%0D%0A++++%3Fs+a+<>+.%0D%0A++++%3Fs+<>+%3Fnumber_of_triples.%0D%0A++}%0D%0A+}%0D%0A+ORDER+BY+%3Fg&format=application%2Fxml&debug=on&timeout=

Using SparqlProxy to Format SPARQL Query Results

TWC's SparqlProxy can be used to query a SPARQL endpoint (i.e. Web Service interface of a triple store), and perform some post-process on formatting the SPARQL query results. The results can be converted to many different formats such as CSV, Google Visualization JSON and Simile Exhibit JSON. This makes it easier to develop mashups and visualizations from SPARQL results. Let's try our example from last time, using SparqlProxy at
Retrieving SPARQL results in HTML: In the screenshot above, the interface here looks very similar as last time. If we want to query the SPARQL endpoint at using SparqlProxy, we can set the SPARQL End Point URL option to . Having this blank will query the LOGD SPARQL endpoint instead. Using the query from last time will return to us the same results as last time. Here is a link to the query result. Retrieving SPARQL results in Google Visualization Compatiable JOSN: You can see SparqlProxy gives us more options in output of results than the original SPARQL endpoint. By selecting "GoogleViz/JSON" option. You will see the results encoded in Google Visualization Compatible JSON. Here is the link to the query result. SparqlProxy Parameters Also like last time, we can access this service as a RESTful web service. The frequently used parameters are listed in the following table. More details about the parameters can be found at
Parameter Status Description
service-uri stable URI of SPARQL service, e.g.,
query stable SPARQL query string
query-uri stable URI of SPARQL query. Use of "query-uri" and "query" are mutually exclusive, i.e., only use one.
output stable (optional) the output format. values: xml,sparql,exhibit,gvds,csv,html. Default is xml

Accessing SPARQL Query Results using SparqlProxy

Now we provide some example code fragment written in different programming languages to help you get started.


google.load("visualization", "1", {packages:["geomap","table","columnchart"]});
function google_callback(){ 
 var sparqlproxy = "";
 var queryloc = "";    
 var service = "";
 var queryurl = sparqlproxy 
                + "?" + "output=gvds"
                + "&service-uri=" + encodeURIComponent(service)
                + "&query-uri=" + encodeURIComponent(queryloc) ;
 var query = new google.visualization.Query(queryurl); // Send the query.

function  handleQueryResponse(reponse){
  // Check for query response errors.
 if (response.isError()) {
   alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
  var data = response.getDataTable();


 // compose query 
 $sparqlproxy_uri = ""
 $params = array();
 $params["query-uri"] = "";
 $params["service-uri"] = "";
 $params["output"] =  "gvds";
 $query= $sparqlproxy_uri."?". http_build_query($params,,'&') ; //specific for Drupal
 //show query result
 echo file_get_contents($query);


 import urllib2 
 sparqlproxy = ""
 queryloc = "" 
 service = ""
 queryurl = sparqlproxy+"?output=gvds&service-uri="+service+"&query-uri="+queryloc

 response = urllib2.urlopen(queryurl) 
 json =
Your rating: None Average: 4 (4 votes)

Python code fix

Very helpful tutorial. Thanks.

Warning: Table './drupal/watchdog' is marked as crashed and last (automatic?) repair failed query: INSERT INTO watchdog (uid, type, message, variables, severity, link, location, referer, hostname, timestamp) VALUES (0, 'php', '%message in %file on line %line.', 'a:4:{s:6:\"%error\";s:12:\"user warning\";s:8:\"%message\";s:348:\"Table &#039;./drupal/accesslog&#039; is marked as crashed and last (automatic?) repair failed\nquery: INSERT INTO accesslog (title, path, url, hostname, uid, sid, timer, timestamp) values(&#039;Retrieving SPARQL Results&#039;, &#039;node/3917&#039;, &#039;&#039;, &#039;;, 0, &#039;5rr9j7movthte9agkjarbjju93&#039;, 36, 1618322904)\";s:5:\"%file\";s:58:\"/data/www/html/drupal/modules/statistics/statistics.module\";s:5:\"%line\";i:63;}', 3, '', ' in /data/www/html/drupal/includes/ on line 134