SPARQL - bounswe/bounswe2024group11 GitHub Wiki
What is SPARQL?
SPARQL (SPARQL Protocol and RDF Query Language) is a query language and protocol used for querying data stored in the Resource Description Framework (RDF) format. RDF is a data model for representing information in the form of subject-predicate-object triples, which are often visualized as directed graphs.
Wikidata vs SPARQL
Wikidata is a knowledge database. It contains millions of statements, such as “the capital of Canada is Ottawa”, or “the Mona Lisa is painted in oil paint on poplar wood”, or “gold has a melting point of 1,064.18 degrees Celsius”.
SPARQL is a language to formulate questions (queries) for knowledge databases. With the right database, a SPARQL query could answer questions like “what is the most popular tonality in music?” or “which character was portrayed by the most actors?” or “what’s the distribution of blood types?” or “which authors’ works entered the public domain this year?”.
Use Cases of SPARQL:
-
Semantic Web: SPARQL is fundamental to the Semantic Web, enabling users to query and retrieve data across distributed, heterogeneous datasets on the web.
-
Linked Data: SPARQL facilitates querying data from interconnected datasets adhering to the principles of Linked Data, allowing for seamless integration and interoperability.
-
Data Integration: It's used for integrating data from multiple sources, especially when dealing with diverse schemas and data formats.
-
Knowledge Graphs: SPARQL is essential for querying knowledge graphs, enabling complex queries across large knowledge repositories.
-
Data Analysis: It's used for querying and analyzing RDF data, enabling users to extract meaningful insights from structured data.
Importance of SPARQL:
-
Semantic Interoperability: SPARQL promotes semantic interoperability by enabling queries across disparate datasets using a common query language.
-
Flexibility: It offers powerful capabilities for querying RDF data, including pattern matching, aggregation, filtering, and inference.
-
Standardization: SPARQL is a W3C standard, ensuring its widespread adoption and interoperability across different platforms and systems.
-
Semantic Web Development: SPARQL is crucial for developing applications and services that leverage the Semantic Web paradigm, enabling intelligent data retrieval and processing.
SPARQL Queries:
Basic Query Form
# Retrieve all triples in the RDF dataset
SELECT ?subject ?predicate ?object
WHERE {
?subject ?predicate ?object
}
Getting all fictional characters:
SELECT ?character ?characterLabel ?description ?image
WHERE {
?character wdt:P31 wd:Q95074 ; # instance of fictional character
schema:description ?description .
OPTIONAL { ?character wdt:P18 ?image . }
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Limiting the results
SELECT ?character ?characterLabel ?description ?image
WHERE {
?character wdt:P31 wd:Q95074 ; # instance of fictional character
schema:description ?description .
OPTIONAL { ?character wdt:P18 ?image . }
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
LIMIT 10
Example Response
Query:
SELECT DISTINCT ?horse ?horseLabel ?mother ?motherLabel ?father ?fatherLabel (year(?birthdate) as ?birthyear) (year(?deathdate) as ?deathyear) ?genderLabel
WHERE
{
?horse wdt:P31/wdt:P279* wd:Q726 . # Instance et sous-classes de Q726-Cheval
OPTIONAL{?horse wdt:P25 ?mother .} # P25 : Mère
OPTIONAL{?horse wdt:P22 ?father .} # P22 : Père
OPTIONAL{?horse wdt:P569 ?birthdate .} # P569 : Date de naissance
OPTIONAL{?horse wdt:P570 ?deathdate .} # P570 : Date de décès
OPTIONAL{?horse wdt:P21 ?gender .} # P21 : Sexe
SERVICE wikibase:label { #BabelRainbow
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],fr,ar,be,bg,bn,ca,cs,da,de,el,en,es,et,fa,fi,he,hi,hu,hy,id,it,ja,jv,ko,nb,nl,eo,pa,pl,pt,ro,ru,sh,sk,sr,sv,sw,te,th,tr,uk,yue,vec,vi,zh"
}
}
ORDER BY ?horse
LIMIT 10
Response:
[
{
"horse": "http://www.wikidata.org/entity/Q100144332",
"horseLabel": "Gazal al Shaqab",
"birthyear": "1995",
"genderLabel": "male organism"
},
{
"horse": "http://www.wikidata.org/entity/Q1001792",
"horseLabel": "Makybe Diva",
"mother": "http://www.wikidata.org/entity/Q14949904",
"motherLabel": "Tugela",
"father": "http://www.wikidata.org/entity/Q5263956",
"fatherLabel": "Desert King",
"birthyear": "1999",
"genderLabel": "female organism"
},
{
"horse": "http://www.wikidata.org/entity/Q100267969",
"horseLabel": "Galactica",
"birthyear": "2012",
"genderLabel": "female organism"
}
]
Query Builder
From an Example Response
Fictional Characters Query Template
Authors Query Template
WDQS
WDQS, the Wikidata Query Service, brings WikiData and SPARQL together: You enter a SPARQL query, it runs it against Wikidata’s dataset and shows you the result.