2

I can get item and its properties by label:

SELECT distinct ?item ?itemLabel ?itemDescription
  (SAMPLE(?DR) as ?DR) (SAMPLE(?article)as ?article)
WHERE {
  ?item wdt:P31 wd:Q5.
  ?item ?label "Einstein"@en
  OPTIONAL{?item wdt:P569 ?DR .}
  ?article schema:about ?item .
  ?article schema:inLanguage "en" .
  ?article schema:isPartOf <https://en.wikipedia.org/>.
  OPTIONAL{?item wdt:P570 ?RIP .}
  OPTIONAL{?item wdt:P18 ?image .}
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
GROUP BY ?item ?itemLabel ?itemDescription

See on Wikidata Query Services.

How can I do the same using QID instead label?

logi-kal
  • 7,107
  • 6
  • 31
  • 43
Alexan
  • 8,165
  • 14
  • 74
  • 101
  • What is QID? Is it the URI of Albert Einstein? If so, simply replace the variabale `?item` with it. – UninformedUser Jan 08 '17 at 08:19
  • in this case QID = Q937, replacing ?item doesn't work – Alexan Jan 08 '17 at 17:41
  • You have to use full URIs or prefixed ones in SPARQL. Just Q937 is not a URI, this would be http://www.wikidata.org/entity/Q937 . And then you can remove the first two triple patterns. See my answer below. – UninformedUser Jan 08 '17 at 19:24
  • @AKSW, your answer below? – Alexan Jan 08 '17 at 19:31
  • Sorry, I had to format it. Now it's there – UninformedUser Jan 08 '17 at 19:32
  • Note, that you have to add the other variables in the SELECT part as well and according to the official SPARQL standard, it's not allowed to reassign a variable like you did with `(sample(?DR) as ?DR)`. That's why I renamed it to `?DRSample`. Just to avoid confusion when you read the query – UninformedUser Jan 08 '17 at 19:35

3 Answers3

4

You can utilize the already known QID by using BIND:

BIND(wd:Q937 AS ?item).
...
Inkling
  • 3,544
  • 4
  • 30
  • 44
3

Using the URI instead of the variable ?item will get the information based on the entity Albert Einstein:

PREFIX  schema: <http://schema.org/>
PREFIX  bd:   <http://www.bigdata.com/rdf#>
PREFIX  wdt:  <http://www.wikidata.org/prop/direct/>
PREFIX  wikibase: <http://wikiba.se/ontology#>

SELECT DISTINCT  ?item ?itemLabel ?itemDescription (SAMPLE(?DR) AS ?DRSample) (SAMPLE(?article) AS ?articleSample)
WHERE
  { ?article  schema:about       ?item ;
              schema:inLanguage  "en" ;
              schema:isPartOf    <https://en.wikipedia.org/>
    FILTER ( ?item = <http://www.wikidata.org/entity/Q937> )
    OPTIONAL
      { ?item  wdt:P569  ?DR }
    OPTIONAL
      { ?item  wdt:P570  ?RIP }
    OPTIONAL
      { ?item  wdt:P18  ?image }
    SERVICE wikibase:label
      { bd:serviceParam
                  wikibase:language  "en"
      }
  }
GROUP BY ?item ?itemLabel ?itemDescription
UninformedUser
  • 8,397
  • 1
  • 14
  • 23
  • your answer can be applied to my other question: http://opendata.stackexchange.com/questions/10334/get-items-properties-know-qid – Alexan Jan 09 '17 at 17:59
2

If you already have the QID of the entity you are looking for and simply look for its properties and labels, you're better off using the Wikidata API wbgetentities module

In A. Einstein (Q937) case, that would give the following API call: https://www.wikidata.org/w/api.php?action=wbgetentities&ids=Q937&format=json

maxlath
  • 1,804
  • 15
  • 24
  • 1
    but can I use SPARQL? – Alexan Jan 08 '17 at 17:44
  • you could, doing something like `wd:Q937 ?property ?value` ([try it](https://query.wikidata.org/#SELECT%20%3Fproperty%20%3FpropertyLabel%20%3Fvalue%20%3FvalueLabel%20WHERE%20%7B%0A%20%20wd%3AQ937%20%3Fproperty%20%3Fvalue%20.%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%0A%20%20%20%20bd%3AserviceParam%20wikibase%3Alanguage%20%22en%22%20.%0A%20%20%20%7D%0A%7D)) but that would be a mess to parse, with no benefice I'm aware of. – maxlath Jan 08 '17 at 18:28
  • this json is very big. How I can get only required information, for example: label, description, birthday, link to image and wikipedia link? – Alexan Jan 09 '17 at 00:47
  • you can pick which information among `info|sitelinks|aliases|labels|descriptions|claims|datatype` you want, using the `props` parameter. Ex: [with only sitelinks and descriptions](https://www.wikidata.org/w/api.php?action=wbgetentities&ids=Q937&format=json&props=sitelinks|descriptions) – maxlath Jan 09 '17 at 13:19
  • then, to simplify claims, if by any chance you are working in javascript, I can recommend [wikidata-sdk](https://github.com/maxlath/wikidata-sdk)'s [`simplifyClaims`](https://github.com/maxlath/wikidata-sdk#simplify-claims-results) parsing functions (a library I wrote) – maxlath Jan 09 '17 at 13:22
  • looks interesting, I'll look at it – Alexan Jan 09 '17 at 20:31