0
Introduction to SPARQL

Introduction to SPARQL
Overview
•
•
•
•
•
•
•
•

What is SPARQL?
ASK and SELECT query forms
UNION and ORDER BY
Subsets of results
FILTER expressi...
What is SPARQL?
• SPARQL is a query language for RDF data
– http://www.w3.org/TR/rdf-sparql-query/

• It is also a protoco...
Ask
• Did 'Steve Hillage' make the album 'Fish Rising'?
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX foaf: <http:/...
Select
• What albums and tracks did 'Steve Hillage' make?
PREFIX
PREFIX
PREFIX
PREFIX

dc: <http://purl.org/dc/elements/1....
Union
• 'Steve Hillage' and 'Jimi Hendrix'?
PREFIX
PREFIX
PREFIX
PREFIX

dc: <http://purl.org/dc/elements/1.1/>
foaf: <htt...
Order By
• In what order did 'Steve Hillage' release his albums?
PREFIX
PREFIX
PREFIX
PREFIX

dc: <http://purl.org/dc/elem...
Subsets of results
• What albums and tracks did 'Steve Hillage' make?
PREFIX
PREFIX
PREFIX
PREFIX

dc: <http://purl.org/dc...
Filtering solutions
• What are 'Steve Hillage' longer tracks?
PREFIX
PREFIX
PREFIX
PREFIX

dc: <http://purl.org/dc/element...
Functions
• What albums and tracks did 'Steve Hillage' make?
PREFIX
PREFIX
PREFIX
PREFIX

