How to specify RDF Dataset in SPARQL queries

From Data-gov Wiki

Jump to: navigation, search
Infobox () [[category:]] edit with form
  • name: How to specify RDF Dataset in SPARQL queries

  • description: Small tutorial about RDF and SPARQL.
  • creator(s): Devin Gaffney
  • created: 2010/02/25
  • modified: 2010-4-7


This page builds upon the introduction How to use SPARQL.

Contents

Introduction

Section 8 of the W3C document "SPARQL Query Language for RDF" in part outlines the usage of the FROM clause, or the DatasetClause. The role of both the FROM and FROM NAMED clauses is to further define the dataset in addition to the initial specification of what is being selected (in SELECT). There are two primary ways to use the clause, and a default option if it is omitted.

FROM uses

FROM clause omitted

Visual Representation of the Dataset Specification scheme for SPARQL. In the first case, we are only specifying the default graph. In the second case, we specify two named graphs, but leave out a default graph, which becomes a blank node. In the third case, we specify a default graph as well as a few named graphs, so we have a connection through the default graph to the named graphs.

As outlined in the beginning of Section 8, we always are querying one default dataset (either explicitly written out in the FROM statement or passed as the query-uri parameter when we are using a service endpoint such as Virtuoso (which allows FROM clauses as opposed to Redland's Rasqal)). In many cases, however, we want to query more than one data set. In this case, we can use the FROM and FROM NAMED clauses to specify them. Whereas in the default method, we are only asking the default, with these clauses you can specify any set of graphs to include, and the nature of how they should be included in the search.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
  <head> 
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/> 
    <title> 
    Google Visualization
    </title> 
    <script type="text/javascript" src="http://www.google.com/jsapi"></script> 
    <script type="text/javascript"> 
      google.load('visualization', '1', {packages: ['geomap']});
    </script> 
    <script type="text/javascript"> 
    var visualization;
 
    function drawVisualization() {
      // To see the data that this visualization uses, browse to
      // http://spreadsheets.google.com/ccc?key=pCQbetd-CptGXxxQIG7VFIQ  
      var query = new google.visualization.Query(
'http://data-gov.tw.rpi.edu/ws/sparqlproxy.php
  ?query-uri=http%3A%2F%2Fdata-gov.tw.rpi.edu%2Fsparql%2Fquakemap.sparql&output=gvds');
      
     
      // Send the query with a callback function.
      
      query.send(handleQueryResponse);
       
    }
    
    function handleQueryResponse(response) {
 
      if (response.isError()) {
        alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
        return;
      }
        var options=null;
      options = {};options['dataMode'] = 'markers';
  var data = response.getDataTable();
 
      visualization = new google.visualization.GeoMap(document.getElementById('visualization'));
      visualization.draw( data ,options);
    }
 
    google.setOnLoadCallback(drawVisualization);
    </script> 
  </head> 
  <body style="font-family: Arial;border: 0 none;"> 
    <div id="visualization" style="height: 400px; width: 400px;"></div>  
  </body> 
</html>


Then, change the following line:

'http://data-gov.tw.rpi.edu/ws/sparqlproxy.php?query-uri=http%3A%2F%2Fdata-gov.tw.rpi.edu%2Fsparql%2Fquakemap.sparql&output=gvds'

to:

'http://data-gov.tw.rpi.edu/ws/sparqlproxy.php?query-uri=http%3A%2F%2Fdata-gov.tw.rpi.edu%2Fsparql%2Fquakemap.sparql&output=gvds&service-uri=http%3A%2F%2Fonto.rpi.edu%2Fjoseki%2Fsparql'

The code should now work, as a service-uri is being passed into the parameters, which is then ostensibly translated into a FROM clause in the service's SPARQL query (or performs the equivalent action), or in other words, the SPARQL line would read:

FROM <http://onto.rpi.edu/joseki/sparql>

FROM clause employed, FROM NAMED omitted

If the FROM clause is explicitly employed, then, as stated in Section 8.2: "If a query provides such a dataset description, then it is used in place of any dataset that the query service would use if no dataset description is provided in a query." In other words, you are removing the default setting the query service employs, and specifying your own set to use instead. Note: This can be over-ridden by the SPARQL protocol request, which you can read more about in Section 2.1.2.

FROM clause employed, FROM NAMED employed

If the FROM clause and the FROM NAMED sub-clause are used, then we get a case where the name of the document implies the relationship between the Default graph and Named Graph(s). Generally, the case seems to be that the Default references the Named graphs as the Subject in relevant Triples. For example, take this set of data used in the W3C Document:

# Default graph (stored at http://example.org/dft.ttl)
@prefix dc: <http://purl.org/dc/elements/1.1/> .

<http://example.org/bob>    dc:publisher  "Bob Hacker" .
<http://example.org/alice>  dc:publisher  "Alice Hacker" .


# Named graph: http://example.org/bob
@prefix foaf: <http://xmlns.com/foaf/0.1/> .

_:a foaf:name "Bob" .
_:a foaf:mbox <mailto:bob@oldcorp.example.org> .


# Named graph: http://example.org/alice
@prefix foaf: <http://xmlns.com/foaf/0.1/> .

_:a foaf:name "Alice" .
_:a foaf:mbox <mailto:alice@work.example.org> .

In the data set above, the Subjects for the two Triples in the default graphs are IRI's pointing to the Named Graphs. In a way, we are creating a branched structure to the data for Bob and Alice Hacker, by including additional and supplementary data concerning them, such as the name and mailbox. This is similar to only employing a FROM clause, it just specifies the name as well in declaring the file to be used. Likewise, a FROM NAMED clause can be employed while a FROM clause is omitted; the result is simply a blank node Default graph. For a clear example, see Section 8.2.2 for FROM NAMED alone and 8.2.3 for FROM as well as FROM NAMED.

Summary

For more than a cursory explanation of this, please have a look at Section 8 of the W3C document specifying SPARQL: http://www.w3.org/TR/rdf-sparql-query/#rdfDataset

Facts about How to specify RDF Dataset in SPARQL queriesRDF feed
Dcterms:created25 February 2010  +
Dcterms:creatorDevin Gaffney  +
Dcterms:descriptionSmall tutorial about RDF and SPARQL.
Dcterms:modified2010-4-7  +
Foaf:nameHow to specify RDF Dataset in SPARQL queries
Skos:altLabelHow to specify RDF Dataset in SPARQL queries  +, how to specify rdf dataset in sparql queries  +, and HOW TO SPECIFY RDF DATASET IN SPARQL QUERIES  +
Personal tools
internal pages