SPARQL Queries (with cURL command) on Wikidata
This gist resulted to be just the spark for a proper article, and won't be maintained here anymore.
The SPARQL endpoint is http://wdqs-beta.wmflabs.org/bigdata/namespace/wdq/sparql and it has a Web form to fire queries. However http://www.wikidata.org/prop/direct/P31
("instance of") tells you what the entity is.
The repository doesn't have named graphs, or at least the SPARQL endpoint rejects graph queries. The classes of entities (rdf:type
) are not described in the repository.
To find the HTML page of an entity (such as https://www.wikidata.org/entity/Q866405
), simply replace /entity/
with /wiki/
.
By default the SPARQL endpoint returns the results in SPARQL Results XML format but
- adding
-H "Accept: application/json"
to the cURL command gets them in JSON Query Results - adding
-H "Accept: text/csv"
to the cURL command gets them in CSV format (the most readable).
Don't forget to URL encode the query ;-)
#Selects the first 20 types of entities encountered:
select distinct ?type where {
?thing a ?type
}
limit 20
curl http://wdqs-beta.wmflabs.org/bigdata/namespace/wdq/sparql?query=select%20distinct%20%3Ftype%20where%20%7B%0A%3Fthing%20a%20%3Ftype%0A%7D%0Alimit%2020 -H "Accept: text/csv"
describe
queries return all the triples in which the selected entity is subject (position 1/3). Certain SPARQL endpoints (such as this one) also return the triples in which the entity is object (position 3/3). The query result being a graph and not a table like for SPARQL Results, the default format is RDF/XML. JSON and Turtle (text/turtle
) can also be requested.
describe <http://www.wikidata.org/entity/Q866405>
curl http://wdqs-beta.wmflabs.org/bigdata/namespace/wdq/sparql?query=describe%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fentity%2FQ866405%3E -H "Accept: text/turtle"
If you need a specific SPARQL request, you can request (uh uh) it here and I'll see what I can do.