Skip to main content

Wikidata Introduction

Wikidata is a triple-store dataset used by Wikimedia Projects, such as Wikipedia. You can use SPARQL to query Wikidata. (Note: not all SPARQL options supported by Wikidata are supported by Fluree's SPARQL interface).

All information in Wikidata is based on subject-predicate-object triples, just like Fluree.

All subjects, predicates, and objects can be referred to by a unique id.

Items (subjects and objects) are uniquely identified by Q followed by a number. They should be prefixed with wd:. For example, wd:Q146 is "house cat".

Predicates are referred to by P followed by a number. They should be prefixed with wdt:. For example, wdt:P31 is "instance of".

Objects can also be a literal value (i.e. 335), depending on the query.

The following query selects all house cats in the ledger.

SELECT ?cat
WHERE
{
?cat wdt:P31 wd:Q146.
}

The first four items in the results are wd:Q378619, wd:Q498787, wd:Q677525, and wd:Q851190. By default, we get the Qids for the items in our SELECT clause.

If we want to return a variable's label, simply SELECT from that variable followed by label. For example to select ?cat labels, include ?catLabel in your select clause.

SELECT ?cat ?catLabel
WHERE
{
?cat wdt:P31 wd:Q146.
}

English labels are automatically returned (there is an option to change languages, but that is not covered in this lesson). The above query works in Fluree, because behind-the-scenes, we add the proper clause to include labels. The above query, however would not work in Wikidata without adding a service label for language (see Wikidata documentation on that here).

Wikidata

If you intend to use Wikidata extensively for your project, we recommend seeking out additional resources to learn about Wikidata. Wikidata is a tremendously large project, and for serious use (more than pulling in information for one or two queries), we recommend:

  1. Watching A Gentle Introduction to Wikidata for Absolute Beginners and additional resources on the Wikidata site.

  2. Testing queries with the Wikidata SPARQL interface

You can issue SPARQL queries using the Fluree interface, and you can issue Wikidata queries on their own SPARQL interface. The Wikidata interface is useful, because it has examples, links to resources, and auto-complete features that help you find the exact Q- and P-ids you need.

Challenge

The challenge at the end of this lesson will involve the following query:

SELECT ?name ?artist ?artwork
WHERE {
?person fd:person/handle "jdoe";
fd:person/favArtists ?artist.
?artist fd:artist/name ?name.
?artwork wdt:P170 ?creator.
?creator wd:?label ?name.
}