SPARQL TUTORIAL
BY AMNA BASHARAT
7th October , 2008
FAST‐NU , Islamabad
in a nutshell
Adapted from fabien, gandon, inria
RDF triple model
is the first layer of the
semantic web standards
FAST – NU, Islamabad, Fall 2008
SPARQL on top...
an RDF query language
and data access protocol
FAST – NU, Islamabad, Fall 2008
SPARQL stands for
SPARQL Protocol and
RDF Query Language
FAST – NU, Islamabad, Fall 2008
SPARQL in 3 parts
part 1: query language
part 2: result format
part 3: access protocol
FAST – NU, Islamabad, Fall 2008
SPARQL query
SELECT ...
FROM ...
WHERE { ... }
FAST – NU, Islamabad, Fall 2008
SELECT clause
to identify the values to
be returned
FAST – NU, Islamabad, Fall 2008
FROM clause
to identify the data
sources to query
q y
FAST – NU, Islamabad, Fall 2008
WHERE clause
the triple/graph pattern
to be matched against the
g
triples/graphs of RDF
FAST – NU, Islamabad, Fall 2008
WHERE clause
a conjunction of triples:
{ ?x rdf:type ex:Person
?x ex:name ?name }
FAST – NU, Islamabad, Fall 2008
PREFIX
to declare the schema
used in the query
q y
FAST – NU, Islamabad, Fall 2008
example : persons and their names
PREFIX ex: <http://inria.fr/schema#>
SELECT ?person ?name
WHERE {
?person rdf:type ex:Person
?person ex:name ?name .
}
FAST – NU, Islamabad, Fall 2008
Example or Result
<?xml version="1.0"?>
<sparql xmlns="http://www.w3.org/2005/sparql-results#" >
<head>
<variable name="person"/>
<variable name="name"/>
</head>
<results ordered="false" distinct="false">
<result>
<binding name="person">
<uri>http://inria.fr/schema#fg</uri>
</binding>
<binding name="name">
<literal>gandon</literal>
</binding>
</result>
<result> ...
FAST – NU, Islamabad, Fall 2008
FILTER
to add constraints to the
g
graph pattern (e.g.,
p p ( g,
numerical like X>17 )
FAST – NU, Islamabad, Fall 2008 15
Example: persons at least 18‐year old
PREFIX ex: <http://inria.fr/schema#
http://inria fr/schema#>
SELECT ?person ?name
WHERE {
?person rdf:type ex:Person
?person ex:name ?name .
?person ex:age ?age .
FILTER (?age > 17)
}
FAST – NU, Islamabad, Fall 2008 16
FILTER can use many
operators, functions (e.g.,
operators functions (e g
regular expressions), and
even users' extensions
FAST – NU, Islamabad, Fall 2008 17
OPTIONAL
to make the matching of a
part of the pattern
p p
optional
FAST – NU, Islamabad, Fall 2008 18
Example:: retrieve the age if available
PREFIX ex:
e #>
<http://inria.fr/schema#
htt //i i f / h
SELECT ?person ?name ?age
WHERE {
?person rdf:type ex:Person
?person ex:name ?name .
OPTIONAL { ?person ex:age ?age }
}
FAST – NU, Islamabad, Fall 2008 19
UNION
to give alternative
patterns in a query
p q y
FAST – NU, Islamabad, Fall 2008 20
Example: explicit or implicit adults
PREFIX ex: <http://inria.fr/schema#>
SELECT ?name
WHERE {
?person ex:name ?name .
{
{ ?person rdf:type ex:Adult }
UNION
{ ?person ex:age ?age
FILTER (?age > 17) }
}
}
FAST – NU, Islamabad, Fall 2008 21
Sequence & modify
ORDER BY to sort
LIMIT result number
OFFSET rank of first result
FAST – NU, Islamabad, Fall 2008 22
Example: results 21 to 40 ordered by name
PREFIX ex: <http://inria.fr/schema#>
SELECT ?person ?name
WHERE {
?person rdf:type ex:Person
?person ex:name ?name .
}
ORDER BY ?name
LIMIT 20
OFFSET 20
FAST – NU, Islamabad, Fall 2008 23
UNBOUND
test a variable is not bound ;
used for negation as failure
FAST – NU, Islamabad, Fall 2008 24
Example: persons who are not known authors
PREFIX ex: <http://inria.fr/schema#
htt //i i f / h #>
SELECT ?name
WHERE {
?person ex:name ?name .
OPTIONAL { ?person ex:author ?x }
FILTER ( ! b
bound(?x))
d( )
}
FAST – NU, Islamabad, Fall 2008 25
NEGATION
is tricky and errors can easily be made.
26
PREFIX ex: <http://inria.fr/schema#>
SELECT ?name
WHERE {
?person ex:name ?name .
?person ex:knows ?x
FILTER ( ?x != "Java" )
}
does this find persons who do not know "java" ?
? FAST – NU, Islamabad, Fall 2008 27
NO! also persons who know something else !
PREFIX ex: <http://inria.fr/schema#>
SELECT ?name
WHERE {
?person ex:name ?name .
?person ex:knows ?x
FILTER ( ?x != "Java" )
}
Amna ex:knows "Java"
Amna ex:knows "C++"
Amna is a answer...
FAST – NU, Islamabad, Fall 2008 28
persons who are not known to know
YES! "java" ... negation of an option...
PREFIX ex: <http://inria.fr/schema#>
SELECT ?name
WHERE {
?person ex:name ?name .
OPTIONAL { ?person ex:knows ?x
FILTER ( ?x = "Java" ) }
FILTER ( ! bound(?x) )
}
FAST – NU, Islamabad, Fall 2008 29
ASK
to check just if there is at
least one answer ; result is
;
"true" or "false"
FAST – NU, Islamabad, Fall 2008
Example: is there a person older than 17 ?
PREFIX ex: <http://inria.fr/schema#>
ASK
{
?person ex:age ?age
FILTER (?
(?age > 17)
}
FAST – NU, Islamabad, Fall 2008
CONSTRUCT
return a specific RDF
graph for each result
g p
FAST – NU, Islamabad, Fall 2008
return instances of adults for
Example:
persons older than 17
PREFIX ex: <http://inria.fr/schema#>
CONSTRUCT
{
?person rdf:type ex:Adult
}
WHERE
{
?person ex:age ?age
FILTER (?age > 17)
}
FAST – NU, Islamabad, Fall 2008
SPARQL protocol
sending queries and their
results accross the web
FAST – NU, Islamabad, Fall 2008
Example: with HTTP Binding
GET /sparql/?query=<encoded query>
HTTP/1.1
Host: www.inria.fr
User-agent: my-sparql-client/0.1
FAST – NU, Islamabad, Fall 2008
Example: with SOAP Binding
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope
xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<query-request
<query request xmlns
xmlns="http://www.w3.org/2005/09/sparql-
http://www.w3.org/2005/09/sparql
protocol-types/#">
<query>SELECT ?x ?p ?y WHERE {?x ?p ?y}</query>
</query-request>
/
</soapenv:Body>
</soapenv:Envelope>
/ p p
FAST – NU, Islamabad, Fall 2008
SPARQL as a Protocol
A way of communication between parties
that run SPARQL queries.
th t SPARQL i
Defining a way of invoking the service.
Bindings of a transport protocol for that
goal.
FAST – NU, Islamabad, Fall 2008 37
SPARQL Protocol (1)
WSDL description file:
à Description of the protocol.
i i f h l
à Not for human understanding.
HTTP binding:
bi di
à Specify how to encode SPARQL queries in URLs
with GET and POST methods.
ith GET d POST th d
SOAP binding:
à Specify the SOAP message format
S if h SOAP f
(XML message exchange format for queries)
FAST – NU, Islamabad, Fall 2008 38
WSDL Description file
WSDL description file: (HTTP binding part)
<binding name="queryHttp"
bi di " Htt "
interface="tns:SparqlQuery“
… > >
<fault name="MalformedQuery" whttp:zode="400"/>
<!‐‐ the GET binding for query operation ‐‐>
<operation ref="tns:query" whttp:method="GET"
whttp:inputSerialization=“…" />
whttp:inputSerialization= />
</binding>
FAST – NU, Islamabad, Fall 2008 39
SPARQL Protocol (2)
Interface SparqlQuery
à Only one operation: query
l i
For transferring string query
à Data types: via XML schema
D t t i XML h
à Bindings: HTTP / SOAP binding for invokable
operations.
operations
A service must support SparqlQuery
interface
à support the bindings as described in the WSDL.
FAST – NU, Islamabad, Fall 2008 40
SPARQL Protocol Examples
Examples after we cover SPARQL Query
Language for RDF.
L f RDF
FAST – NU, Islamabad, Fall 2008 41
SPARQL Query Language
A standard query language in the form of
expressive query
i against the RDF data
i t th RDF d t
model…
Data access language
D t l
Graph patterns
Powerful than XML queries in some aspects
FAST – NU, Islamabad, Fall 2008 42
SPARQL Query Language (1)
SQL:
à Internals of DB (tables, fields, data, meaning)
à Impossible to query databases on the
WILD WILD WEB.
So, what does SPARQL propose?
So what does SPARQL propose?
à URIs.
à Querying databases globally.
Querying databases globally
à Combining data globally.
à Value of data grows exponentially with the ways
you combine it.
FAST – NU, Islamabad, Fall 2008 43
The Wild Wild Web
SQL SPARQL
XML
FAST – NU, Islamabad, Fall 2008 44
SPARQL Query Language (2)
Data structure definitions are being
developed worldwide in a distributed
d l d ld id i di t ib t d
manner.
Common ontologies (Dublin Core, Foaf,
C t l i (D bli C F f
DOAP, etc.)
A database publishes the ontologies it
A d b bli h h l i i
exports to
An application queries it using those
ontologies.
FAST – NU, Islamabad, Fall 2008 45
Power of SPARQL Illustrated
Ask fotograf.com if it has a picture which
matches some constraints such as title,
t h t i t h titl
date, size, and some other tag…
Then ask google for URLs in relation to the
Th k l f URL i l ti t th
tag we specified.
And turn the results of these two
A d h l f h
uncoordinated data as an RSS feed on your
site.
site
All this in just two‐three SPARQL queries.
FAST – NU, Islamabad, Fall 2008 46
Power of SPARQL (2)
Ask music cds of price less than 10
You can run this query against
hepsiburada.com, amazon, e‐bay,
gittigidiyor.com or any other seller on earth
itti idi th ll th
who has a website and a database.
No seller needs to change their databases.
N ll d h h i d b
Seller needs: Conversion layer between
ontologies and database.
Client needs: connectivity framework (like
JDBC) for java.
FAST – NU, Islamabad, Fall 2008 47
Power of SPARQL (2) Imp.
PREFIX dc:
<http://purl.org/dc/elements/1.1/>
htt // l /d / l t / / PREFIX
ns: <http://example.org/ns#>
SELECT ?title ?price
SELECT ?titl ? i
WHERE {
?x ns:price ?price .
?x ns:price ?price
FILTER (?price < 10) .
?x dc:title ?title
?x dc:title ?title .
}
FAST – NU, Islamabad, Fall 2008 48
SPARQL Syntax ‐brief‐1
URIs
à <URI> in < >
i
à or @PREFIX prefix: <http://....>
à prefix:name for full URI
fi f f ll URI
Literals
à “Literal“ or “Literal”@language
Blank Node
à _:name or [ ] for a Blank Node used just once
FAST – NU, Islamabad, Fall 2008 49
SPARQL Syntax ‐brief‐2
Triples and .
à :x :y :z . :t :q :s .
Common predicate and subject:
à :x :y :z, :t .which is the same as :x :y :z . :x :y :t .
Common subject:
RDF Collections
à :x :y ( :z :t :q :s )
which is short for many triples (as lists in LISP)
FAST – NU, Islamabad, Fall 2008 50
A walkthrough example illustrating the
power of SPARQL
XML/SQL SPARQL
FAST – NU, Islamabad, Fall 2008 51
Walkthrough example (1xml)
<Person>
<name>Henry Story</name>
<mbox>hs@bblfish.net</mbox>
<knows>
<Person>
<name>Tim Bray</name>
i
<mbox>tb@eg.com</mbox>
</Person>
<Person>
<name>Jonathan Story</name>
<mbox>js@eg.edu</mbox>
</Person>
</knows>
</Person>
FAST – NU, Islamabad, Fall 2008 52
Walkthrough example (1sparql)
[ a :Person;
:name "Henry Story";
:mbox <mailto:hs@insead.edu>;
:knows [ a :Person;
:name "Tim Bray";
:mbox <mailto:tb@eg.com
b il b@ ]
];
:knows [ a :Person;
:name "Jonathan Story";
name "Jonathan Stor "
:mbox <mailto:js@eg.edu> ];
]
] .
FAST – NU, Islamabad, Fall 2008 53
Graph representation
FAST – NU, Islamabad, Fall 2008 54
Walkthrough example (2)
<AddressBook>
<Person>
<name>Jonathan Story</name>
<mbox>Jonathan.Story@eg.edu</mbox>
<address>
<Country>France</Country>
</address>
</Person>
<Person>
<name>Tim Bray</name>
<mbox>Tim.Bray@eg.Com</mbox>
<address>
dd
<Country>Canada</Country>
</address>
</Person>
</AddressBook>
FAST – NU, Islamabad, Fall 2008 55
Walkthrough example (2sparql)
[ a :Person;
:name "Tim Bray";
:mbox <mailto:Tim.Bray@eg.com>
:address [ a :Address;
:country "Canada"@en ] ].
[[ a :Person;
;
:name "Jonathan Story";
:mbox <mailto:Jonathan.Story@eg.edu>
:address [
a :Address;
:country "France"@en ]
].
FAST – NU, Islamabad, Fall 2008 56
Graph representation
FAST – NU, Islamabad, Fall 2008 57
Walkthrough example (2)
These graphs can be merged into the
following graph especially if the mbox
f ll i h i ll if th b
relation is stated as being inverse functional
FAST – NU, Islamabad, Fall 2008 58
Graph representation(merged)
FAST – NU, Islamabad, Fall 2008 59
Walkthrough example (2)
"Who does Henry know who lives in Canada,
and what is their e‐mail address?"
d h t i th i il dd ?"
Can only be answered by aggregating data
f
from both documents.
b th d t
Can not be done using the XML query
l
languages, which can only work
hi h l k on the
h
surface of the document.
FAST – NU, Islamabad, Fall 2008 60
Walkthrough example (2sparql)
SELECT ?name ?mail
WHERE {
[a :Person;
:name "Henry Story";
:knows [
[ :name ?name; ;
:mbox ?mail;
:address [ a :Address;
:country "Canada"@en;
] ] ]
] ] ].
} 61
FAST – NU, Islamabad, Fall 2008
Walkthrough example (3sparql)
Names and websites of contributors to
PlanetRDF
Pl tRDF
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?
SELECT ?name ?website
? b it
FROM <http://planetrdf.com/bloggers.rdf>
WHERE {
?person foaf:weblog ?website ;
foaf:name ?name .
?website a foaf:Document }
FAST – NU, Islamabad, Fall 2008 62
Protocol Example (1)
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT ?who ?g ?mbox
FROM <http://my.example/publishers>
FROM NAMED <http://my.example/alice>
p // y p /
FROM NAMED <http://my.example/bob>
WHERE {
?g dc:publisher ?who .
GRAPH ? { ? f f b ? b } }
GRAPH ?g { ?x foaf:mbox ?mbox } }
FAST – NU, Islamabad, Fall 2008 63
Protocol Example (1)
HTTP/1.1 200 OK
Date: Wed, 27
d Dec 2005 12:48:25 GMT
8 G
Server: Apache/1.3.29 (Unix) PHP/4.3.4 DAV/1.0.3
Connection: closeContent‐Type:
application/sparql‐results+xml; charset=utf‐8
<?xml version="1.0"?>
l i
<sparql xmlns=“…"> … …
</sparql>
FAST – NU, Islamabad, Fall 2008 64
References
http://www.w3.org/2004/Talks/17Dec‐
sparql/intro/all.html
l/i t / ll ht l
http://jena.sourceforge.net/ARQ/Tutorial/
http://blogs.sun.com/roller/page/bblfish
http://xmlarmyknife.org/api/rdf/sparql
http://xml.com/lpt/a/2005/11/16/introducing
‐sparql‐querying‐semantic‐web‐
tutorial.html
http://www.w3.org/2005/Talks/12May‐
p 3 g 5 y
SPARQL/all.html
FAST – NU, Islamabad, Fall 2008 65
References (2)
http://www‐
128.ibm.com/developerworks/xml/library/j‐
8 ib /d l k / l/lib /j
sparql/
http://www.w3.org/TR/rdf‐sparql‐protocol/
htt // /TR/ df l t l/
http://www.w3.org/2004/Talks/17Dec‐
sparql/intro/ex1.rq
l/i /
http://www.oreillynet.com/pub/wlg/7823
FAST – NU, Islamabad, Fall 2008 66
THANK YOU…
For your attendance
and patience
67
ANY QUESTIONS?
68
DUA ONTOLOGY
Taxonomy
Dua
Qurani Duas Masnoon Duas
Duas To Seek
DuasToSeekParadise ….
K
Knowledge
l d
FAST – NU, Islamabad, Fall 2008
RDF Model of Duas
containsDua isRelatedTo
Surah Dua
Prophet
isContainedIn
hasRelatedDua
hasTheme
Theme
FAST – NU, Islamabad, Fall 2008
SAMPLE SPARQL QUERIES
Search Dua with a Particular Theme
PREFIX dua:
<http://www.semanticweb.org/ontologies/2
htt // ti b / t l i /
008/9/DuaOntology.owl#>
SELECT ?D ?theme
WHERE { ?D dua:hasTheme ?theme.}
FAST – NU, Islamabad, Fall 2008
SAMPLE SPARQL QUERIES
é é
Tested with Protégé 3.3.1
PizzaOntology
Querying for Classes
FAST – NU, Islamabad, Fall 2008
SELECT ?subject ?object
WHERE { ?subject rdfs:subClassOf ?object }
FAST – NU, Islamabad, Fall 2008
PREFIX p: <http://www.co‐
ode.org/ontologies/pizza/2005/10/18/pizza.o
d / t l i / i / / / 8/ i
wl#>
SELECT ?subject ?object
SELECT ? bj t ? bj t
WHERE { ?subject rdfs:subClassOf ?object }
FAST – NU, Islamabad, Fall 2008
PREFIX p: <http://www.co‐
ode.org/ontologies/pizza/2005/10/18/pizza.o
d / t l i / i / / / 8/ i
wl#>
SELECT ?subject ?object
SELECT ? bj t ? bj t
WHERE { ?subject rdfs:subClassOf p:Pizza}
FAST – NU, Islamabad, Fall 2008
PREFIX p: <http://www.co‐
ode.org/ontologies/pizza/2005/10/18/pizza.o
d / t l i / i / / / 8/ i
wl#>
SELECT ?subject ?object
SELECT ? bj t ? bj t
WHERE { ?subject rdfs:subClassOf
p:PizzaTopping.
Pi T i
}
FAST – NU, Islamabad, Fall 2008
PREFIX p: <http://www.co‐
ode.org/ontologies/pizza/2005/10/18/pizza.o
ode org/ontologies/pizza/2005/10/18/pizza o
wl#>
SELECT ?subject ?object
WHERE { ?subject rdfs:subClassOf
p
p:DomainConcept.p
?subject owl:disjointWith ?object.
}
FAST – NU, Islamabad, Fall 2008
Dua Ontology
Querying on Instance Data
FAST – NU, Islamabad, Fall 2008
List all Duas with Corresponding themes
PREFIX dua:
<http://www.semanticweb.org/ontologies/2
htt // ti b / t l i /
008/9/DuaOntology.owl#>
SELECT ?D ?theme
WHERE { ?D dua:hasTheme ?theme.}
FAST – NU, Islamabad, Fall 2008
Example: List of Duas, their Themes which are
contained in Surah Taha
PREFIX dua:
<http://www.semanticweb.org/ontologies/20
htt // ti b / t l i /
08/9/DuaOntology.owl#>
SELECT ?D ?theme
WHERE { ?D dua:hasTheme ?theme.
?D dua:hasSourceSurah dua:Taha. }
FAST – NU, Islamabad, Fall 2008
List of All Dua’s which have some Theme and
have some source Surah
PREFIX dua:
<http://www.semanticweb.org/ontologies/20
htt // ti b / t l i /
08/9/DuaOntology.owl#>
SELECT ?D ?theme ?surah
WHERE { ?D dua:hasTheme ?theme.
?D dua:hasSourceSurah ?surah.}
FAST – NU, Islamabad, Fall 2008
PREFIX dua:
<http://www.semanticweb.org/ontologies/2008/9/DuaOntology.owl#>
SELECT ?D ?theme ?surah ?no
WHERE { ?D dua:hasTheme ?theme.
WHERE { ?D dua:hasTheme ?theme
?D dua:hasSourceSurah ?surah .
?surah dua:hasSurahNo ?no
?surah dua:hasSurahNo ?no.
}
ORDER BY ?no
FAST – NU, Islamabad, Fall 2008
PREFIX dua:
<http://www.semanticweb.org/ontologies/2008/9/DuaOntology.owl#>
SELECT ?D ?theme ?surah ?no ?ayah
WHERE { ?D dua:hasTheme ?theme.
WHERE { ?D dua:hasTheme ?theme
?D dua:hasSourceSurah ?surah ;
dua:hasSourceAyah ?ayah.
?surah dua:hasSurahNo ?no
?surah dua:hasSurahNo ?no.
}
ORDER BY ?no
FAST – NU, Islamabad, Fall 2008
Notice the change in results with the use
of OPTIONAL
PREFIX dua:
<http://www.semanticweb.org/ontologies/2008/9/DuaOntology.owl#>
SELECT ?D ?theme ?surah ?no ?ayah
WHERE{ ?D dua:hasTheme ?theme.
?D dua:hasSourceSurah ?surah.
?surah dua:hasSurahNo ?no.
OPTIONAL{ ?D dua:hasSourceAyah ?ayah.
}}
ORDER BY ?no
FAST – NU, Islamabad, Fall 2008
Use of Regular Expression to retrieve duas
that contain the word Rabbi
PREFIX dua:
<http://www.semanticweb.org/ontologies/2008/9/DuaOntology.
owl#>
l
SELECT ?D ?surah ?no ?text
WHERE{
?D dua:hasSourceSurah ?surah
?D dua:hasSourceSurah ?surah.
?D dua:hasTextAyah ?text.
FILTER regex(?text, "Rabbi") .
?surah dua:hasSurahNo ?no.
}
FAST – NU, Islamabad, Fall 2008
Use of Regular Expression to retrieve duas
that contain the word Rabbana
PREFIX dua:
<http://www.semanticweb.org/ontologies/2008/9/DuaOntology.owl#>
SELECT ?D ?surah ?no ?text
WHERE{
?D dua:hasSourceSurah ?surah.
?D dua:hasTextAyah ?text.
FILTER regex(?text "Rabbana")
FILTER regex(?text, Rabbana ) .
?surah dua:hasSurahNo ?no.
}
FAST – NU, Islamabad, Fall 2008
AN interesting Query!. …Think about
what it does!
PREFIX dua:
<http://www.semanticweb.org/ontologies/2008/9/DuaOntology.owl#>
SELECT ?Dua ?DuaOther ?surah2
WHERE{
?Dua dua:hasSourceSurah dua:Taha.
?Dua dua:isRelatedTo ?p.
?DuaOther dua:isRelatedTo ?p
?DuaOther dua:isRelatedTo ?p.
?DuaOther dua:hasSourceSurah ?surah2.
}
FAST – NU, Islamabad, Fall 2008
Find other Duas related to the same
prophet in a given Surah
“Find other duas related with the prophet of a
given surah.
given surah ”
FAST – NU, Islamabad, Fall 2008