The document discusses SPARQL, a query language for RDF. It describes the different types of SPARQL queries including ASK, SELECT, DESCRIBE, and CONSTRUCT. It provides examples of SPARQL triple patterns and example queries using the space ontology to retrieve information about spacecraft, launches, and disciplines.
1. An
Introduction
to
Linked Data
Part 4 of 5 David Hyland-Wood
SPARQL Query Language University of Mary Washington
Monday, May 16, 2011
2. SPARQL:
A query
language for RDF
Monday, May 16, 2011
3. SPARQL
• Query Language for RDF
http://www.w3.org/TR/rdf-sparql-query
• SPARQL Protocol and RDF Query
Language
• Graph pattern based language
written the same as Turtle
• Several queries — Ask; Select;
Describe; Construct
• Read Only
still vulnerable to injection, though not as bad as SQL
Monday, May 16, 2011
4. Queries
• ASK — True or False
Simple, Fast
• SELECT — Tabular data
easy to work with
• DESCRIBE — RDF
whatever the store has
• CONSTRUCT — RDF
Whatever you want
Monday, May 16, 2011
5. a /discipline/ rdfs:label
space:Discipline Space Physics
spacephysics
foaf:name
Sputnik 1
space:discipline
dc:description
The Sputnik 1 spacecraft was ...
space:agency
U.S.S.R
a /spacecraft/
space:Spacecraft
1957-001B space:mass
86.3
foaf:depiction
/images/sputnik.jpg
space:spacecraft
space:launched
1957-10-04
a
space:Launch /launch/1957-001
space:launchvehicle
Modified SS-6 (Sapwood)
space:launchsite
rdfs:label
Tyuratam (Baikonur Cosmodrome)
a
space:Launchsite /launchsite/tyurata
space:country
U.S.S.R
Monday, May 16, 2011
41. Example NOT
PREFIX space: <http://purl.org/net/schemas/space/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?s WHERE {
?s a space:Spacecraft .
OPTIONAL {
?s foaf:depiction ?image .
}
FILTER ( !bound(?image) )
}
Monday, May 16, 2011
42. Queries
• ASK — True or False
Simple, Fast
• SELECT — Tabular data
easy to work with
• DESCRIBE — RDF
whatever the store has
• CONSTRUCT — RDF
Whatever you want
Monday, May 16, 2011
43. Example DESCRIBE
DESCRIBE <http://example.com/launch/1969-059>
Monday, May 16, 2011
49. a /discipline/ rdfs:label
space:Discipline Space Physics
spacephysics
foaf:name
Sputnik 1
space:discipline
dc:description
The Sputnik 1 spacecraft was ...
space:agency
U.S.S.R
a /spacecraft/
space:Spacecraft
1957-001B space:mass
86.3
foaf:depiction
/images/sputnik.jpg
space:spacecraft
space:launched
1957-10-04
a
space:Launch /launch/1957-001
space:launchvehicle
Modified SS-6 (Sapwood)
space:launchsite
rdfs:label
Tyuratam (Baikonur Cosmodrome)
a
space:Launchsite /launchsite/tyurata
space:country
U.S.S.R
Monday, May 16, 2011
50. a /discipline/ rdfs:label
space:Discipline Space Physics
spacephysics
foaf:name
space:discipline Sputnik 1
dc:description
The Sputnik 1 spacecraft was ...
space:agency
U.S.S.R
a /spacecraft/
space:Spacecraft
1957-001B space:mass
86.3
foaf:depiction
/images/sputnik.jpg
space:spacecraft
space:launched
1957-10-04
a
space:Launch /launch/1957-001
space:launchvehicle
Modified SS-6 (Sapwood)
space:launchsite
rdfs:label
Tyuratam (Baikonur Cosmodrome)
a
space:Launchsite /launchsite/tyurata
space:country
U.S.S.R
Monday, May 16, 2011
51. a /discipline/ rdfs:label
space:Discipline Space Physics
spacephysics
foaf:name
Sputnik 1
space:discipline
dc:description
The Sputnik 1 spacecraft was ...
space:agency
U.S.S.R
a /spacecraft/
space:Spacecraft
1957-001B space:mass
86.3
foaf:depiction
/images/sputnik.jpg
space:spacecraft
space:launched
space:launched
1957-10-04
a
space:Launch /launch/1957-001
space:launchvehicle
Modified SS-6 (Sapwood)
space:launchsite
rdfs:label
a Tyuratam (Baikonur Cosmodrome)
space:Launchsite /launchsite/tyurata
space:country
U.S.S.R
Monday, May 16, 2011
52. Common Problems
• Typos
wrong case for property or class name
• wrong prefix URI
missing # or / or the URI for the web page
• Missing OPTIONAL
you won't even notice...
• Comparing Literals
Literals are compared on all of value, type and language
Monday, May 16, 2011
53. Tip 1: Find all the Properties
SELECT DISTINCT ?p WHERE {
?s ?p ?o .
}
Monday, May 16, 2011
54. a /discipline/ rdfs:label
space:Discipline Space Physics
spacephysics
foaf:name
Sputnik 1
space:discipline
dc:description
The Sputnik 1 spacecraft was ...
space:agency
U.S.S.R
a /spacecraft/
space:Spacecraft
1957-001B space:mass
86.3
foaf:depiction
/images/sputnik.jpg
space:spacecraft
space:launched
1957-10-04
a
space:Launch /launch/1957-001
space:launchvehicle
Modified SS-6 (Sapwood)
space:launchsite
rdfs:label
a Tyuratam (Baikonur Cosmodrome)
space:Launchsite /launchsite/tyurata
space:country
U.S.S.R
Monday, May 16, 2011
55. a rdfs:label
/discipline/
space:Discipline Space Physics
spacephysics
foaf:name
Sputnik 1
space:discipline
dc:description
The Sputnik 1 spacecraft was ...
space:agency
U.S.S.R
a /spacecraft/
space:Spacecraft
1957-001B space:mass
86.3
foaf:depiction
/images/sputnik.jpg
space:spacecraft
space:launched
a 1957-10-04
space:Launch /launch/1957-001
space:launchvehicle
Modified SS-6 (Sapwood)
space:launchsite
rdfs:label
a Tyuratam (Baikonur Cosmodrome)
space:Launchsite /launchsite/tyurata
space:country
U.S.S.R
Monday, May 16, 2011
56. Tip 2: Find all the Classes
SELECT DISTINCT ?type WHERE {
?s a ?type .
}
Monday, May 16, 2011
57. a /discipline/ rdfs:label
space:Discipline Space Physics
spacephysics
foaf:name
Sputnik 1
space:discipline
dc:description
The Sputnik 1 spacecraft was ...
space:agency
U.S.S.R
a /spacecraft/
space:Spacecraft
1957-001B space:mass
86.3
foaf:depiction
/images/sputnik.jpg
space:spacecraft
space:launched
1957-10-04
a
space:Launch /launch/1957-001
space:launchvehicle
Modified SS-6 (Sapwood)
space:launchsite
rdfs:label
a Tyuratam (Baikonur Cosmodrome)
space:Launchsite /launchsite/tyurata
space:country
U.S.S.R
Monday, May 16, 2011
58. a /discipline/ rdfs:label
space:Discipline Space Physics
spacephysics
foaf:name
Sputnik 1
space:discipline
dc:description
The Sputnik 1 spacecraft was ...
space:agency
U.S.S.R
a /spacecraft/
space:Spacecraft
1957-001B space:mass
86.3
foaf:depiction
/images/sputnik.jpg
space:spacecraft
space:launched
1957-10-04
a
space:Launch /launch/1957-001
space:launchvehicle
Modified SS-6 (Sapwood)
space:launchsite
rdfs:label
a Tyuratam (Baikonur Cosmodrome)
space:Launchsite /launchsite/tyurata
space:country
U.S.S.R
Monday, May 16, 2011
59. Thanks to
• Rob Styles, Talis Group Ltd for his CC-BY
licensed “Two Day Introduction to the Web of
Data” slides and spacecraft example data.
Monday, May 16, 2011