1 of 45

Graph data formats

SPARQL

2 of 45

SPARQL & SPARQL endpoints

  • SPARQL
    • query language for RDF data
  • SPARQL endpoint
    • HTTP-based web service
    • input: SPARQL query
    • output: data
      • RDF
      • CSV
      • JSON
      • XML
      • ...
    • typically including a user form
      • or use https://yasgui.triply.cc/

2

3 of 45

Existing SPARQL endpoint examples

  • There is a list on GitHub
  • Wikidata Query Service is quite popular
    • Wikidata is a community built database (like Wikipedia is a community built encyclopedia)
  • The official portal for European data
    • datasets from all over Europe
  • In Czechia
  • User friendly SPARQL query editor Yasgui
    • can be used instead the less friendly Virtuoso SPARQL editor

3

4 of 45

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

5 of 45

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

6 of 45

SPARQL querying - graph pattern matching

?s�dcterms:creatorstaff:8574 .

ex:index.htmldcterms:creatorstaff:8574 .

6

?s

staff:8574

dcterms:creator

ex:index.html

staff:8574

dcterms:creator

It’s a match!

7 of 45

SPARQL querying - variable binding

?s�dcterms:creatorstaff:8574 .

ex:index.htmldcterms:creatorstaff:8574 .

7

?s

staff:8574

dcterms:creator

ex:index.html

staff:8574

dcterms:creator

?s <- ex:index.html

8 of 45

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

9 of 45

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

10 of 45

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

11 of 45

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

12 of 45

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

13 of 45

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

14 of 45

SPARQL - Blank nodes

  • Blank nodes in data
    • Distinct nodes within the document scope
  • Blank nodes in query results
    • Distinct nodes within the result scope

14

Blank node identifiers in the result may not correspond to original blank node identifiers from the given document

15 of 45

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

16 of 45

SPARQL - literals

Language tags

  • "Praha"
  • "Praha"@cs
  • "Prague"@en

Typed literals

  • 1 = "1"^^xsd:integer
  • 1.5 = "1.5"^^xsd:decimal
  • 1.0E6 = "1"^^xsd:double
  • true = "true"^^xsd:boolean

16

17 of 45

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

18 of 45

Example: Simple SPARQL query

Select URI and labels of all skos:ConceptSchemes from a given SPARQL endpoint, which are labeled “Continent“.

PREFIX skos: <http://www.w3.org/2004/02/skos/core#>

SELECT *

WHERE {

?s a skos:ConceptScheme ;

skos:prefLabel ?label .

FILTER (?label = "Continent")

}

18

19 of 45

Example: Simple SPARQL query

Select URI and labels of all skos:ConceptSchemes from a given SPARQL endpoint, which are labeled “Continent“.

PREFIX skos: <http://www.w3.org/2004/02/skos/core#>

SELECT *

WHERE {

?s a skos:ConceptScheme ;

skos:prefLabel ?label .

FILTER (?label = "Continent"@en)

}

19

20 of 45

SPARQL: logical expression operators

The usual operators

  • Arithmetic operators
    • Unary + –
    • Binary + – * /
  • Comparison operators
    • < <= >= >
    • = !=
  • Logical connectives
    • ! && ||

RDF specific operators

  • Other operators
    • bound, isIri, isBlank, isLiteral
  • Access functions
    • str, lang, dataType

20

21 of 45

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 ?sanction .

?sanction schema:result ?price .

?price gr:hasCurrencyValue ?fine .

}

LIMIT 100

ex:Check

ex:Sanction

gr:PriceSpecification

40000

schema:result

schema:result

gr:hasCurrencyValue

22 of 45

SPARQL - property paths example

Select first 100 inspections of CTIA, which have a fine as a result.�Return its value.

22

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

23 of 45

SPARQL - property paths

  • Sequences
    • path1/path2
  • Alternatives
    • path1|path2
  • Groups
    • (path)
  • Negation
    • !item
    • !(item1|item2|…)
  • Occurrences
    • path*
    • path+
    • path?
  • Inverse paths (from object to subject)
    • ^path

23

24 of 45

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 .

}

24

25 of 45

SPARQL - aggregation

  • COUNT
  • SUM
  • AVG
  • MIN
  • MAX
  • GROUP_CONCAT(?x ; separator="|")
  • SAMPLE

25

26 of 45

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 .

}

}

26

27 of 45

SPARQL - binding variables

