Title: SPARQL AN RDF Query Language
1SPARQLAN RDF Query Language
2SPARQL
- SPARQL is a recursive acronym for
- SPARQL Protocol And Rdf Query Language
- SPARQL is the SQL for RDF
- Example
- PREFIX abc lthttp//example.com/exampleOntogt
- SELECT ?capital ?country
- WHERE
- ?x abccityname ?capital
- abcisCapitalOf ?y .
- ?y abccountryname ?country
- abcisInContinent abcAfrica .
-
3SPARQL
- SPARQL is a recursive acronym for
- SPARQL Protocol And Rdf Query Language
- SPARQL is the SQL for RDF
- Example
- PREFIX abc lthttp//example.com/exampleOntogt
- SELECT ?capital ?country
- WHERE
- ?x abccityname ?capital
- abcisCapitalOf ?y .
- ?y abccountryname ?country
- abcisInContinent abcAfrica .
-
4SPARQL History
- Several RDF query languages were developed prior
to SPARQL - W3C RDF Data Access Working Group (DAWG) worked
out SPARQL 2005-2008 - Became a W3C recommendation in January 2008 with
key documents - http//www.w3.org/TR/rdf-sparql-query/
- http//www.w3.org/TR/rdf-sparql-protocol/
- http//www.w3.org/TR/rdf-sparql-XMLres/
- Implementations for multiple programming
languages available
5SPARQL Query Forms
- SELECT
- Returns all, or a subset of, the variables bound
in a query pattern match. - ASK
- Returns a boolean indicating whether a query
pattern matches or not. - DESCRIBE
- Returns an RDF graph that describes the resources
found. - CONSTRUCT
- Returns an RDF graph constructed by substituting
variables in a set of triple templates.
6Its Turtles all the way down
Turtle (Terse RDF Triple Language ) - An RDF
serialization - Triple representation of
ltSubject, Predicate, Objectgt - Human-friendly
alternative to RDF/XML
lthttp//example/person/Agt lthttp//xmlns.com/foaf.
0.1/namegt Jek
_at_prefix person lthttp//example/person/gt
. _at_prefix foaf lthttp//xmlns.com/foaf/0.1/gt
. PersonA foafname Jek" . PersonA foafmbox
ltmailtojek_at_example.netgt . PersonB foafname
Yuan" . _b foafname Jeff" . _b foafmbox
ltmailtojeff_at_example.orggt .
------------- name Jek
Yuan Jeff -------------
Blank Node
A "hello world" of queries SELECT ?name WHERE
?x foafname ?name
7Matching RDF Literals
_at_prefix dt lthttp//example.org/datatypegt .
_at_prefix ns lthttp//example.org/nsgt . _at_prefix
lthttp//example.org/nsgt . _at_prefix xsd
lthttp//www.w3.org/2001/XMLSchemagt . x nsp
"cat"_at_en . y nsp "42"xsdinteger . z nsp
"abc"dtspecialDatatype .
--------- v
----------------------------------
v http
//example.org/nsx ----------------------------
------
----------------------------------
V htt
p//example.org/nsz --------------------------
--------
----------------------------------
V http
//example.org/nsy -----------------------------
-----
SELECT ?v WHERE ?v ?p "cat"
SELECT ?v WHERE ?v ?p "cat_at_en
SELECT ?v WHERE ?v ?p 42
SELECT ?v WHERE ?v ?p "abc dtspecialDatatype
8Filter
_at_prefix dc lthttp//purl.org/dc/elements/1.1/gt
. _at_prefix stock lthttp//example.org/stockgt
. _at_prefix inv lthttp//example.org/inventorygt
. stockbook1 dctitle "SPARQL Query Language
Tutorial" . stockbook1 dcedition
First stockbook1 invprice 10 . stockbook1
invquantity 3 . stockbook2 dctitle "SPARQL
Query Language (2nd ed)" . stockbook2 invprice
20 invquantity 5 . stockbook3 dctitle
"Applying XQuery dcedition Second
. stockbook3 invprice 20 invquantity 8 .
--------------------------------------------------
------------------- book title
stockbook1 "SPARQL Query Language
Tutorial" --------------------------------------
-------------------------------
PREFIX dc lthttp//purl.org/dc/elements/1.1/gt PREF
IX stock lthttp//example.org/stockgt PREFIX inv
lthttp//example.org/inventorygt SELECT ?book
?title WHERE ?book dctitle ?title . ?book
invprice ?price . FILTER ( ?price lt 15 ) ?book
invquantity ?num . FILTER ( ?num gt 0 )
9Other Solution Modifiers
PREFIX foaf lthttp//xmlns.com/foaf/0.1/gt SELECT
?name WHERE ?x foafname ?name ORDER BY
?name
PREFIX foaf lthttp//xmlns.com/foaf/0.1/gt SELECT
DISTINCT ?name WHERE ?x foafname ?name
ORDER BY ?name LIMIT 5 OFFSET 10
10ASK
_at_prefix foaf lthttp//xmlns.com/foaf/0.1/gt . _a
foafname Jek" . _a foafhomepage
lthttp//work.example.org/Jek/gt . _b foafname
"Bob" . _b foafmbox ltmailtobob_at_work.examplegt
.
Yes
PREFIX foaf lthttp//xmlns.com/foaf/0.1/gt ASK
?x foafname Jek"
lt?xml version"1.0"?gt ltsparql xmlns"http//www.w
3.org/2005/sparql-results"gt ltheadgt lt/headgt
ltresultsgt ltbooleangttruelt/booleangt
lt/resultsgt lt/sparqlgt
11DESCRIBE
PREFIX books lthttp//example.org/book/gt PREFIX
dc lthttp//purl.org/dc/elements/1.1/gt DESCRI
BE ?book WHERE ?book dctitle "Harry Potter
and the Prisoner Of Azkaban"
ltrdfRDFgt ltrdfDescription
rdfabout"http//example.org/book/book3"gt
ltdccreator rdfparseType"Resource"gt
ltvcardN rdfparseType"Resource"gt
ltvcardGivengtJoannalt/vcard
Givengt
ltvcardFamilygtRowlinglt/vcardFamilygt
lt/vcardNgt ltvcardFNgtJ.K.
Rowlinglt/vcardFNgt lt/dccreatorgt
ltdctitlegtHarry Potter and the Prisoner
Of Azkabanlt/dctitlegt lt/rdfDescriptiongt lt/
rdfRDFgt
12Describess results?
- The DAWG did not reach a consensus on what
describe should return - Possibilities include
- All triples where the variable bindings are
mentioned - All triples where the bindings are the subject
- Something else
- What is useful might depend on the application or
the amount of data involved - So it was left to the implementation
13CONSTRUCT
_at_prefix foaf lthttp//xmlns.com/foaf/0.1/gt . _a
foafgivenname "Alice" . _a foaffamily_name
"Hacker" . _b foaffirstname "Bob" . _b
foafsurname "Hacker" .
PREFIX foaf lthttp//xmlns.com/foaf/0.1/gt PREFIX
vcard lthttp//www.w3.org/2001/vcard-rdf/3.0gt
CONSTRUCT ?x vcardN _v . _v
vcardgivenName ?gname . _v vcardfamilyName
?fname WHERE ?x foaffirstname ?gname
UNION ?x foafgivenname ?gname . ?x
foafsurname ?fname UNION ?x foaffamily_name
?fname .
_at_prefix vcard lthttp//www.w3.org/2001/vcard-rdf/3
.0gt . _v1 vcardN _x . _x
vcardgivenName "Alice" . _x vcardfamilyName
"Hacker" . _v2 vcardN _z . _z
vcardgivenName "Bob" . _z vcardfamilyName
"Hacker" .
14On construct
- Having a result form that produces an RDF graph
is a good idea - It enables on to construct systems by using the
output of one SPARQL query as the data over which
another query works - This kind of capability was a powerful one for
relational databases
15Optional Pattern Matching
- Data
- _at_prefix foaf lthttp//xmlns.com/foaf/0.1/gt .
- _at_prefix rdf lthttp//www.w3.org/1999/02/22-rdf-syn
tax-nsgt . - _a rdftype foafPerson .
- _a foafname "Alice" .
- _a foafmbox ltmailtoalice_at_example.comgt .
- _a foafmbox ltmailtoalice_at_work.examplegt .
- _b rdftype foafPerson .
- _b foafname "Bob" .
- Query
- PREFIX foaf lthttp//xmlns.com/foaf/0.1/gt
- SELECT ?name ?mbox
- WHERE
-
- ?x foafname ?name .
- OPTIONAL ?x foafmbox ?mbox
-
Query Result
name mbox
Alice ltmailtoalice_at_example.comgt
Alice ltmailtoalice_at_work.examplegt
Bob
16RDF Dataset
- RDF data stores may hold multiple RDF graphs
- record information about each graph
- queries that involve information from gt one graph
- RDF Dataset in SPARQL terminology
- the background graph, which doent have a name,
0 or more named graphs, identified by URI
reference - Use cases
- (i) to have information in the background graph
that includes provenance information about the
named graphs (the application is not directly
trusting the information in the named graphs ) - (ii) to include the information in the named
graphs in the background graph as well
17RDF Named graphs
- Having multiple RDF graphs in a single
document/repository and naming them with URIs - Provides useful additional functionality built on
top of the RDF Recommendations - SPARQL queries can involve several graphs, a
background one and multiple named ones, e.g. - SELECT ?who ?g ?mbox
- FROM lthttp//example.org/dft.ttlgt
- FROM NAMED lthttp//example.org/alicegt
- FROM NAMED lthttp//example.org/bobgt
- WHERE
- ?g dcpublisher ?who .
- GRAPH ?g ?x foafmbox ?mbox
-
18Example (I)
Background graph _at_prefix dc lthttp//purl.org/dc/elements/1.1/gt . lthttp//example.org/bobgt dcpublisher "Bob" . lthttp//example.org/alicegt dcpublisher "Alice" .
Graph http//example.org/bob _at_prefix foaf lthttp//xmlns.com/foaf/0.1/gt . _a foafname "Bob" . _a foafmbox ltmailtobob_at_oldcorp.example.orggt .
Graph http//example.org/alice _at_prefix foaf lthttp//xmlns.com/foaf/0.1/gt . _a foafname "Alice" . _a foafmbox ltmailtoalice_at_work.example.orggt .
19Example(II)
Background graph _at_prefix foaf lthttp//xmlns.com/foaf/0.1/gt . _x foafname "Bob" . _x foafmbox ltmailtobob_at_oldcorp.example.orggt . _y foafname "Alice" . _y foafmbox ltmailtoalice_at_work.example.orggt .
Graph http//example.org/bob _at_prefix foaf lthttp//xmlns.com/foaf/0.1/gt . _a foafname "Bob" . _a foafmbox ltmailtobob_at_oldcorp.example.orggt .
Graph http//example.org/alice _at_prefix foaf lthttp//xmlns.com/foaf/0.1/gt . _a foafname "Alice" . _a foafmbox ltmailtoalice_at_work.example.orggt .
20Querying the Dataset
Graph http//example.org/foaf/aliceFoaf _at_prefix foaf lthttp//xmlns.com/foaf/0.1/gt . _at_prefix rdf lthttp//www.w3.org/1999/02/22-rdf-syntax-nsgt . _at_prefix rdfs lthttp//www.w3.org/2000/01/rdf-schemagt . _a foafname "Alice" . _a foafmbox ltmailtoalice_at_work.examplegt . _a foafknows _b . _b rdfsseeAlso lthttp//example.org/foaf/bobFoafgt . lthttp//example.org/foaf/bobFoafgt rdftype foafPersonalProfileDocument . _b foafname "Bob" . _b foafmbox ltmailtobob_at_work.examplegt . _b foafage 32 .
Graph http//example.org/foaf/bobFoaf _at_prefix foaf lthttp//xmlns.com/foaf/0.1/gt . _at_prefix rdf lthttp//www.w3.org/1999/02/22-rdf-syntax-nsgt . _at_prefix rdfs lthttp//www.w3.org/2000/01/rdf-schemagt . _1 foafmbox ltmailtobob_at_work.examplegt . _1 rdfsseeAlso lthttp//example.org/foaf/bobFoafgt . _1 foafage 35 . lthttp//example.org/foaf/bobFoafgt rdftype foafPersonalProfileDocument .
21Accessing Graph Labels
PREFIX foaf lthttp//xmlns.com/foaf/0.1/gt SELECT ?src ?bobAge WHERE GRAPH ?src ?x foafmbox ltmailtobob_at_work.examplegt . ?x foafage ?bobAge
src bobAge
lthttp//example.org/foaf/aliceFoafgt 32
lthttp//example.org/foaf/bobFoafgt 35
22Restricting by Graph Label
PREFIX foaf lthttp//xmlns.com/foaf/0.1/gt PREFIX data lthttp//example.org/foaf/gt SELECT ?age WHERE GRAPH databobFoaf ?x foafmbox ltmailtobob_at_work.examplegt . ?x foafage ?age
age
35
23Restricting via Query Pattern
PREFIX data lthttp//example.org/foaf/gt PREFIX foaf lthttp//xmlns.com/foaf/0.1/gt PREFIX rdf lthttp//www.w3.org/1999/02/22-rdf-syntax-nsgt PREFIX rdfs lthttp//www.w3.org/2000/01/rdf-schemagt SELECT ?mbox ?age ?ppd WHERE GRAPH dataaliceFoaf ?alice foafmbox ltmailtoalice_at_work.examplegt foafknows ?whom . ?whom foafmbox ?mbox rdfsseeAlso ?ppd . ?ppd a foafPersonalProfileDocument . . GRAPH ?ppd ?w foafmbox ?mbox foafage ?age
mbox age ppd
ltmailtobob_at_work.examplegt 35 lthttp//example.org/foaf/bobFoafgt
24More Features
- RDF Dataset
- - Collection of RDF Graphs
- use FROM http//planetrdf.com/bloggers.rdf
- use FROM NAMED lthttp//site1.example.com/foo.rdfgt
25Limitation of SPARQL
- SPARQL has many limitations, including
- No Insert, Update, Delete queries
- No aggregation functions
- These and many other features are being evaluated
for inclusion in a future recommendation, see - http//www.w3.org/2009/sparql/wiki/CategoryFeatur
es