SPARQL intro

From n² wiki

Jump to: navigation, search

Contents

[edit] Overview

SPARQL is a W3C Recommendation query language for RDF supported by the Talis Platform.

If you are familiar with SQL, you will recognize keywords such as SELECT, DISTINCT, WHERE, FROM, OFFSET and LIMIT, though the usage may be slightly different.

A form of the RDF notation Turtle, is used in the WHERE clause, to define conditions that the triples must match.

[edit] What SPARQL can give you

You can use SPARQL to retrieve:

  • complete RDF descriptions of one or more resource in the data-store (using DESCRIBE)
  • selective RDF triples from the data-store (using CONSTRUCT)
  • RDF data modeled differently from the way it is stored in the data-store (using CONSTRUCT)
  • Tabular results (in SPARQL XML format), such as those you get from a SQL query (using SELECT)
  • A Boolean result (in SPARQL XML format), using ASK


[edit] What SPARQL can't give you (at least, not yet)

  • Modify data - SPARQL is a read-only query language (for updating and inserting data to Talis platform stores, see ChangeSets).
  • COUNT - you can't use SPARQL to give you back a count.
  • AVG - neither can SPARQL give you averages
  • sub-SELECTS: there is no WHERE ... IN ( ... ), or WHERE ... NOT IN ( ... )

[edit] Results Formats

Currently, the results will either be SPARQL XML (from a SELECT or ASK query), or RDF/XML (from a DESCRIBE or CONSTRUCT query). Talis has plans to implement RDF results in other formats, such as RDF/JSON and turtle.

[edit] Query Forms

[edit] DESCRIBE

Returns an RDF graph of complete descriptions (containing all known properties) of matching resources.

[edit] looks like
	DESCRIBE <http://example.com/resources/#uri>
	# returns one description - of the resource identified by the given URI

or

	PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
	
	DESCRIBE ?resource
	WHERE
	{
		?resource rdfs:label "SPARQL tutorial" .
	}
	# returns descriptions of any resources 
	# that match  the conditions in the WHERE clause


[edit] good for

When you don't know exactly what properties you expect the resource(s) to have.

[edit] CONSTRUCT

Returns an RDF graph of triples templated from the CONSTRUCT clause with data from triples matching the WHERE clause.

[edit] looks like
	PREFIX dc: <http://purl.org/dc/elements/1.1/>
	PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
	
	CONSTRUCT
	{
	  ?resource rdfs:label ?title .	
	}
	WHERE
	{
	  ?resource dc:title ?title .		
	}
	#returns triples with dc:title values mapped onto rdfs:label properties
[edit] good for
  • retrieving 'selective' RDF graphs, containing only the properties you need.
  • reshaping the data into RDF modeled in a different way:
    • mapping the data to using different RDF properties or class types.
    • 'smushing' - merging the properties of multiple resources into one resource based on them sharing some uniquely identifying property value, such as an e-mail address perhaps.
    • 'inferencing' - see [Ontology Mapping with SPARQL CONSTRUCT]
    • 'flattening' the graph structure by moving properties from an ancillary resource to the target resource.
    • 'deepening' the graph structure by moving properties from a resource to a new related resource.

[edit] SELECT

Returns tabular results in the SPARQL XML format

[edit] looks like
	PREFIX foaf: <http://xmlns.com/foaf/0.1/>
	
	SELECT ?name ?email
	WHERE
	{
		?person foaf:name ?name ;
				foaf:mbox ?mbox .
	}
	#returns tabular results with columns for 'name' and 'email'
[edit] good for
  • retrieving data in XML with a reliably predictable structure (for use with XSLT for example). RDF/XML has many possible ways of serialising the same data.
  • selecting specific rows of data, especially when used with the DISTINCT keyword

[edit] ASK

[edit] looks like
	ASK 
	WHERE 
	{ 
		?person  <http://xmlns.com/foaf/0.1/mbox> <example@example.org> . 
	}	
	#returns TRUE if a resource with that e-mail address as a foaf:mbox 
	# property exists in the store 
[edit] good for
  • when you only need to know if an RDF graph matching certain conditions exists in the store

[edit] the WHERE clause

The WHERE clause describes patterns that the solutions need to match. These patterns are written in [turtle notation], with the addition that variables can be used as for unspecified literals or URIs. These can take the form ?foo or $foo.

[edit] OPTIONAL

Patterns can be made optional by use of the OPTIONAL keyword. eg:

	PREFIX foaf: <http://xmlns.com/foaf/0.1/>
	
	SELECT ?name ?email
	WHERE
	{
		?person foaf:name ?name .
		OPTIONAL{ ?person	foaf:mbox ?mbox . }
	}
# every result row will have a name, and may or may not have an email

[edit] UNION

Multiple different patterns can match to the same variables using UNION. eg:

	PREFIX foaf: <http://xmlns.com/foaf/0.1/>
	PREFIX ex: <http://example.org/vocab#>
	SELECT ?name ?email
	WHERE
	{
	  {
		?person foaf:name ?name ;
				foaf:mbox ?mbox .
	  }
	UNION
	  {
		?person rdfs:label ?name ;
				ex:email ?mbox .
	  }
	
	}
	#returns tabular results with columns for 'name' and 'email'
	# filled from solutions to both patterns in the WHERE clause

[edit] FILTER

FILTERs test values within a graph. The FILTER must evaluate to TRUE in order for the pattern to match.

eg:
	PREFIX foaf: <http://xmlns.com/foaf/0.1/>
	DESCRIBE ?person
	WHERE
	{
		?person a foaf:Person ;
				foaf:name ?name .
		FILTER(REGEX(?name, "Tarquin"))
	}
	# returns descriptions of every resource of rdf:type foaf:Person 
	# and foaf:name property where the value contains "Tarquin"

see the Testing Values :: W3C SPARQL Specification

[edit] ORDER BY

ORDER the result set according to one of the variables in the WHERE clause - can be used with either ASC() or DESC(), eg:

	PREFIX foaf: <http://xmlns.com/foaf/0.1/>
	
	SELECT ?name ?email
	WHERE
	{
		?person foaf:name ?name ;
				foaf:mbox ?mbox .
	}
	ORDER BY DESC(?name)


[edit] LIMIT and OFFSET

LIMIT sets the maximum number of results to be returned, OFFSET sets the number of results to skip. eg:

	PREFIX foaf: <http://xmlns.com/foaf/0.1/>
	DESCRIBE ?person
	WHERE
	{
		?person a foaf:Person
	}
	LIMIT 10 OFFSET 0

[edit] Further Reading

Personal tools