This very short tutorial will tell you:
- What SPARQL is
- How to build a simple query
- Building a useful query
- Requesting SPARQL from WikiData
OK? Let's go!
1. WTF is SPARQL?
SPARQL (pronounced "Sparkle") is a way of querying semantic database.
Wikipedia - the free online encyclopedia - is built on Wikidata. That's a semantic database which is built on "triples". For example:
- Jill
- was born in
- London
or
- Jill
- is the child of
- Jane
This means we can ask the database "give me all the people born in London". Or, "give me all people who Jill is the child of".
SPARQL lets us create queries based on data relationships.
2. A Simple Query
Let's ask WikiData to find all the people who were born in London.
SELECT ?person WHERE {
?person wdt:P19 wd:Q84
} limit 10
That will select 10 people born in London. Here's what each line does.
SELECT ?person WHERE {
Ask the database to assign results to a variable called ?person
?person wdt:P19 wd:Q84
Assign to the variable, data where the property "born in" is the entity "London".
-
wdt
means property, andP19
means born in. -
wd
means entity, andQ84
is the ID of London.
} limit 10
Just get us 10 results.
You can run this query on WikiData
3. Building a more useful query
If you've run that query, you will see that it isn't very useful! It just gives us back a list of IDs. We want human readable information!
Here's how we do it:
SELECT ?person ?personLabel WHERE {
?person wdt:P19 wd:Q84;
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
} limit 10
I've added two new things here:
?personLabel
This gives us the "Label" - or human readable name - for the result.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
This ensures we are querying the English WikiData.
Run the query to see the results
4. Requesting SPARQL from WikiData
As you've seen from the above links, you can use the WikiData editor to craft requests. You can then see the results visually.
If you want to use the results in a program or website, it's a little more complicated.
The easy way
Once you have written out your query, you can URL encode it and send it directly to a SPARQL endpoint. For example:
You will receive back the results in XML.
The hard - but more useful - way
There are hundreds of different libraries for SPARQL. Whatever your favourite programming language, you will be able to find a way to interact with SPARQL.
Here's a quick snippet of JavaScript which doesn't use a library.
class SPARQLQueryDispatcher {
constructor( endpoint ) {
this.endpoint = endpoint;
}
query( sparqlQuery ) {
const fullUrl = this.endpoint + '?query=' + encodeURIComponent( sparqlQuery );
const headers = { 'Accept': 'application/sparql-results+json' };
return fetch( fullUrl, { headers } ).then( body => body.json() );
}
}
const endpointUrl = 'https://query.wikidata.org/sparql';
const sparqlQuery = `SELECT ?person ?personLabel WHERE {
?person wdt:P19 wd:Q84;
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
} limit 10`;
const queryDispatcher = new SPARQLQueryDispatcher( endpointUrl );
queryDispatcher.query( sparqlQuery ).then( console.log );
Next steps
We've only just scratched the surface of the power of SPARQL. If you're interested in learning more, please leave a comment. Thanks!
Top comments (0)