Introduction to RDF Query with SPARQL

Dave Beckett

Yahoo! Inc

Overview

What Is RDF?

What Is SPARQL?

SPARQL

Background of RDF Query Languages

Why do you need (RDF) query languages?

Pre-Existing RDF Query Language Designs

The main RDF query language styles:

Most popular by far are the SQL-like languages.

RDQL/Squish was the most popular SQL-like language with multiple independent implementations, before SPARQL.

Querying XML and Querying RDF

Concept XML RDF
Model Document or
Tree or
Infoset (PSVI)
Set of Triples
= RDF Graph
Atomic Units Elements, Attributes, Text Triples, URIs, Blank Nodes, Text
Identifiers Element/Attribute names
QNames
IDs
XPointers / XPaths
URIs
Described by DTDs
W3C XML Schema
Relax NG
...
RDF Schema

XSLT / XQuery and RDF

Why standardize RDF query now?

given that RDQL (and others) are widely implemented

So in March 2004 the W3C formed the RDF Data Access Working Group

It has just reached Last Call stage February 2006

Use Cases for RDF Query

Some of the use cases DAWG recorded were:

Requirements for RDF query

These led to requirements

from existing languages:
conjunction (AND) of triple patterns with variable bindings and constraints
from use cases:
graphs, datatypes, extension functions, aggregation, alternates, descriptions
from the WG charter:
a protocol.
no rules language, no cursors, no proofs and no updates [more of this later]

SPARQL - Query Language

SPARQL query syntax

plus more abbreviating syntax

Turtle RDF syntax - IRIs and Blank Nodes

IRIs
Enclosed in <>
Relative IRI references turned into IRIs
<IRI>
or
@prefix prefix <http://....>
prefix:name
in the style of XML QNames as a shorthand for the full IRI
Blank Nodes
_:name
or
[] for a Blank Node used once

Turtle RDF syntax - RDF Literals

Literals
"Literal"
"Literal"@language
"""Long literal with
newlines
"""
Datatyped Literals
"lexical form"^^datatype IRI
e.g. "10"^^xsd:integer
 
10 Decimal integer (xsd:integer)
true Boolean (xsd:boolean)
2.5 Decimal (xsd:decimal)

Turtle RDF Syntax - Abbreviations

Triples separated by .
:a :b :c . :d :e :f .
Common triple predicate and subject:
:a :b :c ,
      :d .
which is the same as
:a :b :c .
:a :b :d .
Common triple subject:
:a :b :c ;
   :d :e .
which is the same as:
:a :b :c .
:a :d :e .

Turtle RDF Syntax - Abbreviations 2

Blank node as a subject
:a :b [ :c :d ] which is the same as:
:a :b _:x .
_:x :c :d .

for blank node _:x
RDF Collections
:a :b ( :c :d :e :f )
which is short for many many triples :)

SPARQL Triple Patterns

Add variables to a Turtle RDF graph. Data:

:a :b :c .
:c :d "hello" 

Query: Find me the x in the RDF graph such that this matches:

?x :b :c .
:c :d "hello"

The full SPARQL query:

PREFIX : <http://example.org/stuff#>
SELECT ?v
WHERE {
  ?v :b :c .
  :c :d "hello"
}

{ } is a SPARQL Graph Pattern

SPARQL Graph Patterns (GPs)

Basic
contains only triple patterns which must all match
Group
contain GPs that MUST match or the GP fails
Optional
contain GPs that MAY match
Union
contain alternate GPs, any or all of which MAY match

All may contain FILTER expressions.

FILTER Expressions and Values

FILTER ?x > 3
FILTER BOUND(?x)
FILTER (?date < "2006-03-01T20:15:00Z"^^xsd:date)

Query Results

Data Management

The Rest

Use Every SPARQL Keyword Example



BASE <http://example.org/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
# This is a relative IRI to BASE above
PREFIX ex: <properties/1.0#>

SELECT DISTINCT $person ?name $age
FROM <http://rdf.example.org/personA.rdf>
FROM <http://rdf.example.org/personB.rdf>
WHERE {  $person a foaf:Person ;
                 foaf:name ?name.
         OPTIONAL { $person ex:age $age } .
         FILTER (!REGEX(?name, "Bob"))
      }
ORDER BY ASC(?name) LIMIT 10 OFFSET 20 

Rasqal SPARQL examples

SPARQL Status

Extending SPARQL Query

SPARQL - Protocol

SPARQL Issues

Thanks


Questions?

Slides (will be at):
http://www.dajobe.org/talks/200603-sparql-stanford
(and the CSS will be better too)

Rasqal SPARQL demo
http://librdf.org/query

Copyright

Copyright Yahoo! Inc. 2006

Based in part on slides originally from the Introduction to RDF Query with SPARQL Tutorial Copyright 2005 Dave Beckett, Steve Harris, Eric Prud'hommeaux and Andy Seaborne.