Graph data formats
SPARQL
This work is licensed under a Creative Commons Attribution 4.0 International License.
SPARQL & SPARQL endpoints
2
Existing SPARQL endpoint examples
3
RDF model: a triple, a statement
<http://example.com/index.html> <http://purl.org/dc/terms/creator> <http://example.com/staff/8574> .
4
http://example.com/index.html
http://example.com/staff/8574
http://purl.org/dc/terms/creator
subject (S)
predicate (P)�(property)
object (O)
Resource / Thing
Resource / Thing
SPARQL querying - variable
?s <http://purl.org/dc/terms/creator> <http://example.com/staff/8574> .
5
?s
http://example.com/staff/8574
http://purl.org/dc/terms/creator
subject (S)
predicate (P)�(property)
object (O)
Variable
Resource / Thing
SPARQL querying - graph pattern matching
?s�dcterms:creator�staff:8574 .
ex:index.html�dcterms:creator�staff:8574 .
6
?s
staff:8574
dcterms:creator
ex:index.html
staff:8574
dcterms:creator
It’s a match!
SPARQL querying - variable binding
?s�dcterms:creator�staff:8574 .
ex:index.html�dcterms:creator�staff:8574 .
7
?s
staff:8574
dcterms:creator
ex:index.html
staff:8574
dcterms:creator
?s <- ex:index.html
SPARQL querying - more triples
sis:stud1 sis:name "John" ;
sis:age 26 ;
rdf:type sis:Person .
sis:stud2 sis:name "Peter" ;
sis:age 30 ;
rdf:type sis:Person .
sis:stud3 sis:name "Martin" ;
sis:age 20 .
8
?stud rdf:type sis:Person ;
sis:name ?name ;
sis:age ?age .
?stud | ?name | ?age |
sis:stud1 | "John" | 26 |
sis:stud2 | "Peter" | 30 |
| | |
Solutions table
SPARQL querying - optional graph pattern matching
sis:stud1 sis:name "John" ;
sis:age 26 ;
rdf:type sis:Person .�
sis:stud2 sis:name "Peter" ;
sis:age 30 ;
rdf:type sis:Person .�
sis:stud3 sis:name "Martin" ;
sis:age 20 .
9
?stud sis:name ?name ;
sis:age ?age .�OPTIONAL { � ?stud rdf:type ?type.
}
Solutions table
?stud | ?name | ?age | ?type |
sis:stud1 | "John" | 26 | sis:Person |
sis:stud2 | "Peter" | 30 | sis:Person |
sis:stud3 | "Martin" | 20 | NOT BOUND |
SPARQL querying - multiple optional graph patterns
?stud rdf:type ?type .
OPTIONAL {
?stud sis:age ?age .
}
OPTIONAL {
?stud sis:name ?name .
}
10
OPTIONAL { ?dataset dcterms:temporal ?temporal .
OPTIONAL { ?temporal dcat:startDate ?startDate . }
OPTIONAL { ?temporal dcat:endDate ?endDate . }
OPTIONAL { ?temporal schema:startDate ?schemaStartDate . }
OPTIONAL { ?temporal schema:endDate ?schemaEndDate . }
BIND(IF(BOUND(?startDate), ?startDate, ?schemaStartDate) AS ?finalStartDate)
BIND(IF(BOUND(?endDate), ?endDate, ?schemaEndDate) AS ?finalEndDate)
}
OPTIONAL { ?dataset dcat:contactPoint ?cp .
?cp a ?cptype.
OPTIONAL { ?cp vcard:fn ?cpfn . }
OPTIONAL { ?cp vcard:hasEmail ?cpemail . }
}
OPTIONAL { ?dataset foaf:page ?page . }
OPTIONAL { ?dataset dcterms:conformsTo ?conformsTo . }
OPTIONAL { ?dataset dcat:spatialResolutionInMeters ?spatialResolution . }
OPTIONAL { ?dataset dcat:temporalResolution ?temporalResolution . }
OPTIONAL { ?dataset dcterms:isPartOf ?topDataset . }
SPARQL querying - union graph pattern matching
sis:stud1 sis:name "John" ;
sis:age 26 ;
rdf:type sis:Person .�
sis:stud2 sis:name "Peter" ;
sis:age 30 ;
rdf:type sis:Person .
sis:stud3 sis:name "Martin" ;
sis:age 20 .
11
{�?stud sis:name ?name ;
sis:age ?age .�}�UNION�{ �?stud sis:name ?name ;
sis:age ?age ;� rdf:type ?type.
}
?stud | ?name | ?age | ?type |
sis:stud1 | "John" | 26 | sis:Person |
sis:stud2 | "Peter" | 30 | sis:Person |
sis:stud3 | "Martin" | 20 | NOT BOUND |
sis:stud1 | "John" | 26 | NOT BOUND |
sis:stud2 | "Peter" | 30 | NOT BOUND |
SPARQL - First complete query
PREFIX sis: <http://is.cuni.cz/studium/sis#>
SELECT ?name ?age
WHERE {
?stud a ?type ;
sis:name ?name ;
sis:age ?age .
}
12
?name | ?age |
"John" | 26 |
"Peter" | 30 |
| |
Query result
SPARQL - Prologue
Prefix
Like in Turtle, but without @ and without "."
PREFIX name: <http://www.my.cz/>
…
WHERE {
name:local ?p ?o.
}
Relative URIs
Like in Turtle, but without @ and without "."
BASE <http://www.my.cz/>
…
WHERE {
<sis> <http://www.my.cz/sis> ?o
}
13
SPARQL - Blank nodes
14
Blank node identifiers in the result may not correspond to original blank node identifiers from the given document
SPARQL - (named) graph patterns
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT DISTINCT ?g
WHERE {
GRAPH ?g {
?s a foaf:Person .
}
}
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT DISTINCT ?s
WHERE {
GRAPH <https://...> {
?s a foaf:Person .
}
}
15
SPARQL - literals
Language tags
Typed literals
16
SPARQL - Simple query with FILTER
PREFIX sis: <http://is.cuni.cz/studium/sis#>
SELECT ?name ?age
WHERE {
?stud a ?type ;
sis:name ?name ;
sis:age ?age .
FILTER (?age > 27)
}
17
?name | ?age |
"John" | 26 |
"Peter" | 30 |
| |
Query result
Example: Simple SPARQL query
Select URI and labels of all skos:ConceptSchemes from a given SPARQL endpoint, which are labeled „Paragraf“.
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT *
WHERE {
?s a skos:ConceptScheme ;
skos:prefLabel ?label .
FILTER (?label = "Paragraf"@cs)
}
18
SPARQL: logical expression operators
The usual operators
RDF specific operators
19
SPARQL - property paths example
Select first 100 inspections of CTIA, which have a fine as a result.�Return its value.
20
PREFIX schema: <http://schema.org/>
PREFIX gr: <http://purl.org/goodrelations/v1#>�
SELECT ?check ?fine
WHERE {
?check a schema:CheckAction ;
schema:result ?sanction .
?sanction schema:result ?price .
?price gr:hasCurrencyValue ?fine .
}
LIMIT 100
ex:Check
ex:Sanction
gr:PriceSpecification
40000
schema:result
schema:result
gr:hasCurrencyValue
SPARQL - property paths example
Select first 100 inspections of CTIA, which have a fine as a result.�Return its value.
21
PREFIX schema: <http://schema.org/>
PREFIX gr: <http://purl.org/goodrelations/v1#>
SELECT ?check ?fine
WHERE {
?check a schema:CheckAction ;
schema:result/schema:result/gr:hasCurrencyValue ?fine .
}
LIMIT 100
ex:Check
ex:Sanction
gr:PriceSpecification
40000
schema:result
schema:result
gr:hasCurrencyValue
SPARQL - property paths
22
SPARQL - aggregation example
What was the highest issued fine?
PREFIX schema: <http://schema.org/>
PREFIX gr: <http://purl.org/goodrelations/v1#>
SELECT (MAX (?fine) AS ?max)
WHERE
{
?check a schema:CheckAction ;
schema:result/schema:result/gr:hasCurrencyValue ?fine .
}
23
SPARQL - aggregation
24
SPARQL - federated query
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?s ?name
WHERE {
?s a foaf:Person .
SERVICE <https://peoplenames.org/sparql> {
?s foaf:name ?name .
}
}
25
SPARQL - binding variables
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX ns: <http://example.org/ns#>
SELECT ?title ?price
WHERE { ?product ns:price ?p ;
ns:discount ?discount ;
dct:title ?title .
BIND (?p * ( 1 - ?discount) AS ?price)
FILTER(?price < 20)
}
26
SPARQL - binding variables example
Select first 100 inspections of CTIA, which have a fine as a result.�Return its value and whether it is higher than 1000.
PREFIX schema: <http://schema.org/>
PREFIX gr: <http://purl.org/goodrelations/v1#>
SELECT ?check ?fine ?higher
WHERE {
?check a schema:CheckAction ;
schema:result/schema:result/gr:hasCurrencyValue ?fine .
BIND(IF(?fine > 1000, true, false) AS ?higher)
}
LIMIT 100
27
SPARQL - functions
28
SPARQL - functions on RDF terms
29
UUID() | <urn:uuid:b9302fb5-642e-4d3b-af19-29a8f6d894c9> |
STRLANG("chat", "en") | "chat"@en |
STRDT("123", xsd:integer) | "123"^^<http://www.w3.org/2001/XMLSchema#integer> |
STRDT("iiii", <http://ex/roman>) | "iiii"^^<http://ex/roman> |
SPARQL - functions on strings
30
substr("foobar"@en, 4, 1) | "b"@en |
strbefore("abc","b") | "a" |
strbefore("abc"@en,"bc") | "a"@en |
strbefore("abc"@en,"b"@cy) | error |
encode_for_uri("Los Angeles") | "Los%20Angeles" |
replace("abcd", "b", "Z") | "aZcd" |
replace("abab", "B", "Z","i") | "aZaZ" |
replace("abab", "B.", "Z","i") | "aZb" |
SPARQL - another binding variables example
Create IRI for entities with IDs�the Frequency EU vocabulary
�PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX nkod: <https://data.gov.cz/slovník/nkod/>
SELECT ?frequency
WHERE
{
?dataset nkod:accrualPeriodicity ?freq .
BIND(IRI(CONCAT("http://publications.europa.eu/resource/authority/frequency/", ?freq)) AS ?frequency)
}
31
?freq |
MONTHLY |
ANNUAL |
BIENNIAL |
… |
<http://publications.europa.eu/resource/authority/frequency/MONTHLY>�<http://publications.europa.eu/resource/authority/frequency/ANNUAL>�…
SPARQL - functions on numerics
32
SPARQL - functions on dates and times
33
timezone("2011-01-10T14:45:13.815-05:00"^^xsd:dateTime) | "-PT5H"^^xsd:dayTimeDuration |
tz("2011-01-10T14:45:13.815-05:00"^^xsd:dateTime) | "-05:00" |
now() | "2011-01-10T14:45:13.815-05:00"^^xsd:dateTime |
day("2011-01-10T14:45:13.815-05:00"^^xsd:dateTime) | 10 |
SPARQL - subqueries
Inner queries evaluated first.
PREFIX : <https://example.org/>
SELECT ?company ?maxFine
WHERE {
?company a schema:Organization .
{
SELECT ?company (MAX(?fine) AS ?maxFine)
WHERE {
?company :fine ?fine .
} GROUP BY ?company
}
}
34
SPARQL - VALUES example
35
@prefix dct: <http://purl.org/dc/terms/> .
@prefix : <http://example.org/book/> .
@prefix ns: <http://example.org/ns#> .
:book1 dct:title "SPARQL Tutorial"@en ;
ns:price 42 .
:book2 dct:title "The Semantic Web"@en ;
ns:price 23 .
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX : <http://example.org/book/>
PREFIX ns: <http://example.org/ns#>
SELECT ?book ?title ?price
WHERE {
VALUES ?book { :book1 :book3 }
?book dct:title ?title ;
ns:price ?price .
}
book | title | price |
<http://example.org/book/book1> | "SPARQL Tutorial" | 42 |
SPARQL - VALUES example - codelist translation
PREFIX dcat: <http://www.w3.org/ns/dcat#>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX f: <http://publications.europa.eu/resource/authority/frequency/>
CONSTRUCT {
?dataset dcterms:accrualPeriodicity ?new.
?new a skos:Concept, dcterms:Frequency .
}
WHERE {
?dataset dcterms:accrualPeriodicity ?f .
VALUES (?f ?new) {
("R/P1M" f:MONTHLY)
("R/P1Y" f:ANNUAL)
("R/P2Y" f:BIENNIAL)
…
("jednorázově" f:NEVER)
("nikdy" f:NEVER)
}
}
36
<#dataset1> dcterms:accrualPeriodicity "R/P1M" .
<#dataset2> dcterms:accrualPeriodicity "R/P2Y" .
<#dataset1> dcterms:accrualPeriodicity f:MONTHLY .
f:MONTHLY a skos:Concept, dcterms:Frequency .
<#dataset2> dcterms:accrualPeriodicity f:BIENNIAL .
f:BIENNIAL a skos:Concept, dcterms:Frequency .
SPARQL - result forms - SELECT and ASK
Is there a CTIA inspection with a resulting fine higher than 5 000 000?��PREFIX schema: <http://schema.org/>�PREFIX gr: <http://purl.org/goodrelations/v1#>�PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>�ASK {�?check a schema:CheckAction ;� schema:result/schema:result/gr:hasCurrencyValue ?fine .�FILTER(?fine > 5000000)�}
37
SPARQL - result forms - CONSTRUCT
Result: RDF graph constructed from a template.
CONSTRUCT
{ ?s sis:name ?fullName }
WHERE {
?s sis:firstName ?n1 ;
sis:lastName ?n2 .
BIND(STRLANG(CONCAT(?n1, " ", ?n2), "cs") AS ?fullName)
}
38
SPARQL - result forms - DESCRIBE
Result: RDF graph about the given resource.
Specification non-normative - various triplestores implement it differently.
Example: DESCRIBE <http://www.my.cz/>
39
SPARQL SELECT - modifiers
ORDER BY
40
SPARQL SELECT - ORDER BY example
PREFIX : <http://example.org/ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name
WHERE { ?x foaf:name ?name ;
:empId ?emp }
ORDER BY ?name DESC(?emp)
41
SPARQL SELECT - modifiers
LIMIT
OFFSET
42
SPARQL SELECT - GROUP BY, HAVING
PREFIX : <http://data.example/>
SELECT (AVG(?size) AS ?asize)
WHERE {
?x :size ?size
}
GROUP BY ?x
HAVING(AVG(?size) > 10)
43
SPARQL 1.1
44