sparql in a nutshell
TRANSCRIPT
![Page 1: SPARQL in a nutshell](https://reader034.vdocuments.us/reader034/viewer/2022042607/55a574191a28aba7118b460c/html5/thumbnails/1.jpg)
fabien, gandon, inriaSSPARPARQLQLin a nutshellin a nutshell
![Page 2: SPARQL in a nutshell](https://reader034.vdocuments.us/reader034/viewer/2022042607/55a574191a28aba7118b460c/html5/thumbnails/2.jpg)
RDF triple model is the first layer of the semantic web standards
2
![Page 3: SPARQL in a nutshell](https://reader034.vdocuments.us/reader034/viewer/2022042607/55a574191a28aba7118b460c/html5/thumbnails/3.jpg)
SPARQL on top...an RDF query language and data access protocol
3
![Page 4: SPARQL in a nutshell](https://reader034.vdocuments.us/reader034/viewer/2022042607/55a574191a28aba7118b460c/html5/thumbnails/4.jpg)
SPARQL stands forSPARQL Protocol and RDF Query Language
4
![Page 5: SPARQL in a nutshell](https://reader034.vdocuments.us/reader034/viewer/2022042607/55a574191a28aba7118b460c/html5/thumbnails/5.jpg)
SPARQL in 3 partspart 1: query languagepart 2: result formatpart 3: access protocol
5
![Page 6: SPARQL in a nutshell](https://reader034.vdocuments.us/reader034/viewer/2022042607/55a574191a28aba7118b460c/html5/thumbnails/6.jpg)
SPARQL querySELECT ...
FROM ...
WHERE { ... }6
![Page 7: SPARQL in a nutshell](https://reader034.vdocuments.us/reader034/viewer/2022042607/55a574191a28aba7118b460c/html5/thumbnails/7.jpg)
SELECT clauseto identify the values to be returned
7
![Page 8: SPARQL in a nutshell](https://reader034.vdocuments.us/reader034/viewer/2022042607/55a574191a28aba7118b460c/html5/thumbnails/8.jpg)
FROM clauseto identify the data sources to query
8
![Page 9: SPARQL in a nutshell](https://reader034.vdocuments.us/reader034/viewer/2022042607/55a574191a28aba7118b460c/html5/thumbnails/9.jpg)
WHERE clausethe triple/graph pattern to be matched against the triples/graphs of RDF
9
![Page 10: SPARQL in a nutshell](https://reader034.vdocuments.us/reader034/viewer/2022042607/55a574191a28aba7118b460c/html5/thumbnails/10.jpg)
WHERE clausea conjunction of triples:{ ?x rdf:type ex:Person
?x ex:name ?name }10
![Page 11: SPARQL in a nutshell](https://reader034.vdocuments.us/reader034/viewer/2022042607/55a574191a28aba7118b460c/html5/thumbnails/11.jpg)
PREFIXto declare the schema used in the query
11
![Page 12: SPARQL in a nutshell](https://reader034.vdocuments.us/reader034/viewer/2022042607/55a574191a28aba7118b460c/html5/thumbnails/12.jpg)
example persons and their names
12
PREFIX ex: <http://inria.fr/schema#>SELECT ?person ?nameWHERE { ?person rdf:type ex:Person ?person ex:name ?name .}
![Page 13: SPARQL in a nutshell](https://reader034.vdocuments.us/reader034/viewer/2022042607/55a574191a28aba7118b460c/html5/thumbnails/13.jpg)
example of result
13
<?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> ...
![Page 14: SPARQL in a nutshell](https://reader034.vdocuments.us/reader034/viewer/2022042607/55a574191a28aba7118b460c/html5/thumbnails/14.jpg)
FILTERto add constraints to the graph pattern (e.g., numerical like X>17 )
14
![Page 15: SPARQL in a nutshell](https://reader034.vdocuments.us/reader034/viewer/2022042607/55a574191a28aba7118b460c/html5/thumbnails/15.jpg)
example persons at least 18-year old
15
PREFIX ex: <http://inria.fr/schema#>SELECT ?person ?nameWHERE { ?person rdf:type ex:Person ?person ex:name ?name . ?person ex:age ?age . FILTER (?age > 17)}
![Page 16: SPARQL in a nutshell](https://reader034.vdocuments.us/reader034/viewer/2022042607/55a574191a28aba7118b460c/html5/thumbnails/16.jpg)
FILTER can use many operators, functions (e.g., regular expressions), and even users' extensions
16
![Page 17: SPARQL in a nutshell](https://reader034.vdocuments.us/reader034/viewer/2022042607/55a574191a28aba7118b460c/html5/thumbnails/17.jpg)
OPTIONALto make the matching of a part of the pattern optional
17
![Page 18: SPARQL in a nutshell](https://reader034.vdocuments.us/reader034/viewer/2022042607/55a574191a28aba7118b460c/html5/thumbnails/18.jpg)
example retrieve the age if available
18
PREFIX ex: <http://inria.fr/schema#>SELECT ?person ?name ?ageWHERE { ?person rdf:type ex:Person ?person ex:name ?name . OPTIONAL { ?person ex:age ?age }}
![Page 19: SPARQL in a nutshell](https://reader034.vdocuments.us/reader034/viewer/2022042607/55a574191a28aba7118b460c/html5/thumbnails/19.jpg)
UNIONto give alternative patterns in a query
19
![Page 20: SPARQL in a nutshell](https://reader034.vdocuments.us/reader034/viewer/2022042607/55a574191a28aba7118b460c/html5/thumbnails/20.jpg)
example explicit or implicit adults
20
PREFIX ex: <http://inria.fr/schema#>SELECT ?nameWHERE { ?person ex:name ?name . { { ?person rdf:type ex:Adult } UNION { ?person ex:age ?age FILTER (?age > 17) } }}
![Page 21: SPARQL in a nutshell](https://reader034.vdocuments.us/reader034/viewer/2022042607/55a574191a28aba7118b460c/html5/thumbnails/21.jpg)
Sequence & modify ORDER BY to sortLIMIT result numberOFFSET rank of first result
21
![Page 22: SPARQL in a nutshell](https://reader034.vdocuments.us/reader034/viewer/2022042607/55a574191a28aba7118b460c/html5/thumbnails/22.jpg)
example results 21 to 40 ordered by name
22
PREFIX ex: <http://inria.fr/schema#>SELECT ?person ?nameWHERE { ?person rdf:type ex:Person ?person ex:name ?name .}ORDER BY ?nameLIMIT 20OFFSET 20
![Page 23: SPARQL in a nutshell](https://reader034.vdocuments.us/reader034/viewer/2022042607/55a574191a28aba7118b460c/html5/thumbnails/23.jpg)
UNBOUNDtest a variable is not bound ; used for negation as failure
23
![Page 24: SPARQL in a nutshell](https://reader034.vdocuments.us/reader034/viewer/2022042607/55a574191a28aba7118b460c/html5/thumbnails/24.jpg)
example persons who are not known authors
24
PREFIX ex: <http://inria.fr/schema#>SELECT ?nameWHERE { ?person ex:name ?name . OPTIONAL { ?person ex:author ?x }
FILTER ( ! bound(?x))}
![Page 25: SPARQL in a nutshell](https://reader034.vdocuments.us/reader034/viewer/2022042607/55a574191a28aba7118b460c/html5/thumbnails/25.jpg)
negationis tricky and errors can easily be made.
25
![Page 26: SPARQL in a nutshell](https://reader034.vdocuments.us/reader034/viewer/2022042607/55a574191a28aba7118b460c/html5/thumbnails/26.jpg)
?does this find persons who do not know "java" ?26
PREFIX ex: <http://inria.fr/schema#>SELECT ?nameWHERE { ?person ex:name ?name . ?person ex:knows ?x FILTER ( ?x != "Java" )}
![Page 27: SPARQL in a nutshell](https://reader034.vdocuments.us/reader034/viewer/2022042607/55a574191a28aba7118b460c/html5/thumbnails/27.jpg)
NO! also persons who know something else !
27
PREFIX ex: <http://inria.fr/schema#>SELECT ?nameWHERE { ?person ex:name ?name . ?person ex:knows ?x FILTER ( ?x != "Java" )}
fabien ex:knows "Java"fabien ex:knows "C++"fabien is a answer...
![Page 28: SPARQL in a nutshell](https://reader034.vdocuments.us/reader034/viewer/2022042607/55a574191a28aba7118b460c/html5/thumbnails/28.jpg)
YES! persons who are not known to know "java" ... negation of an option...
28
PREFIX ex: <http://inria.fr/schema#>SELECT ?nameWHERE { ?person ex:name ?name . OPTIONAL { ?person ex:knows ?x FILTER ( ?x = "Java" ) } FILTER ( ! bound(?x) )}
![Page 29: SPARQL in a nutshell](https://reader034.vdocuments.us/reader034/viewer/2022042607/55a574191a28aba7118b460c/html5/thumbnails/29.jpg)
ASKto check just if there is at least one answer ; result is "true" or "false"
29
![Page 30: SPARQL in a nutshell](https://reader034.vdocuments.us/reader034/viewer/2022042607/55a574191a28aba7118b460c/html5/thumbnails/30.jpg)
example is there a person older than 17 ?
30
PREFIX ex: <http://inria.fr/schema#>ASK{ ?person ex:age ?age FILTER (?age > 17) }
![Page 31: SPARQL in a nutshell](https://reader034.vdocuments.us/reader034/viewer/2022042607/55a574191a28aba7118b460c/html5/thumbnails/31.jpg)
CONSTRUCTreturn a specific RDF graph for each result
31
![Page 32: SPARQL in a nutshell](https://reader034.vdocuments.us/reader034/viewer/2022042607/55a574191a28aba7118b460c/html5/thumbnails/32.jpg)
example return instances of adults for persons older than 17
32
PREFIX ex: <http://inria.fr/schema#>CONSTRUCT{ ?person rdf:type ex:Adult}WHERE{ ?person ex:age ?age FILTER (?age > 17) }
![Page 33: SPARQL in a nutshell](https://reader034.vdocuments.us/reader034/viewer/2022042607/55a574191a28aba7118b460c/html5/thumbnails/33.jpg)
SPARQL protocolsending queries and their results accross the web
33
![Page 34: SPARQL in a nutshell](https://reader034.vdocuments.us/reader034/viewer/2022042607/55a574191a28aba7118b460c/html5/thumbnails/34.jpg)
examplewith HTTP Binding
34
GET /sparql/?query=<encoded query> HTTP/1.1Host: www.inria.frUser-agent: my-sparql-client/0.1
![Page 35: SPARQL in a nutshell](https://reader034.vdocuments.us/reader034/viewer/2022042607/55a574191a28aba7118b460c/html5/thumbnails/35.jpg)
examplewith SOAP Binding
35
<?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 xmlns="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>
![Page 36: SPARQL in a nutshell](https://reader034.vdocuments.us/reader034/viewer/2022042607/55a574191a28aba7118b460c/html5/thumbnails/36.jpg)
Take-awaysummary of SPARQL
36
![Page 37: SPARQL in a nutshell](https://reader034.vdocuments.us/reader034/viewer/2022042607/55a574191a28aba7118b460c/html5/thumbnails/37.jpg)
SPARQL is...
... a query language ...
... a result format ...
... an access protocol ...
... for RDF 37
![Page 38: SPARQL in a nutshell](https://reader034.vdocuments.us/reader034/viewer/2022042607/55a574191a28aba7118b460c/html5/thumbnails/38.jpg)
SPARQL query languagebased on the triple model ?x ?p ?y
filters to add constraints
optional parts and alternative parts
38
![Page 39: SPARQL in a nutshell](https://reader034.vdocuments.us/reader034/viewer/2022042607/55a574191a28aba7118b460c/html5/thumbnails/39.jpg)
39
fabien, gandon