Introduction to Wikidata and SPARQL

Introduction to Wikidata and SPARQL
作者: Calpa Liu
字數:2483
出版日期:September 6, 2022
更新日期:September 6, 2022

Wikidata is a free multilingual database that available for everyone to query information from wikipedia. We can use it to get the statistics, dates, locations that stored in the wikipedia.

Wikidata is a free multilingual database that available for everyone to query information from wikipedia. We can use it to get the statistics, dates, locations that stored in the wikipedia.

Let’s take Japan as an example. The record is in page.

Item

The Wikidata repository consists mainly of items, each one having a label, a description and any number of aliases. Items are uniquely identified by a Q followed by a number, such as Douglas Adams (Q42).

The id of item in wikidata is in the format of Q{number}, so for Japan, the id is Q17.

Statement

Statements describe detailed characteristics of an Item and consist of a property and a value.

Property is in the format of Q{number}, such as instance of (P31). So for example, Japan is instance of country (Q6256).

Let’s query the more data using query language SPARQL.

SPARQL

SPARQL language is a special query language that we can use to get all data from the wikidata.

We can use the online Wikidata Query Service to query the wikidata database.

List all countries in the world

This is a simple query for listing all countries, so we can construct the query like this.

SELECT ?item ?itemLabel
WHERE {
  ?item wdt:P31 wd:Q6256;
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}

Select variables item and itemLabel that the record is instance of(P31) country(Q6256).

And we can translate the label in english by this:

SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }

List the countries in the East Asia

Let’s add one more criteria, the country is in the East Asia Geographic Region.

So scroll a little bit in the page, Japan is part of (P361) East Asia(Q27231). (?item wdt:P361 wd:Q27231)

So the SPARQL query will be:

SELECT ?item ?itemLabel
WHERE {
  ?item wdt:P31 wd:Q6256;
        wdt:P361 wd:Q27231.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}

So the difference is ?item wdt:P361 wd:Q27231.

Homework

  1. List the prefectures of Japan
  2. List the regions of Japan
  3. List all Shinto shrine
  4. List all Japan Festival

Thoughts

It’s interesting to explore the world using the power of programming, and I find it’s more important to build curiosity towards the world.

感謝您閱讀我的文章。歡迎隨時分享你的想法。
關於 Calpa

Calpa 擅長使用 TypeScript、React.js 和 Vue.js 開發Responsive Web Design網站。

此外,Calpa 積極參與香港和台灣的開源社區,曾在2019年的香港開源大會上擔任講者,提供工作經驗和見解。此外,他也在 GitHub 上公開分享個人博客程式碼,已獲得超過300顆星星和60個分支的支持。

更多前端開發技術文章:傳送門