dc: <http://purl.org/dc/elements...
Filter expressions
• Can be combined with logical, arithmetic, casts and
comparisons, e.g.
FILTER( ?age < 30 && xsd:double...
Optionals
• Parts of the matching graph can be optional
• Use OPTIONAL {} to enclose the optional parts
• Can test in filt...
Optionals
• Albums from artists who are not dead
PREFIX
PREFIX
PREFIX
PREFIX
PREFIX

dc: <http://purl.org/dc/elements/1.1/...
Describe
• Returns RDF statements about any resource
identified by the query
PREFIX dbpedia: <http://dbpedia.org/resource/...
Construct
• Returns RDF statements created from variable
bindings
PREFIX
PREFIX
PREFIX
PREFIX

dc: <http://purl.org/dc/ele...
Common Problems
• Spelling mistakes in identifiers are not noticed
– It is simply a different URI
– URIs are LONG, are cas...
Summary
• SPARQL is a powerful query language for RDF
• SPARQL 1.0 is read-only
• SPARQL 1.1 will allow updates

Introduct...
Upcoming SlideShare
Loading in...5
×

ESWC SS 2012 - Monday Tutorial 2 Barry Norton: Introduction to SPARQL

223

Published on

Published in: Business, Real Estate
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
223
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
12
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "ESWC SS 2012 - Monday Tutorial 2 Barry Norton: Introduction to SPARQL"

  1. 1. Introduction to SPARQL Introduction to SPARQL
  2. 2. Overview • • • • • • • • What is SPARQL? ASK and SELECT query forms UNION and ORDER BY Subsets of results FILTER expressions and functions OPTIONAL DESCRIBE and CONSTRUCT query forms Common problems Introduction to SPARQL #2
  3. 3. What is SPARQL? • SPARQL is a query language for RDF data – http://www.w3.org/TR/rdf-sparql-query/ • It is also a protocol • It is designed around graph patterns – Graph patterns use Turtle syntax • SPARQL 1.0 is query only – SPARQL 1.1 (covered next, not yet a recommendation) includes syntax for updates Introduction to SPARQL #3
  4. 4. Ask • Did 'Steve Hillage' make the album 'Fish Rising'? PREFIX dc: <http://purl.org/dc/elements/1.1/> PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX dbpedia: <http://dbpedia.org/resource/> ASK { dbpedia:Steve_Hillage foaf:made ?album . ?album dc:title "Fish Rising" . } • Namespaces are added with 'PREFIX' directive • Statement patterns that make up the graph are between {} brackets Introduction to SPARQL #4
  5. 5. Select • What albums and tracks did 'Steve Hillage' make? PREFIX PREFIX PREFIX PREFIX dc: <http://purl.org/dc/elements/1.1/> foaf: <http://xmlns.com/foaf/0.1/> dbpedia: <http://dbpedia.org/resource/> music-ont: <http://purl.org/ontology/mo/> SELECT ?album_name ?track_title WHERE { dbpedia:Steve_Hillage foaf:made ?album . ?album dc:title ?album_name. ?album music-ont:track ?track . ?track dc:title ?track_title . } • All variables in a query can be selected with '*' – i.e. SELECT * WHERE { … } Introduction to SPARQL #5
  6. 6. Union • 'Steve Hillage' and 'Jimi Hendrix'? PREFIX PREFIX PREFIX PREFIX dc: <http://purl.org/dc/elements/1.1/> foaf: <http://xmlns.com/foaf/0.1/> dbpedia: <http://dbpedia.org/resource/> music-ont: <http://purl.org/ontology/mo/> SELECT ?album_name ?track_title WHERE { { dbpedia:Steve_Hillage foaf:made ?album . } UNION { dbpedia:Jimi_Hendrix foaf:made ?album . } ?album dc:title ?album_name. ?album music-ont:track ?track . ?track dc:title ?track_title . } Introduction to SPARQL #6
  7. 7. Order By • In what order did 'Steve Hillage' release his albums? PREFIX PREFIX PREFIX PREFIX dc: <http://purl.org/dc/elements/1.1/> foaf: <http://xmlns.com/foaf/0.1/> dbpedia: <http://dbpedia.org/resource/> music-ont: <http://purl.org/ontology/mo/> SELECT distinct ?album_name ?date WHERE { dbpedia:Steve_Hillage foaf:made ?album . ?album dc:title ?album_name. ?album music-ont:track ?track . ?album dc:date ?date. } ORDER BY( ?date) Introduction to SPARQL #7
  8. 8. Subsets of results • What albums and tracks did 'Steve Hillage' make? PREFIX PREFIX PREFIX PREFIX dc: <http://purl.org/dc/elements/1.1/> foaf: <http://xmlns.com/foaf/0.1/> dbpedia: <http://dbpedia.org/resource/> music-ont: <http://purl.org/ontology/mo/> SELECT ?album_name ?track_title WHERE { dbpedia:Steve_Hillage foaf:made ?album . ?album dc:title ?album_name. ?album music-ont:track ?track . ?track dc:title ?track_title . } OFFSET 10 LIMIT 15 • Start after the 10th solution, provide (up to) 15 more Introduction to SPARQL #8
  9. 9. Filtering solutions • What are 'Steve Hillage' longer tracks? PREFIX PREFIX PREFIX PREFIX dc: <http://purl.org/dc/elements/1.1/> foaf: <http://xmlns.com/foaf/0.1/> dbpedia: <http://dbpedia.org/resource/> music-ont: <http://purl.org/ontology/mo/> SELECT ?album_name ?track_title ?duration WHERE { dbpedia:Steve_Hillage foaf:made ?album . ?album dc:title ?album_name. ?album music-ont:track ?track . ?track dc:title ?track_title . ?track music-ont:duration ?duration . FILTER( ?duration > 500000 ). } • Allow only those solutions where the track length is more than 500 seconds Introduction to SPARQL #9
  10. 10. Functions • What albums and tracks did 'Steve Hillage' make? PREFIX PREFIX PREFIX PREFIX dc: <http://purl.org/dc/elements/1.1/> foaf: <http://xmlns.com/foaf/0.1/> dbpedia: <http://dbpedia.org/resource/> music-ont: <http://purl.org/ontology/mo/> SELECT ?album_name ?track_title WHERE { dbpedia:Steve_Hillage foaf:made ?album . ?album dc:title ?album_name. ?album music-ont:track ?track . ?track dc:title ?track_title . FILTER( REGEX( ?track_title, "hurd.*", "i") ) } • Allow only those solutions where the track title contains a substring that begins with 'hurd' in any case Introduction to SPARQL #10
  11. 11. Filter expressions • Can be combined with logical, arithmetic, casts and comparisons, e.g. FILTER( ?age < 30 && xsd:double(?weight) < (?empty + ?fuel) ) • Also tests, accessors, e.g. FILTER( isURI( ?id ) && datatype( ?age ) = xsd:double ) Introduction to SPARQL #11
  12. 12. Optionals • Parts of the matching graph can be optional • Use OPTIONAL {} to enclose the optional parts • Can test in filter expressions if variable are bound • Using logical NOT '!' it is possible to filter out solutions for the optional part Introduction to SPARQL #12
  13. 13. Optionals • Albums from artists who are not dead PREFIX PREFIX PREFIX PREFIX PREFIX dc: <http://purl.org/dc/elements/1.1/> foaf: <http://xmlns.com/foaf/0.1/> dbpedia: <http://dbpedia.org/resource/> music-ont: <http://purl.org/ontology/mo/> dbp-ont: <http://dbpedia.org/ontology/> SELECT distinct ?artist_name ?album_name ?place_of_death WHERE { ?artist foaf:made ?album . ?artist foaf:name ?artist_name . ?album dc:title ?album_name. ?album music-ont:track ?track . ?track dc:title ?track_title . OPTIONAL { ?artist dbp-ont:deathPlace ?place_of_death } FILTER( ! BOUND( ?place_of_death ) ). } Introduction to SPARQL #13
  14. 14. Describe • Returns RDF statements about any resource identified by the query PREFIX dbpedia: <http://dbpedia.org/resource/> DESCRIBE dbpedia:Steve_Hillage PREFIX foaf: <http://xmlns.com/foaf/0.1/> DESCRIBE ?x ?y <http://example.org/> WHERE {?x foaf:knows ?y} Introduction to SPARQL #14
  15. 15. Construct • Returns RDF statements created from variable bindings PREFIX PREFIX PREFIX PREFIX dc: <http://purl.org/dc/elements/1.1/> foaf: <http://xmlns.com/foaf/0.1/> dbpedia: <http://dbpedia.org/resource/> music-ont: <http://purl.org/ontology/mo/> CONSTRUCT { ?album dc:creator dbpedia:Steve_Hillage } WHERE { dbpedia:Steve_Hillage foaf:made ?album . ?album dc:title ?album_name. ?album music-ont:track ?track . } Introduction to SPARQL #15
  16. 16. Common Problems • Spelling mistakes in identifiers are not noticed – It is simply a different URI – URIs are LONG, are case-sensitive, have '#' or '/' for local part • Easy to use the right local name with the wrong namespace • Literals are compared on type, language and value – "Steve Hillage"@EN is not the same as "Steve Hillage" – "3" is not the same as "3"^^xsd:integer Introduction to SPARQL #16
  17. 17. Summary • SPARQL is a powerful query language for RDF • SPARQL 1.0 is read-only • SPARQL 1.1 will allow updates Introduction to SPARQL #17
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×