3

I am mapping multiple entities (Q entries) in Wikidata for which I need the same record of properties (P entries). For sake of this question, say I have a list of five Wikidata entities (Q1, Q2, Q3, Q4, Q5) for which I want to retrieve the same property records (say, P1, P2, and P3). I know all Q and P in advance.

I understand how to pose a SPARQL query for an individual record (say for Q1), and I also understand how to specify the return of labels using the special SERVICE wikibase:label function for P1, P2, and P3.

First question, is there a way to loop over a list of entities (Q1 to Q5) with a given SPARQL query using only SPARQL?

If the answer is No, does any example Python or pseudocode exist for issuing single SPARQL queries looping over a list of external identifiers (Q1 to Q5), say provided as an external text or CSV file?

If there is a simple way to approach this, I'd love to hear.

1 Answers1

4

I think you’re looking for the VALUES clause:

SELECT ?item ?itemLabel ?class ?classLabel ?projectLabel WHERE {
  VALUES ?item { wd:Q1 wd:Q2 wd:Q3 wd:Q4 wd:Q5 }
  ?item wdt:P31 ?class;
        wdt:P5008 ?project.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

Try it!

Lucas Werkmeister
  • 2,584
  • 1
  • 17
  • 31
  • 1
    Lucas, fantastic! Thank you. This is exactly what I was seeking. Is there a known limit to the VALUES clause (either in SPARQL or the Wikidata endpoint?). – Mike Bergman Aug 19 '18 at 16:26
  • 1
    Eventually the query will become too long for a `GET` request and you’ll have to use `POST` instead, but beyond that I’m not aware of any hard limit. (But, like, you probably can’t use this for *millions* of items ;) ) – Lucas Werkmeister Aug 19 '18 at 22:56
  • Again, Lucas, thanks. I will look into other Wikidata documentation about best practices for how to hammer their site. – Mike Bergman Aug 20 '18 at 02:49