SPARQL§

author:Pierre-Antoine Champin

Contrat Creative Commons

1

Introduction§

2

Objectifs§

3

Requête simple§

PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT ?n1 ?n2
WHERE {
    ?p1 a <http://xmlns.com/foaf/0.1/Person> .
        foaf:name ?n1 ;
        foaf:knows ?p2 .
    ?p2 a foaf:Person ;
        foaf:name ?n2 .
}

digraph simpleSelectQuery {
graph [ rankdir="LR" ]

Person [ label="foaf:Person" ]
p1 [ label="?p1" ]
n1 [ label="?n1" style=filled, fillcolor=gray ]
p2 [ label="?p2" ]
n2 [ label="?n2" style=filled, fillcolor=gray ]

p1 -> Person [ label="rdf:type" ]
p1 -> n1 [ label="foaf:name" ]
p1 -> p2 [ label="foaf:knows" ]
p2 -> Person [ label="rdf:type" ]
p2 -> n2 [ label="foaf:name" ]
}

4

Description du graphe§

5

Préfixes§

Rappel : les préfixes servent à abréger les IRIs.

SPARQL :

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX : <http://example.com/>

Turtle :

@prefix foaf: <http://xmlns.com/foaf/0.1/> . # ⚠ point final
@prefix : <http://example.com> .   # après chaque déclaration
6

Termes§

IRI en extension (relatif ou absolu) :

<http://xmlns.org/foaf/0.1/Person>
<../other-file.rdf>
<#something>
<>

IRI abrégé :

foaf:Person
:something
7

Termes (suite)§

Litéral :

"Bonjour"
"Hello"@en          # avec tag de langue
"123"^^xsd:integer  # typé

42                  # equiv. "42"^^xsd:integer
1.5                 # equiv. "1.5"^^xsd:decimal
314e-2              # equiv. "314e-2"^^xsd:double
true                # equiv. "true"^^xsd:boolean

Nœud muet :

_:toto
[]       # voir ci-après
8

Termes (suite)§

Variable (SPARQL seulement) :

?foo
$foo

NB: pas de distinction entre ? et $.

9

Triplets§

?p1 foaf:name "Pierre-Antoine Champin" .
?p1 a foaf:Person .
10

Factorisation§

11

Nœud muet§

Lorsqu'un nœud muet n'a qu'un seul arc entrant, au lieu de lui inventer un identifiant local :

<#pa> foaf:know _:quelqun .
_:quelqun a foaf:Person ; foaf:name ?n .

on peut utiliser la notation [] :

<#pa> foaf:knows [
    a foaf:Person ;
    foaf:name ?n
] .

digraph bracketBlankNode {
graph [ rankdir=LR ]

pa [ label="<#pa>" ]
bn [ label="" ]
Person [ label="foaf:Person" ]
n [ label="?n" ]
pa -> bn [ label="foaf:knows" ]
bn -> Person [ label="rdf:type" ]
bn -> n [ label="foaf:name"]
}

12

Sous-graphe optionel§

En SPARQL, on peut accepter qu'une partie du graphe ne soit pas satisfaite :

?p1 a foaf:Person ; foaf:name ?n .
OPTIONAL { ?p1 foaf:phone ?tel }
13

Filtres§

En SPARQL, on peut ajouter des contraintes sur les valeurs d'un graphe, avec la clause FILTER.

?p foaf:age ?a .
FILTER (?a >= 18)

On peut combiner des conditions avec les opérateurs logiques « et » (&&), « ou » (||) et « non » (!).

FILTER ( 20 <= ?a  &&  ?a < 30 )
14

Opérations utiles pour les filtres§

15

Requête SELECT§

16

Présentation§

17

DISTINCT§

SELECT DISTINCT ?sn
WHERE { <#pa> foaf:knows ?p. ?p foaf:surname ?sn. }

digraph exempleDistinct {
graph [ rankdir=LR ]

pa [ label="<#pa>" ]
john [ label="" ]
jane [ label="" ]
doe [ shape=rectangle, label="Doe" ]
john_name [ shape=rectangle, label="John" ]
jane_name [ shape=rectangle, label="Jane" ]

pa -> john [ label="foaf:knows" ]
pa -> jane [ label="foaf:knows" ]
john -> john_name [ label="foaf:givenName" ]
john -> doe [ label="foaf:surname" ]
jane -> doe [ label="foaf:surname" ]
jane -> jane_name [ label="foaf:givenName" ]
}

18

LIMIT et OFFSET§

Pour obtenir les 10 premiers résultats :

SELECT ?p
WHERE { <#pa> foaf:knows ?p. }
LIMIT 10

Pour obtenir les 5 résultats suivants :

SELECT ?p
WHERE { <#pa> foaf:knows ?p. }
LIMIT 5 OFFSET 10
19

ORDER BY§

SELECT ?p ?n
WHERE { <#pa> foaf:knows [ foaf:givenName ?p ; foaf:surname ?n ] }
ORDER BY ?n ?p

On peut aussi trier par ordre descendant :

SELECT ?p ?n
WHERE { <#pa> foaf:knows [ foaf:age ?age ] }
ORDER BY DESC(?age)
LIMIT 1
20

GROUP BY§

Sert à aggréger certaines valeurs avec l'une des fonctions d'aggrégations : Count, Sum, Avg, Min, Max, GroupConcat et Sample.

SELECT ?p1 count(?p2)
WHERE { ?p1 foaf:knows ?p2 }
GROUP BY ?p1

On peut combiner GROUP BY avec ORDER BY et LIMIT (attention à l'ordre) :

SELECT ?p1 count(?p2)
WHERE { ?p1 foaf:knows ?p2 }
GROUP BY ?p1
ORDER BY DESC(count(?p2))
LIMIT 3
21

Autres types de requête§

22

ASK§

23

CONSTRUCT§

24

SPARQL Update§

Depuis la version 1.1, possibilité de modifier les données.

25

Quelques requêtes utiles§

26

Exploration des types de ressources§

SELECT ?type count(?o)
WHERE { ?o a ?type }
GROUP BY ?type
ORDER BY DESC(count(?o))
LIMIT 30
27

Exploration des propriétés liées à un type§

SELECT DISTINCT ?prop
WHERE { ?o a <http://example.org/UnType> ; ?prop ?val . }
LIMIT 30
28