01
📖 Qu'est-ce que l'API Graph Database ?
L'API Graph Database permet d'interroger directement votre graphe de connaissances via deux langages puissants : Cypher (pour Neo4j) et SPARQL (pour RDF). Que vous soyez développeur Neo4j ou spécialiste du web sémantique, vous trouverez l'interface adaptée à vos besoins.
💡 À retenir : Cette API est une passerelle universelle vers votre graphe de connaissances, supportant à la fois les graphes de propriétés (Neo4j) et les triple stores RDF.
🌐 Endpoints publics :
https://lemondesemantique.fr/api/graph/cypher (requêtes Cypher)
https://lemondesemantique.fr/api/graph/sparql (requêtes SPARQL)
Caractéristiques :
- ✅ Support de Cypher (langage Neo4j)
- ✅ Support de SPARQL (standard W3C)
- ✅ Résultats en JSON, CSV, XML
- ✅ Pagination et limitation des résultats
- ✅ Authentification par clé API
02
🔌 Endpoints disponibles
POST /api/graph/cypher - Requête Cypher
curl -X POST https://lemondesemantique.fr/api/graph/cypher \
-H "Authorization: Bearer votre_clé_api" \
-H "Content-Type: application/json" \
-d '{
"query": "MATCH (o:Orateur) RETURN o.nom LIMIT 10",
"format": "json"
}'
POST /api/graph/sparql - Requête SPARQL
curl -X POST https://lemondesemantique.fr/api/graph/sparql \
-H "Authorization: Bearer votre_clé_api" \
-H "Content-Type: application/sparql-query" \
-d "PREFIX foaf:
SELECT ?nom WHERE { ?orateur foaf:name ?nom } LIMIT 10"
GET /api/graph/schema - Schéma du graphe
curl -X GET https://lemondesemantique.fr/api/graph/schema \
-H "Authorization: Bearer votre_clé_api"
03
🗣️ Requêtes Cypher (Neo4j)
Cypher est le langage de requête de Neo4j, conçu pour être intuitif et visuel avec sa syntaxe ASCII-art.
Syntaxe de base
MATCH (orateur:Orateur)
RETURN orateur.nom, orateur.dateNaissance
ORDER BY orateur.dateNaissance
LIMIT 10
Recherche de relations
MATCH (orateur:Orateur)-[:A_PRONONCE]->(discours:Discours)
WHERE discours.date > '1940-01-01'
RETURN orateur.nom, discours.titre, discours.date
ORDER BY discours.date
Parcours à plusieurs niveaux
# Discours qui citent d'autres discours
MATCH (d1:Discours)-[:CITE*1..3]->(d2:Discours)
RETURN d1.titre, d2.titre
LIMIT 20
📝 Exemple concret : "Trouver tous les orateurs qui ont prononcé un discours sur la résistance"
MATCH (orateur:Orateur)-[:A_PRONONCE]->(discours:Discours)-[:CONCERNE]->(theme:Theme {nom: "Résistance"})
RETURN orateur.nom, discours.titre, discours.date
04
🔍 Requêtes SPARQL (RDF)
SPARQL est le langage standard du W3C pour interroger des données RDF. Idéal pour l'interopérabilité et les inférences complexes.
Syntaxe de base
PREFIX schema:
PREFIX foaf:
SELECT ?nom ?date
WHERE {
?orateur a foaf:Person ;
foaf:name ?nom ;
schema:birthDate ?date .
}
ORDER BY ?date
LIMIT 10
Recherche de relations
PREFIX ex:
SELECT ?orateurNom ?discoursTitre ?date
WHERE {
?orateur ex:aPrononce ?discours ;
foaf:name ?orateurNom .
?discours schema:title ?discoursTitre ;
schema:date ?date .
FILTER(?date > "1940-01-01"^^xsd:date)
}
ORDER BY ?date
Requête avec inférence
PREFIX ex:
SELECT ?discours ?theme
WHERE {
?discours ex:concerne+ ?theme .
}
# L'opérateur + permet de suivre les relations transitives
05
🎯 Exemples de requêtes
Cypher : Tous les discours d'un orateur
curl -X POST https://lemondesemantique.fr/api/graph/cypher \
-H "Authorization: Bearer kg_live_xxxxx" \
-H "Content-Type: application/json" \
-d '{
"query": "MATCH (o:Orateur {nom: \"Charles de Gaulle\"})-[:A_PRONONCE]->(d:Discours) RETURN d.titre, d.date",
"format": "json"
}'
SPARQL : Même requête
curl -X POST https://lemondesemantique.fr/api/graph/sparql \
-H "Authorization: Bearer kg_live_xxxxx" \
-H "Content-Type: application/sparql-query" \
-d "PREFIX ex:
SELECT ?titre ?date WHERE {
ex:aPrononce ?discours .
?discours schema:title ?titre ; schema:date ?date .
}"
Cypher : Chemin entre deux orateurs
curl -X POST https://lemondesemantique.fr/api/graph/cypher \
-H "Authorization: Bearer kg_live_xxxxx" \
-H "Content-Type: application/json" \
-d '{
"query": "MATCH path = (:Orateur {nom: \"Victor Hugo\"})-[:*]-(:Orateur {nom: \"Charles de Gaulle\"}) RETURN path",
"format": "json"
}'
SPARQL : Comptage des discours par orateur
curl -X POST https://lemondesemantique.fr/api/graph/sparql \
-H "Authorization: Bearer kg_live_xxxxx" \
-H "Content-Type: application/sparql-query" \
-d "PREFIX ex:
SELECT ?nom (COUNT(?discours) AS ?nbDiscours) WHERE {
?orateur ex:aPrononce ?discours ; foaf:name ?nom .
} GROUP BY ?nom ORDER BY DESC(?nbDiscours)"
06
🔗 Intégration dans vos applications
Python avec Neo4j (Cypher)
import requests
API_KEY = "kg_live_xxxxx"
url = "https://lemondesemantique.fr/api/graph/cypher"
response = requests.post(
url,
headers={
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
},
json={
"query": "MATCH (o:Orateur) RETURN o.nom, o.dateNaissance LIMIT 10"
}
)
data = response.json()
for row in data["results"]:
print(f"{row['o.nom']} - {row['o.dateNaissance']}")
Python avec SPARQL
from SPARQLWrapper import SPARQLWrapper, JSON
sparql = SPARQLWrapper("https://lemondesemantique.fr/api/graph/sparql")
sparql.setCredentials("", API_KEY, "X-API-Key")
sparql.setQuery("""
PREFIX foaf:
SELECT ?nom WHERE { ?orateur foaf:name ?nom } LIMIT 10
""")
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
💡 Astuce : Choisissez le langage selon votre expertise. Cypher est plus simple pour les débutants, SPARQL plus puissant pour l'interopérabilité.