DuCharme: Chapter 01: Intro
Metadata
Title: Jumping Right In
Number: 1
Book: DuCharme Learning SPARQL
Core Ideas
SPARQL stands for ‘SPARQL Protocol and RDF Query Language’.
It is specified as a query language for querying data using the RDF data model.
It is also specified as a protocol for client-processor communication.
Toy examples
Here is some toy rdf in Turtle syntax:
@prefix ab: <http://learningsparql.com/ns/toyexample#>
ab:id192881 ab:firstName "Alex" .
ab:id192881 ab:lastName "Dummy" .
ab:id192881 ab:homeTel "999 5209" .
ab:id192881 ab:email "alex@example.org" .
ab:id23442 ab:firstName "Brian" .
ab:id23442 ab:lastName "Bogus" .
ab:id23442 ab:homeTel "1123221" .
ab:id23442 ab:email "brian@example.org" .
Each triple is terminated by a full stop, conventionally there is a space before the full stop for readability.
Here is a toy SPARQL query we might run to find Brian’s email:
PREFIX ab: <http://learningsparql.com/ns/toyexample#>
SELECT ?brianEmail
WHERE
{
?person ab:firstName "Brian" .
?person ab:lastName "Bogus" .
?person ab:email ?brianEmail .
}
capitalization of keywords is by convention only, not required by the syntax. Note the full stops to terminate the query triples.
The query processor will look for a match on the first pattern. If it finds one it will check the second and so on.
The ?
terms are variables. Once a match has been found the variables become bound to the respective value in subsequent query patterns.
Note that all the patterns have to be matched for data to be returned by default. Though there is an OPTIONAL
keyword we’ll learn later to return partial matches.
Here is a more generic search:
PREFIX ab: <http://learningsparql.com/ns/toyexample#>
SELECT *
WHERE
{
?s ?p ?o .
FILTER (regex(?o, "example", "i"))
}
We conventionally use ?s
, ?p
, and ?o
for placeholder variable names for subject, predicate, and object.
This query will match every triple and then only return those that pass meet the filter condition, in this case a regex filter on the triple’s object.