Home

Awesome

simple-wikidata-db

This library provides a set of scripts to download the Wikidata dump, sort it into staging files, and query the data in these staged files in a distributed manner. The staging is optimized for (1) querying time, and (2) simplicity.

This library is helpful if you'd like to issue queries like:

Downloading the dump

A full list of available dumps is available here. To fetch the most recent dump, run:

wget https://dumps.wikimedia.org/wikidatawiki/entities/latest-all.json.gz

or, if aria2c is installed, run:

aria2c --max-connection-per-server 16 https://dumps.wikimedia.org/wikidatawiki/entities/latest-all.json.gz

Downloading takes about 2-5 hours (depending on bandwidth).

Processing the dump

The original downloaded wikidata dump is a single file and combines different types of information (alias names, properties, relations, etc). We preprocess the dump by iterating over the compressed file, and saving information to different subdirectories. For more information, see the Data Format. To preprocess the dump, run:

python3 preprocess_dump.py \ 
    --input_file $PATH_TO_COMPRESSED_WIKI_JSON \
    --out_dir $DIR_TO_SAVE_DATA_TO \
    --batch_size $BATCH_SIZE \
    --language_id $LANG

These arguments are:

To do an initial verification of the pipeline, specify a small num_lines_read like 100. This should finish in less than a second.

Providing num_lines_in_dump will provide a progress bar.

It takes ~5 hours to process the dump when running with 90 processes on a 1024GB machine with 56 cores. A tqdm progress bar should provide a more accurate estimate while data is being processed.

Data Format

The Wikidata dump is made available as a single, unweildy JSON file. To make querying/filtering easier, we split the information contained in this JSON file into multiple tables, where each table contains a certain type of information. The tables we create are described below:

Table nameTable descriptionTable schema
labelsHolds the labels for different entitiesqid: the QID of the entity <br> label: the entity's label ('name')
descriptionsHolds the descriptions for different entitiesqid: the QID of the entity <br> description: the entity's description (short summary at the top of the page)
aliasesHolds the aliases for different entitiesqid: the QID of the entity <br> alias: an alias for the entity
entity_relsHolds statements where the value of the statement is another wikidata entityclaim_id: the ID for the statement <br> qid: the ID for wikidata entity <br> property_id: the ID for the property <br> value: the qid for the value wikidata entity
external_idsHolds statements where the value of the statement is an identifier to an external database (e.g. Musicbrainz, Freebase, etc)claim_id: the ID for the statement <br> qid: the ID for wikidata entity <br> property_id: the ID for the property <br> value: the identifier for the external ID
entity_valuesHolds statements where the value of the statement is a string/quantityclaim_id: the ID for the statement <br> qid: the ID for wikidata entity <br> property_id: the ID for the property <br> value: the value for this property
qualifiersHolds qualifiers for statementsqualifier_id: the ID for the qualifier <br> claim_id: the ID for the claim being qualified <br> property_id: the ID for the property <br> value: the value of the qualifier
wikipedia_linksHolds links to Wikipedia itemsqid: the QID of the entity <br> wiki_title: link to corresponding wikipedia entity

<br><br> Each table is stored in a directory, where the content of the table is written to multiple jsonl files stored inside the directory (each file contains a subset of the rows in the table). Each line in the file corresponds to a different triple. Partitioning the table's contents into multiple files improves querying speed--we can process each file in parallel.

Querying scripts

Two scripts are provided as examples of how to write parallelized queries over the data once it's been preprocessed:

Other helpful resources:

For any questions or feedback, contact Neel Guha at nguha@cs.stanford.edu