0% found this document useful (0 votes)
7 views

Introduction to RDF and SPARQL

The document provides an introduction to the Resource Description Framework (RDF), detailing its purpose, key concepts, and syntax for representing data relationships. It explains RDF triples, properties, and the structure of RDF graphs, as well as how to serialize RDF data using Turtle syntax. Additionally, it covers querying RDF data using SPARQL, including various query types and dataset specifications.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Introduction to RDF and SPARQL

The document provides an introduction to the Resource Description Framework (RDF), detailing its purpose, key concepts, and syntax for representing data relationships. It explains RDF triples, properties, and the structure of RDF graphs, as well as how to serialize RDF data using Turtle syntax. Additionally, it covers querying RDF data using SPARQL, including various query types and dataset specifications.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

Introduction to RDF

Why RDF?

• Resource Description Framework (RDF) provides a standardized universal model for


representing data and its meaning
• Support hybrid, varied, and changing data models with ease
• Easy to represent any change in data or schema
• Interoperable and composable
• Eg. The song with the name “Love Me Do” has two
:Paul_McCartney
:Paul_McCartney
writers, Paul McCartney and John Lennon
“Love Me Do”

: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

Eg. The Beatles has as a member Paul McCartney

:member
:The_Beatles :Paul_McCartney
Key Terms 2: Objects

• By class we mean a type of thing (eg. band or artist)


• A class is made up of a set of individuals (eg. The Beatles or John
Lennon), which can also be called instances or objects
• A class or individual can be the subject or the object in a 3-part RDF
structure called an RDF triple
Key Terms 3: Properties

• 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

Subject Predicate Object

• If the predicate provides data (a number, date, string, etc.) about an


object, it is called a data property describing an attribute

Subject Predicate Data


Key Terms 4: Graphs
• Taken together, these
elements make up a
graph
• In a graph, points
representing objects or
data are called nodes
while the predicates
that connect them
(either object properties
or data properties) are
called edges
Key Terms: Review
• There are two kinds of objects: classes and individuals/instances
• Classes are sets, collections, types of objects, kinds of things
• Individuals (or instances) are what a class groups together
• Properties come in two types. An object property is a relationship between
two things. Datatype properties are attributes of one thing
• In graph representation diagrams, classes & individuals are called nodes while
properties are called edges
• In RDF triples, classes and individuals are the subjects or objects, while
properties correspond to predicates. A set of RDF triples is called an RDF
graph
RDF Concepts

• IRI: Nodes and edges with a unique identifier


• Literal: Nodes representing values like numbers and dates
• Blank node: Nodes without an explicit identifier
IRI
• Internationalized Resource Identifier

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

• An IRI looks like this


http://www.w3.org/1999/02/22-rdf-syntax-ns#type

• Using a prefix declaration for its namespace


PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

• Can be shortened to a 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

• Datatype can be omitted for strings:


“The Beatles” “The Beatles”^^xsd:string

• Datatype and quotes can be omitted for some datatypes


125 3.14 3.2E4 true

integer decimal double boolean


RDF Serialization

• Declare prefixes

PREFIX :<http://stardog.com/tutorial/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

• Write subject, predicate, object followed by a ‘.’

:The_Beatles rdf:type :Band .


:The_Beatles :name “The Beatles” .
Turtle Syntax

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 rdf:type :Song .


:Love_Me_Do :name “Love Me Do" .
:Love_Me_Do :length “125” ^^xsd:integer .
:Love_Me_Do :writer :John_Lennon .
:Love_Me_Do :writer :Paul_McCartney .
Literal Shorthand

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 rdf:type :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 .
Shorthand for rdf:type

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

• A triple pattern is a triple with zero or more variables


?band rdf:type :Band . ?album :artist ?artist .

:Love_Me_Do :writer ?writer . :The_Beatles ?p ?o .

• Triple patterns match the triples in the graph


• Each matching triple produces one result
SELECT Query: The Main Query Form
in SPARQL
• It has two basic components: SELECT ?band
WHERE {
1. A list of selected variables
?band rdf:type :Band .
2. Triple patterns to match }

• Results are returned as a table


SELECT ?album ?artist
where each selected variable is WHERE {
?album rdf:type :Album .
a column and each pattern
?album :artist ?artist .
match is a row ?artist rdf:type :SoloArtist .
}
Single Triple Pattern

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 ?song ?length {


?song a :Song .
OPTIONAL {
?song :length ?length .
}
}
Subqueries

SELECT (avg(?count) AS ?avgCount)


{
SELECT ?year (count(?album) AS ?count)
{
?album a :Album ;
:date ?date .
BIND (year(?date) AS ?year)
}
GROUP BY ?year
}
Alternatives

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

SELECT DISTINCT ?year


{
?album a :Album ;
:artist ?artist ;
:date ?date
BIND (year(?date) AS ?year)
}
ORDER BY ?year
Grouping Results

SELECT ?year (count(?album) AS ?count)


{
?album a :Album ;
:date ?date ;
BIND (year(?date) AS ?year)
}
GROUP BY ?year
ORDER BY desc(?count)
Query Types
ASK Query

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

• An RDF dataset is a collection of RDF graphs: Default Graph (no IRI)

• There is exactly one default graph


Named Graph 1
<...#g1>
• It does not have a name
• May be empty or contain RDF triples Named Graph 2
<...#g2>
• Zero or more named graphs
Named Graph 3
• A named graph is an RDF graph <...#g3>

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 .

named graphs can be ...


}
serialized in a single
GRAPH :Album {
document :Please_Please_Me rdf:type :Album .
• Use GRAPH to specify a ...

named graph followed by its }


}
triples
Specifying SPARQL Dataset
• A query can use FROM to override the default
PREFIX ex: <...>
SELECT * graph and temporarily treat the merge of one
FROM ex:g1
FROM ex:g4 or more graphs as though they are the
FROM NAMED ex:g1 default graph
FROM NAMED ex:g2
FROM NAMED ex:g3 • FROM NAMED determines which graphs can
WHERE {
...Pattern A... be available as named graphs in the query
GRAPH ex:g3 { • GRAPH directs a query either to a particular
...Pattern B...
} named graph, or to any of the available
GRAPH ?graph {
named graphs
...Pattern C...
}
} Based on: http://www.slideshare.net/LeeFeigenbaum/sparql-cheat-sheet
Specifying SPARQL Dataset
In this example…
PREFIX ex: <...>
SELECT * • Pattern A results come from the merge of g1
FROM ex:g1
FROM ex:g4 and g4 which temporarily act as the default
FROM NAMED ex:g1 graph
FROM NAMED ex:g2
FROM NAMED ex:g3 • Pattern B results can only come from the
WHERE {
...Pattern A... named graph g3
GRAPH ex:g3 { • Pattern C results may come from any of the
...Pattern B...
} named graphs available to this query: g1, g2, or
GRAPH ?graph {
g3. The ?graph variable will specify the
...Pattern C...
} source(s) of any results
} Based on: http://www.slideshare.net/LeeFeigenbaum/sparql-cheat-sheet
Querying a Specific Dataset

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 .
}
}

You might also like