Description:
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 Data.gov, found at
http://services.data.gov/sparql .
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: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?g ?number_of_triples
WHERE {
GRAPH ?g {
?s a <http://data-gov.tw.rpi.edu/2009/data-gov-twc.rdf#Dataset> .
?s <http://data-gov.tw.rpi.edu/2009/data-gov-twc.rdf#number_of_triples> ?number_of_triples.
}
}
ORDER BY ?g
The SPARQL query is also available at this URL:
http://logd.tw.rpi.edu/demo/retrieving-sparql-results/datagov-list-loaded-dataset.sparql
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
http://services.data.gov/sparql?default-graph-uri=&query=+PREFIX+rdf%3A+<http%3A%2F%2Fwww.w3.org%2F1999%2F02%2F22-rdf-syntax-ns%23>+%0D%0A+SELECT+%3Fg+%3Fnumber_of_triples%0D%0A+WHERE+{%0D%0A++GRAPH+%3Fg+{%0D%0A++++%3Fs+a+<http%3A%2F%2Fdata-gov.tw.rpi.edu%2F2009%2Fdata-gov-twc.rdf%23Dataset>+.%0D%0A++++%3Fs+<http%3A%2F%2Fdata-gov.tw.rpi.edu%2F2009%2Fdata-gov-twc.rdf%23number_of_triples>+%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
http://logd.tw.rpi.edu/ws/sparqlproxy.php
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 data.gov using
SparqlProxy, we can set the SPARQL End Point URL option to
http://services.data.gov/sparql
. 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
http://logd.tw.rpi.edu/technology/sparqlproxy.
Parameter
| Status
| Description
|
service-uri
| stable
| URI of SPARQL service, e.g. http://dbpedia.org/sparql, http://logd.tw.rpi.edu/sparql
|
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.
JavaScript
google.load("visualization", "1", {packages:["geomap","table","columnchart"]});
google.setOnLoadCallback(google_callback);
function google_callback(){
var sparqlproxy = "http://logd.tw.rpi.edu/ws/sparqlproxy.php";
var queryloc = "http://logd.tw.rpi.edu/demo/retrieving-sparql-results/datagov-list-loaded-dataset.sparql";
var service = "http://services.data.gov/sparql";
var queryurl = sparqlproxy
+ "?" + "output=gvds"
+ "&service-uri=" + encodeURIComponent(service)
+ "&query-uri=" + encodeURIComponent(queryloc) ;
var query = new google.visualization.Query(queryurl); // Send the query.
query.send(handleQueryResponse);
}
function handleQueryResponse(reponse){
// Check for query response errors.
if (response.isError()) {
alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
return;
}
var data = response.getDataTable();
...
}
PHP
// compose query
$sparqlproxy_uri = "http://logd.tw.rpi.edu/ws/sparqlproxy.php"
$params = array();
$params["query-uri"] = "http://logd.tw.rpi.edu/demo/retrieving-sparql-results/datagov-list-loaded-dataset.sparql";
$params["service-uri"] = "http://services.data.gov/sparql";
$params["output"] = "gvds";
$query= $sparqlproxy_uri."?". http_build_query($params,,'&') ; //specific for Drupal
//show query result
echo file_get_contents($query);
Python
import urllib2
sparqlproxy = "http://logd.tw.rpi.edu/ws/sparqlproxy.php"
queryloc = "http://logd.tw.rpi.edu/demo/retrieving-sparql-results/datagov-list-loaded-dataset.sparql"
service = "http://services.data.gov/sparql"
queryurl = sparqlproxy+"?output=gvds&service-uri="+service+"&query-uri="+queryloc
response = urllib2.urlopen(queryurl)
json = response.read()
Python code fix