notes from the library juice academy courses on “sparql fundamentals”: university of florida...
TRANSCRIPT
Tech TalkBIBFRAME Working Group
15 March 2016
Notes from the Library Juice Academy courses on
“SPARQL Fundamentals” Allison Jai O’Dell | [email protected]
SPARQLThe SPARQL Protocol and RDF Query Language (SPARQL) is “a semantic query
language for databases, able to retrieve and manipulate data stored in Resource
Description Framework (RDF) format” -- Wikipedia
Turtle Syntax
SPARQL uses the triple pattern and Turtle syntax
Turtle: Terse RDF Triple Language
<http://www.example.com/uri> namespace:element “literal” .
“literal”@en Literal with a language tag
“literal”^^xsd:integer Typed literal
Basic SPARQL Query
PREFIX dct: <http://purl.org/dc/terms/>
SELECT ?subject
WHERE
{
<http://www.worldcat.org/oclc/704257552> dct:creator ?subject .
}
PREFIX specifies namespaces
SELECT specifies solutions to return
WHERE specifies triple & graph patterns to query
DISTINCT Modifier
PREFIX dct: <http://purl.org/dc/terms/>
SELECT DISTINCT ?subject
WHERE
{
?title dct:subject ?subject .
}
DISTINCT de-duplicates solutions
More Modifiers
PREFIX dct: <http://purl.org/dc/terms/>
SELECT ?title ?subject
WHERE { ?title dct:subject ?subject . }
ORDER BY ?subject
LIMIT 5
OFFSET 10
ORDER BY specifies how to sort solutions.
OFFSET causes the solutions generated to start after the specified number of solutions.
LIMIT puts an upper bound on the number of solutions returned.
Federated Queries
PREFIX dct: <http://purl.org/dc/terms/>
SELECT DISTINCT ?subject
WHERE
{
SERVICE <http://dbpedia.org/snorql>
{
?title dct:subject ?subject .
}
}
SERVICE lets you run federated queries over multiple endpoints
Helpful Operators
• Conjunction / Boolean “and”
A . B
• Disjunction / Boolean “or”
{A} UNION {B}
• Negation
A MINUS {B}
Property Paths
• path1/path2 Sequence path (pipe)
• ^path1 Inverse path (object to subject)
• path1|path2 Alternative path (either path1 or path2)
• path1* path1 repeated zero or more times
• path1+ path1 repeated one or more times
• path1? Zero or one, e.g., path1 is optional
• !(set) Negation
Slightly complicated SPARQL Query
# British Museum http://collection.britishmuseum.org/sparql
PREFIX crm: <http://erlangen-crm.org/current/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX ecrm: <http://erlangen-crm.org/current/>
PREFIX bmo: <http://collection.britishmuseum.org/id/ontology/>
SELECT ?physdesc
{
?henry skos:prefLabel "Henry Spencer Ashbee" .
?henry ^ecrm:P51_has_former_or_current_owner/
bmo:PX_physical_description ?physdesc .
}
Four Query Forms
• SELECT
• CONSTRUCTConstruct new RDF triples & graphs based on patterns and existing data
• ASKAsk whether or not a pattern matches the data; result is true or false
• DESCRIBEDescribe the resources matched by given variables
CONSTRUCT Query
PREFIX dbr: <http://dbpedia.org/resource/>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
CONSTRUCT
{
dbr:Mindy_Kaling foaf:knows ?who .
?who foaf:knows ?more .
}
WHERE
{
SERVICE <http://dbpedia.org/sparql>
{
dbr:The_Mindy_Project dbo:starring ?who .
?who ^dbo:starring/dbo:starring ?more .
}
}
Let’s have a go.http://dbpedia.org/snorql/
http://collection.britishmuseum.org/sparql