Wikidata:SPARQL query service/apache-jena

From Wikidata
Jump to navigation Jump to search

This is an introduction to using the Apache Jena arq command line tool to query WDQS.

Prefixes[edit]

These prefixes are common for queries in WD:

PREFIX schema: <http://schema.org/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX hist: <http://wikiba.se/history/ontology#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX dct: <http://purl.org/dc/terms/>

All of these are implicit in the WDQS UI but have to be stated when using the endpoint with other tools.

Query[edit]

A query using arqs look like this:

PREFIX schema: <http://schema.org/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX hist: <http://wikiba.se/history/ontology#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX dct: <http://purl.org/dc/terms/>

SELECT ?lexemeId ?lemma WHERE {
    ?lexemeId dct:language wd:Q1860;
            wikibase:lemma ?lemma.
  # only those lemmas that begin with "pota", i.e. "potato"
  FILTER (regex(?lemma, '^pota.*'))
}

To run it do something like this:

$ rsparql --service=https://query.wikidata.org/sparql --query query.txt

Which should output:

------------------------------
| lexemeId  | lemma          |
==============================
| wd:L18187 | "potassium"@en |
| wd:L41230 | "potash"@en    |
| wd:L41229 | "potable"@en   |
| wd:L3784  | "potato"@en    |
------------------------------