Introduction to RDF and SPARQL
Introduction to RDF and SPARQL
Why RDF?
:wr
ite
e
am
r
:n
:writer
:John_Lennon :Love_Me_Do
Key Terms 1: The Basic Idea
• Let’s say the class (ie. category) “artist” includes both solo artists and
bands, and a member of a band is a solo artist
• The RDF way to describe these relationships is based on how we
would express it in speech:
Predicate
Subject Object
:member
:The_Beatles :Paul_McCartney
Key Terms 2: Objects
• The middle part of an RDF triple is the predicate, which is used in two
ways. When it describes a relationship between two objects (classes
or individuals) in our model, then it is called an object property
http://www.w3.org/1999/02/22-rdf-syntax-ns#type
http://stardog.com/tutorial/The_Beatles
mailto:[email protected]
urn:isbn:9788026874256
tag:stardog.com,2018:product:stardog
Prefixed Name
rdf:type
Literals
• Literals are written in quotes followed by their datatype IRI
“1963-03-22” ^^xsd:date “1963-03-22T21:44:00Z”^^xsd:dateTime
• Declare prefixes
PREFIX :<http://stardog.com/tutorial/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX :<http://stardog.com/tutorial/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX :<http://stardog.com/tutorial/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX :<http://stardog.com/tutorial/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
:Love_Me_Do a :Song .
:Love_Me_Do :name “Love Me Do" .
:Love_Me_Do :length 125 .
:Love_Me_Do :writer :John_Lennon .
:Love_Me_Do :writer :Paul_McCartney .
Same Subject
PREFIX :<http://stardog.com/tutorial/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
:Love_Me_Do a :Song ;
:name “Love Me Do" ;
:length 125 ;
:writer :John_Lennon ;
:writer :Paul_McCartney .
Same Subject and Predicate
PREFIX :<http://stardog.com/tutorial/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
:Love_Me_Do a :Song ;
:name “Love Me Do" ;
:length 125 ;
:writer :John_Lennon ,
:Paul_McCartney .
Ignore Whitespace
PREFIX :<http://stardog.com/tutorial/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
:Love_Me_Do a :Song ;
:name “Love Me Do" ;
:length 125 ;
:writer :John_Lennon , :Paul_McCartney .
Blank Nodes
SPARQL
Triple Patterns
SELECT ?album
WHERE {
?album rdf:type :Album .
}
SELECT *
{
?album a :Album
}
Joins
SELECT * {
?album a :Album .
?album :artist ?artist .
}
SELECT * {
?album a :Album .
?album :artist ?artist .
?artist a :SoloArtist .
}
Optional Join
SELECT ?name
{
{ ?artist a :SoloArtist }
UNION
{ ?artist a :Band }
?artist :name ?name
}
Negation
SELECT ?song {
?song a :Song .
FILTER (
NOT EXISTS {
?song :length ?length .
}
)
}
Sort Results
SELECT *
{
?album a :Album ;
:artist ?artist ;
:date ?date
}
ORDER BY ?date
Limit Results
SELECT *
{
?album a :Album ;
:artist ?artist ;
:date ?date
}
ORDER BY ?date
LIMIT 2
Offset Results
SELECT *
{
?album a :Album ;
:artist ?artist ;
:date ?date
}
ORDER BY ?date
LIMIT 2
OFFSET 2
Filtering Results
SELECT *
{
?album a :Album ;
:artist ?artist ;
:date ?date
FILTER (year(?date) >= 1970)
}
ORDER BY ?date
Binding Variables
SELECT *
{
?album a :Album ;
:artist ?artist ;
:date ?date
BIND (year(?date) AS ?year)
FILTER (?year >= 1970)
}
ORDER BY ?date
Removing Duplicates
ASK {
?band a :Band .
?song :writer ?band .
}
CONSTRUCT Query
CONSTRUCT WHERE {
?band a :Band ;
:member ?member
}
CONSTRUCT {
?member a :BandMember
}
WHERE {
?band a :Band ;
:member ?member
}
Named Graphs
RDF Datasets
identified by an IRI
Named Graph 4
• Graph names are unique within an <...#g4>
RDF dataset
RDF Data in TriG: Turtle with Named Graphs
• RDF data for the default
GRAPH :Artist {
graph and zero or more :The_Beatles a :Band .
SELECT * {
default graph Default Graph (no IRI)
...graph pattern... #
and any named
graphs
GRAPH :g1 {
...graph pattern... # a specific graph Named Graph 1
<...#g1>
}
GRAPH ?graph { Named Graph 2
or <...#g2>
...graph pattern... # any named graph
or
} Named Graph 3
<...#g3>
}
Based on: http://www.slideshare.net/LeeFeigenbaum/sparql-cheat-sheet
Named Graph Query
SELECT * {
GRAPH :Album {
?album a :Album .
?album :artist ?artist .
}
GRAPH :Artist {
?artist a :SoloArtist .
}
}