Post on 03-Jun-2018
8/12/2019 Sparql Tutorial 090313093724 Phpapp01
1/49
Data Extraction & Exploration
with SPARQL & the Talis
Platform
8/12/2019 Sparql Tutorial 090313093724 Phpapp01
2/49
shared innovation
Agenda
Tutorial Schema
Graph Patterns
Simple SELECT queries
OPTIONAL patterns
UNION queries
Sorting & Limiting
Filtering & Restrictions
DISTINCT SPARQL Query Forms
Useful Links
8/12/2019 Sparql Tutorial 090313093724 Phpapp01
3/49
shared innovation
Tutorial Schema
Based on NASA spaceflight data
Available in:
http://api.talis.com/stores/space
8/12/2019 Sparql Tutorial 090313093724 Phpapp01
4/49
8/12/2019 Sparql Tutorial 090313093724 Phpapp01
5/49
shared innovation
Triple and Graph Patterns
How do we describe the structure of the RDFgraph which we're interested in?
8/12/2019 Sparql Tutorial 090313093724 Phpapp01
6/49
shared innovation
#An RDF triple in Turtle syntax
foaf:name Sputnik 1.
8/12/2019 Sparql Tutorial 090313093724 Phpapp01
7/49
shared innovation
#An SPARQL triple pattern, with a single variable
foaf:name ?name.
8/12/2019 Sparql Tutorial 090313093724 Phpapp01
8/49
shared innovation
#All parts of a triple pattern can be variables
?spacecraftfoaf:name ?name.
8/12/2019 Sparql Tutorial 090313093724 Phpapp01
9/49
8/12/2019 Sparql Tutorial 090313093724 Phpapp01
10/49
shared innovation
#Matching labels of resources
?subject rdfs:label ?label.
8/12/2019 Sparql Tutorial 090313093724 Phpapp01
11/49
8/12/2019 Sparql Tutorial 090313093724 Phpapp01
12/49
shared innovation
#Combine triples patterns to create a graph pattern
?subject rdfs:label ?label.?subject rdf:type space:Discipline.
8/12/2019 Sparql Tutorial 090313093724 Phpapp01
13/49
shared innovation
#SPARQL is based on Turtle, which allows abbreviations#e.g. predicate-object lists:
?subject rdfs:label ?label;
rdf:type space:Discipline.
8/12/2019 Sparql Tutorial 090313093724 Phpapp01
14/49
8/12/2019 Sparql Tutorial 090313093724 Phpapp01
15/49
shared innovation
#Graph patterns allow us to traverse a graph
?spacecraft foaf:name Sputnik 1.
?launch space:spacecraft ?launch.
?launch space:launched ?launchdate.
8/12/2019 Sparql Tutorial 090313093724 Phpapp01
16/49
shared innovation
#Graph patterns allow us to traverse a graph
?spacecraft foaf:name Sputnik 1.
?launch space:spacecraft ?launch.
?launch space:launched ?launchdate.
8/12/2019 Sparql Tutorial 090313093724 Phpapp01
17/49
8/12/2019 Sparql Tutorial 090313093724 Phpapp01
18/49
shared innovation
Structure of a Query
What does a basic SPARQL query look like?
8/12/2019 Sparql Tutorial 090313093724 Phpapp01
19/49
shared innovation
#Ex. 1#Associate URIs with prefixes
PREFIX space: PREFIX rdf: PREFIX rdfs:
#Example of a SELECT query, retrieving 2 variables#Variables selected MUST be bound in graph pattern
SELECT ?subject ?labelWHERE {
#This is our graph pattern?subject rdfs:label ?label;
rdf:type space:Discipline.}
8/12/2019 Sparql Tutorial 090313093724 Phpapp01
20/49
shared innovation
#Ex. 2
PREFIX space: PREFIX rdf: PREFIX rdfs:
#Example of a SELECT query, retrieving all variables
SELECT *WHERE {
?subject rdfs:label ?label;rdf:type space:Discipline.
}
8/12/2019 Sparql Tutorial 090313093724 Phpapp01
21/49
shared innovation
OPTIONAL bindings
How do we allow for missing or unknowninformation?
8/12/2019 Sparql Tutorial 090313093724 Phpapp01
22/49
shared innovation
#Ex. 3
PREFIX space: PREFIX foaf:
SELECT ?name ?image
WHERE {
#This pattern must be bound?spacecraft foaf:name ?name.
#Anything in this block doesn't have to be boundOPTIONAL {
?spacecraft foaf:depiction ?image.}
}
8/12/2019 Sparql Tutorial 090313093724 Phpapp01
23/49
shared innovation
UNION queries
How do we allow for alternatives or variations inthe graph?
8/12/2019 Sparql Tutorial 090313093724 Phpapp01
24/49
shared innovation
#Ex. 4
PREFIX space: PREFIX foaf: PREFIX rdfs:
SELECT ?subject ?displayLabelWHERE {
{?subject foaf:name ?displayLabel.}UNION
{?subject rdfs:label ?displayLabel.}
}
http://xmlns.com/foaf/0.1/http://xmlns.com/foaf/0.1/8/12/2019 Sparql Tutorial 090313093724 Phpapp01
25/49
shared innovation
Sorting & Restrictions
How do we apply a sort order to the results?
How can we restrict the number of resultsreturned?
8/12/2019 Sparql Tutorial 090313093724 Phpapp01
26/49
shared innovation
#Ex.5
#Select the uri and the mass of all the spacecraft
PREFIX space: PREFIX foaf:
PREFIX rdfs:
SELECT ?spacecraft ?massWHERE {
?spacecraft space:mass ?mass.
}
http://xmlns.com/foaf/0.1/http://xmlns.com/foaf/0.1/8/12/2019 Sparql Tutorial 090313093724 Phpapp01
27/49
shared innovation
#Ex. 6
#Select the uri and the mass of all the spacecraft#with highest first
PREFIX space:
PREFIX foaf: PREFIX rdfs:
SELECT ?spacecraft ?massWHERE {
?spacecraft space:mass ?mass.
}#Use an ORDER BY clause to apply a sort. Can be ASC or DESCORDER BY DESC(?mass)
http://xmlns.com/foaf/0.1/http://xmlns.com/foaf/0.1/http://xmlns.com/foaf/0.1/http://xmlns.com/foaf/0.1/8/12/2019 Sparql Tutorial 090313093724 Phpapp01
28/49
shared innovation
#Ex. 7
#Select the uri and the mass of the 10 heaviest spacecraft
PREFIX space: PREFIX foaf:
PREFIX rdfs:
SELECT ?spacecraft ?massWHERE {
?spacecraft space:mass ?mass.
}#Order by weight descendingORDER BY DESC(?mass)#Limit to first ten resultsLIMIT 10
http://xmlns.com/foaf/0.1/http://xmlns.com/foaf/0.1/http://xmlns.com/foaf/0.1/http://xmlns.com/foaf/0.1/8/12/2019 Sparql Tutorial 090313093724 Phpapp01
29/49
shared innovation
#Ex. 8
#Select the uri and the mass of the 11-20thmost#heaviest spacecraft
PREFIX space:
PREFIX foaf: PREFIX rdfs:
SELECT ?spacecraft ?massWHERE {
?spacecraft space:mass ?mass.
}ORDER BY DESC(?mass)#Limit to ten resultsLIMIT 10#Apply an offset to get next pageOFFSET 10
http://xmlns.com/foaf/0.1/http://xmlns.com/foaf/0.1/http://xmlns.com/foaf/0.1/http://xmlns.com/foaf/0.1/8/12/2019 Sparql Tutorial 090313093724 Phpapp01
30/49
shared innovation
Filtering
How do we restrict results based on aspects of thedata rather than the graph, e.g. string matching?
8/12/2019 Sparql Tutorial 090313093724 Phpapp01
31/49
shared innovation
#Sample data for Sputnik launch
rdf:type space:Launch;
#Assign a datatype to the literal, to indicate it is#a datespace:launched "1957-10-04"^^xsd:date;
space:spacecraft.
8/12/2019 Sparql Tutorial 090313093724 Phpapp01
32/49
shared innovation
#Ex. 9
#Select name of spacecraft launched between#1stJan 1969 and 1stJan 1970
PREFIX space:
PREFIX foaf: PREFIX xsd:
SELECT ?nameWHERE {
?launch space:launched ?date;space:spacecraft ?spacecraft.
?spacecraft foaf:name ?name.
FILTER (?date > "1969-01-01"^^xsd:date &&
?date < "1970-01-01"^^xsd:date)
}
8/12/2019 Sparql Tutorial 090313093724 Phpapp01
33/49
shared innovation
#Ex. 10
#Select spacecraft with a mass of less than 90kg
PREFIX space: PREFIX foaf:
PREFIX xsd:
SELECT ?spacecraft ?nameWHERE {
?spacecraft foaf:name ?name;space:mass ?mass.
#Note that we have to cast the data to the right type#As it is not declared in the dataFILTER( xsd:double(?mass) < 90.0 )
}
8/12/2019 Sparql Tutorial 090313093724 Phpapp01
34/49
shared innovation
#Ex. 11
#Select spacecraft with a name like ollo
PREFIX space: PREFIX foaf:
PREFIX xsd:
SELECT ?nameWHERE {
?spacecraft foaf:name ?name.
FILTER( regex(?name, ollo, i ) )}
8/12/2019 Sparql Tutorial 090313093724 Phpapp01
35/49
shared innovation
Built-In Filters
Logical: !, &&, ||
Math: +, -, *, /
Comparison: =, !=, >,
8/12/2019 Sparql Tutorial 090313093724 Phpapp01
36/49
shared innovation
DISTINCT
How do we remove duplicate results?
8/12/2019 Sparql Tutorial 090313093724 Phpapp01
37/49
shared innovation
#Ex. 12
#Select list of agencies associated with spacecraft
PREFIX space: PREFIX foaf:
PREFIX xsd:
SELECT DISTINCT ?agencyWHERE {
?spacecraft space:agency ?agency.
}
8/12/2019 Sparql Tutorial 090313093724 Phpapp01
38/49
shared innovation
SPARQL Query Forms
Does SPARQL do more than just SELECT data?
8/12/2019 Sparql Tutorial 090313093724 Phpapp01
39/49
shared innovation
ASK
Test whether the graph contains some data ofinterest
8/12/2019 Sparql Tutorial 090313093724 Phpapp01
40/49
shared innovation
#Ex. 13
#Was there a launch on 16thJuly 1969?
PREFIX space: PREFIX xsd:
ASK WHERE {?launch space:launched "1969-07-16"^^xsd:date.
}
8/12/2019 Sparql Tutorial 090313093724 Phpapp01
41/49
shared innovation
DESCRIBE
Generate an RDF description of a resource(s)
8/12/2019 Sparql Tutorial 090313093724 Phpapp01
42/49
shared innovation
#Ex. 14
#Describe launch(es) that occurred on 16thJuly 1969
PREFIX space: PREFIX xsd:
DESCRIBE ?launch WHERE {?launch space:launched "1969-07-16"^^xsd:date.
}
8/12/2019 Sparql Tutorial 090313093724 Phpapp01
43/49
shared innovation
#Ex. 15
#Describe spacecraft launched on 16thJuly 1969
PREFIX space: PREFIX xsd:
DESCRIBE ?spacecraft WHERE {
?launch space:launched "1969-07-16"^^xsd:date.
?spacecraft space:launch ?launch.
}
8/12/2019 Sparql Tutorial 090313093724 Phpapp01
44/49
shared innovation
CONSTRUCT
Create a custom RDF graph based on query criteria
Can be used to transform RDF data
8/12/2019 Sparql Tutorial 090313093724 Phpapp01
45/49
shared innovation
#Ex. 16
PREFIX space: PREFIX xsd: PREFIX foaf:
CONSTRUCT {?spacecraft foaf:name ?name;
space:agency ?agency;space:mass ?mass.
}WHERE {
?launch space:launched "1969-07-16"^^xsd:date.
?spacecraft space:launch ?launch;foaf:name ?name;space:agency ?agency;space:mass ?mass.
}
8/12/2019 Sparql Tutorial 090313093724 Phpapp01
46/49
shared innovation
SELECT
SQL style result set retrieval
8/12/2019 Sparql Tutorial 090313093724 Phpapp01
47/49
shared innovation
#Ex. 17
PREFIX space: PREFIX xsd: PREFIX foaf:
SELECT ?name ?agency ?massWHERE {
?launch space:launched "1969-07-16"^^xsd:date.
?spacecraft space:launch ?launch;
foaf:name ?name;space:agency ?agency;space:mass ?mass.
}
8/12/2019 Sparql Tutorial 090313093724 Phpapp01
48/49
shared innovation
Useful Links
SPARQL FAQ http://www.thefigtrees.net/lee/sw/sparql-faq
SPARQL Recipes http://n2.talis.com/wiki/SPARQL_Recipes
SPARQL By Example Tutorial http://www.cambridgesemantics.com/2008/09/sparql-by-example
Twinkle, GUI SPARQL editor http://www.ldodds.com/projects/twinkle
http://code.google.com/p/twinkle-sparql-tools
http://www.thefigtrees.net/lee/sw/sparql-faqhttp://n2.talis.com/wiki/SPARQL_Recipeshttp://www.cambridgesemantics.com/2008/09/sparql-by-examplehttp://www.ldodds.com/projects/twinklehttp://code.google.com/p/twinkle-sparql-toolshttp://code.google.com/p/twinkle-sparql-toolshttp://code.google.com/p/twinkle-sparql-toolshttp://code.google.com/p/twinkle-sparql-toolshttp://code.google.com/p/twinkle-sparql-toolshttp://code.google.com/p/twinkle-sparql-toolshttp://code.google.com/p/twinkle-sparql-toolshttp://www.ldodds.com/projects/twinklehttp://www.ldodds.com/projects/twinklehttp://www.cambridgesemantics.com/2008/09/sparql-by-examplehttp://www.cambridgesemantics.com/2008/09/sparql-by-examplehttp://www.cambridgesemantics.com/2008/09/sparql-by-examplehttp://www.cambridgesemantics.com/2008/09/sparql-by-examplehttp://www.cambridgesemantics.com/2008/09/sparql-by-examplehttp://www.cambridgesemantics.com/2008/09/sparql-by-examplehttp://www.cambridgesemantics.com/2008/09/sparql-by-examplehttp://www.cambridgesemantics.com/2008/09/sparql-by-examplehttp://www.cambridgesemantics.com/2008/09/sparql-by-examplehttp://n2.talis.com/wiki/SPARQL_Recipeshttp://n2.talis.com/wiki/SPARQL_Recipeshttp://n2.talis.com/wiki/SPARQL_Recipeshttp://www.thefigtrees.net/lee/sw/sparql-faqhttp://www.thefigtrees.net/lee/sw/sparql-faqhttp://www.thefigtrees.net/lee/sw/sparql-faq8/12/2019 Sparql Tutorial 090313093724 Phpapp01
49/49
shared innovation