PREFIX dc: <http://purl.org/dc/elements/1.1/>

PREFIX ns: <http://example.org/ns#>

SELECT ?title ?price

WHERE { ?x ns:price ?p .

?x ns:discount ?discount .

BIND (?p*(1-?discount) AS ?price)

FILTER(?price < 20)

?x dc:title ?title .

}

27

28 of 45

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

28

29 of 45

SPARQL - functions

  • if
    • IF(?x = 2, "yes", "no")
      1. evaluates first parameter
      2. If true, returns second parameter
      3. If false, returns third parameter
  • coalesce
    • COALESCE(1/0, ?x, ...)
      • returns first parameter which does not result in error
  • exists { graph pattern }
  • not exists { graph pattern }
  • IN, NOT IN
    • 2 IN (<http://example/iri>, "str", 2.0) = true

29

30 of 45

SPARQL - functions on RDF terms

  • isIRI
  • isBlank
  • isLiteral
  • isNumeric
  • str
  • lang
  • datatype
  • IRI
  • STRDT
  • STRLANG
  • UUID

30

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>

31 of 45

SPARQL - functions on strings

  • STRLEN
  • SUBSTR
  • UCASE, LCASE
  • STRSTARTS, STRENDS
  • CONTAINS
  • STRBEFORE, STRAFTER
  • ENCODE_FOR_URI
  • CONCAT
  • langMatches
  • REGEX
  • REPLACE

31

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"

32 of 45

SPARQL - another binding variables example

Create IRI for entities with IDs

PREFIX dcterms: <http://purl.org/dc/terms/>

PREFIX nkod: <https://data.gov.cz/slovník/nkod/>

CONSTRUCT {

?dataset dcterms:accrualPeriodicity ?frequency .

}

WHERE

{

?dataset nkod:accrualPeriodicity ?freq .

BIND(IRI(CONCAT("http://publications.europa.eu/resource/authority/frequency/", ?freq)) AS ?frequency)

}

32

33 of 45

SPARQL - functions on numerics

  • abs
  • round
  • ceil
  • floor
  • RAND

33

34 of 45

SPARQL - functions on dates and times

  • now
  • year
  • month
  • day
  • hours
  • minutes
  • seconds
  • timezone
  • tz

34

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

35 of 45

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

}

}

35

36 of 45

SPARQL - VALUES example

36

@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

37 of 45

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 {

?distribution dcterms:accrualPeriodicity ?new.

?new a skos:Concept, dcterms:Frequency .

}

WHERE {

?distribution 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)

}

}

37

38 of 45

SPARQL - result forms - SELECT and ASK

  • SELECT
    • Result: Solutions table (e.g. in CSV or JSON)
  • ASK
    • Checks whether at least one result item exists
    • Result: True or false

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)�}

38

39 of 45

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(CONCAT(?n1, " ", ?n2) AS ?fullName)

}

39

40 of 45

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/>

40

41 of 45

SPARQL SELECT - modifiers

ORDER BY

  • Orders items in the solutions table
  • Hierarchical ordering criteria
    • ASC = ascending (default)
    • DESC = descending
  • Unbound variable < blank node < URI < literal
  • Example
    • ORDER BY ASC(?name), DESC(?age)

41

42 of 45

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)

42

43 of 45

SPARQL SELECT - modifiers

LIMIT

  • Limits the number of items in the result sequence
  • Always should be preceded by ORDER BY modifier
    • Because RDF graph is a set with no ordering
  • Used as page size
  • Example: ORDER BY ?name LIMIT 10

OFFSET

  • Index of the first reported item from the sequence
  • Used for paging through results
  • Example: ORDER BY ?name LIMIT 10 OFFSET 20

43

44 of 45

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)

44

45 of 45

SPARQL 1.1

W3C Recommendation

  • 21 March 2013
  • Set of recommendations
    • SPARQL 1.1 Overview
    • SPARQL 1.1 Query Language
    • SPARQL 1.1 Update
    • SPARQL 1.1 Service Description
    • SPARQL 1.1 Federated Query
    • SPARQL 1.1 Query Results JSON Format
    • SPARQL 1.1 Query Results CSV and TSV Formats
    • SPARQL Query Results XML Format (Second Edition)
    • SPARQL 1.1 Entailment Regimes
    • SPARQL 1.1 Protocol
    • SPARQL 1.1 Graph Store HTTP Protocol

45