8

I'm new to Wikidata and I'm wanting to query it. The official page list several promising syntax / APIs :

The WDQ syntax seems more concise, ex :

  • WDQ : CLAIM[31:6465] AND NOCLAIM[576] vs.
  • SPARQL (same query) : prefix wdt: <http://www.wikidata.org/prop/direct/> prefix wd: <http://www.wikidata.org/entity/> SELECT ?item WHERE { ?item wdt:P31 wd:Q6465 . OPTIONAL { ?item wdt:P576 ?dummy0 } FILTER(!bound(?dummy0)) }

However, reading https://www.mediawiki.org/wiki/Wikimedia_Discovery#Wikidata_Query_Service_.28WDQS.29 I'm under the impression that WDQ is an unofficial experiment and that SPARQL is the official query syntax.

So is there an official or preferred way of querying wikidata ? Is it safe to invest in WDQ or should I go SPARQL ?

1
  • In SPARQL 1.1, one can write FILTER NOT EXISTS {?item wdt:P576 []} (instead of OPTIONAL { ?item wdt:P576 ?dummy0 } FILTER(!bound(?dummy0))), which is slightly shorter. Commented Nov 8, 2018 at 14:51

3 Answers 3

15

I'm the developer of WDQ. It was developed as a stop-gap measure, until Wikidata would get its own query service. That took some years, but now we have SPARQL and https://query.wikidata.org/ so I plan to retire WDQ in the not-too-distant future. I strongly recommend you go for SPARQL, even if it seems a little verbose at first.

1
5

You can abbreviate SPARQL by expanding a given query fragment with default namespace definitions and other options. For instance the wikidata query command line client expands

wdq -tplanet '?planet wdt:P31 wd:Q44559; wdt:P397 ?sun'

to this SPARQL query

PREFIX bd: <http://www.bigdata.com/rdf#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX schema: <http://schema.org/>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
SELECT * WHERE {
    ?planet wdt:P31 wd:Q44559 .
    ?planet wdt:P397 ?sun .
    SERVICE wikibase:label {
        bd:serviceParam wikibase:language "en" .
        ?planet rdfs:label ?planetLabel .
        ?planet schema:description ?planetDescription .
    }
}
2
  • Interesting. Do you have a reference about this expansion ?
    – Offirmo
    Commented Feb 24, 2016 at 14:46
  • Update 2022: wdq installed from CPAN still works on Ubuntu 20.04 . However it installs quite a few dependencies (Perl Modules as deb packages). For me, n=36, and I had a lot of Perl stuff already installed. -- By the way, the query about planets now returns info about 3890 exoplanets.
    – knb
    Commented May 23, 2022 at 20:19
5

As Magnus Manske points out, SPARQL is the preferred way since, I would add, SPARQL is standardized BUT, here is the downside:

The SPARQL endpoint, at this point in time, is NOT as reliable as WDQ yet, for example a query like this:

https://wdq.wmflabs.org/api?q=tree[729][150][171,273,75,76,77,70,71,74,89]

Which returns all animals in wikidata, takes 15 seconds to complete as WDQ query, and the equivalent (via https:// tools.wmflabs.org/wdq2sparql/w2s.php) in SPARQL:

prefix wdt: <http://www.wikidata.org/prop/direct/>
prefix wd: <http://www.wikidata.org/entity/>
SELECT ?item WHERE {
  ?tree0 (wdt:P150)* ?item .
  ?tree0 (wdt:P171|wdt:P273|wdt:P75|wdt:P76|wdt:P77|wdt:P70|wdt:P71|wdt:P74|wdt:P89)* wd:Q729 .
}

Times out HORRIBLY (test it!).

My advice is to follow Magnus advice to invest in SPARQL, but if you are serious about using this in your project, I'll add a abstraction layer and be prepared to failed queries. (which you should do anyway).

Edit:

Despite of this, WDQ seems more unstable, at least from the availability perspective (maybe it´s just the wmflabs.org domain, or whatever, but here are the unfiltered results) :

Daily performance WDQ vs SPARQL endpoints. Wikidata API as reference

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.