COMP5860M
Semantic Technologies and Applications
Lecture Eleven: SPARQL
John Stell
School of Computing, University of Leeds
SPARQL
• Query Language for RDF
– Based on RDF Data Model Triples
• Possible to write complex joins of disparate datasets
• Implemented by all major RDF databases See: https://www.w3.org/TR/sparql11-overview/
SPARQL Public Endpoints
• http://live.dbpedia.org/sparql
• https://query.wikidata.org/
• https://sparql.uniprot.org/
• http://vocabs.ceh.ac.uk/edg/tbl/swp?_viewClass=endp oint:HomePage
• http://dbtune.org/bbc/peel/cliopatria/yasgui/index.html
• https://collection.britishmuseum.org/sparql
• noguaranteethesewillalwaysfunction(butcorrectas of 2 March 2020)
Structure of a SPARQL Query
SELECT query: Find everything about Concept of “Person” as in Dbpedia
#prefix declaration
prefix dbo:
#result clause
SELECT *
#dataset definition
FROM
#query pattern
WHERE {
dbo:Person ?p ?o.
}
SELECT query: Find the superclasses of Class “Person” as in Dbpedia
#prefix declaration
prefix dbo:
prefix rdfs:
#result clause
SELECT ?superclass
#dataset definition
FROM
#query pattern
WHERE {
dbo:Person rdfs:subClassOf ?superclass. }
Modify to retrieve the sub-classes of person?
SELECT query: Find all persons in Dbpedia
#prefix declaration
prefix dbo:
prefix rdf:
#result clause
SELECT ?Person
#dataset definition
FROM
#query pattern
WHERE {
?Person rdf:type dbo:Person.
}
SELECT query: Find specific types of persons (architects) in Dbpedia
#prefix declaration
prefix dbo:
Prefix rdf:
#result clause
SELECT ?Architect
#dataset definition
FROM
#query pattern
WHERE {
? Architect rdf:type dbo:Person .
? Architect rdf:type dbo:Architect .
}
#prefix declaration
prefix dbo:
Prefix rdf:
SELECT COUNT(?Architect) as ?NumberOfArchitects
FROM
WHERE { ?Architect rdf:type dbo:Person . ?Architect rdf:type dbo:Architect .}
9
…but results are themself RDF:
_:_ a res:ResultSet .
_:_ res:resultVariable “NumberOfArchitects” . _:_ res:solution [ res:binding [ res:variable
“NumberOfArchitects” ; res:value 2885 ] ] .
10
SELECT query: Find 10 of this, LIMIT
SELECT ?Astronaut
FROM
?Astronaut rdf:type dbo:Astronaut.
?Astronaut dbo:nationality dbr:United_States. }
LIMIT 10
Find 10 American astronauts 11
Order the results by date of birth ORDER BY
SELECT ?Astronaut
FROM
WHERE {
}
ORDER BY ?date
?Astronaut rdf:type dbo:Person.
?Astronaut rdf:type dbo:Astronaut.
?Astronaut dbo:nationality dbr:United_States. ?Astronaut dbp:birthDate ?date.
Exercise: Modify to
print the birthdate
12
PREFIX rdfs:
PREFIX :
SELECT ?name ?birth ?description ?person WHERE {
?person a dbo:MusicalArtist . ?person dbo:birthPlace :Berlin . ?person dbo:birthDate ?birth . ?person foaf:name ?name .
?person rdfs:comment ?description . FILTER (LANG(?description) = ‘en’) .
} ORDER BY ?name
Here FROM is missing, but it works for dbpedia
13
SELECT query: optional
prefix dbo:
prefix dbp:
prefix dbr:
prefix rdf:
SELECT ?Astronaut ?date FROM
WHERE { ?Astronaut rdf:type dbo:Person. ?Astronaut rdf:type dbo:Astronaut.
?Astronaut dbo:nationality dbr:United_States.
Optional {?Astronaut dbp:birthDate ?date.}
}
ORDER BY ?date
14
prefix dbo:
prefix rdfs:
SELECT ?s
FROM
WHERE { }
?s foaf:name “Ivor Cutler”@en.
and another:
SELECT * WHERE {
?sub ?pred ?obj .
}
LIMIT 10
another example:
SELECT DISTINCT ?type
WHERE {
?s a ?type. }
Note: The SPARQL keyword a is a shortcut for the common predicate rdf:type, giving the class of a resource.
15
Filter Astronauts From Countries < 200,000,000 population
SELECT ?Astronaunt
FROM
WHERE {
?Astronaunt rdf:type dbo:Person.
?Astronaunt rdf:type dbo:Astronaut. ?Astronaunt dbo:nationality ?nationality. ?nationality dbo:populationTotal ?population Optional {?Astronaunt dbp:birthDate ?date.} Filter (?population < 200000000)
}
ORDER BY ?date
Exercise: Modify to print the nationality and population
16
Wikidata: Query Service Airports in Belgium
https://query.wikidata.org/
#defaultView:Map
SELECT DISTINCT ?airport ?coor ?range WHERE
{
?airport wdt:P31 wd:Q1248784 ; ?range wd:Q31;
wdt:P625 ?coor.
}
prefix dbo:
SELECT ?city (COUNT (?y) as ?Structures)
WHERE {
?y a dbo:ArchitecturalStructure.
?y dbo:location ?city.
?city dbo:country dbr:United_States.
?city a dbo:City.
}
GROUP BY ?city
HAVING (COUNT (?y) > 100)
18
SELECT ?city ?Structures (MIN(?example) as ?OneExample)
WHERE {?example a dbo:ArchitecturalStructure. ?example dbo:location ?city.
?city dbo:populationTotal ?pop. FILTER (?pop > 500000)
{ SELECT ?city (COUNT (?y) as ?Structures)
WHERE {?y a dbo:ArchitecturalStructure.
?y dbo:location ?city.
?city dbo:country dbr:United_States.
?city a dbo:City.
}
GROUP BY ?city
HAVING (COUNT (?y) > 100)
} }
GROUP BY ?city ?Structures
ORDER BY ?Structures
19