by simonw · 5y ago · 0 comments
select * { wd:Q9682 (wdt:P25|wdt:P22)* ?p . ?p wdt:P25|wdt:P22 ?q } -
I am extremely motivated to learn how to use this: I have a deep desire to extract data from Wikipedia, and I'm fascinated by graph databases.And yet, despite trying on several previous occasions, SPARQL has completely failed to stick in my brain.
This is partly my own failing: I'm confident that if I really dedicated myself to it I could get over this hump.
But it's also a sign that the learning curve here really is tremendously steep, which I think indicates a problem with the design of the technology.
> select *
Show all variables starting with ?
> wd:Q9682
Find item Q9682 (Queen Elizabeth 2)
> (wdt:P25|wdt:P22)*
Follow edges that are either P22 (father) or P25 (mother) zero-or more times
Everytime you follow one of those edges, add the new item to ?p. Keep following these edges until you can't anymore.
> ?p wdt:P25|wdt:P22 ?q
For every ?p follow a mother/father edge precisely once, call the item it points to ?q (if there is no such edge we get rid of the p)
The end result, is we have a list of rows containing pairs of (an ancestor of elizabeth, one of that ancestor's direct parents).
----
I feel like one of the reasons that sparql is confusing is because people use their intuitions from SQL which is wrong - since the underlying data model is different but the syntax looks vaugely sql-like which leads to misunderstandings.
But honestly i think people would have a lot easier time if we had less indirection and just wrote "mother" instead of wdt:P25
What i actually do, is take the number, if it starts with a q go to wikidata.org/wiki/Q123 . If it starts with P go to https://wikidata.org/wiki/Property:P25
What it actually means in a technical sense:
Identifiers in sparql are urls (sort of similar to integer id fields in sql). wdt: is short for http://wikidata.org/prop/direct so wdt:P25 is http://wikidata.org/prop/direct/P25 wdt: means basically normalized but there are other prefixes if you need to access deprectated statements or modifiers on properties. Gory details at https://www.mediawiki.org/wiki/Wikibase/Indexing/RDF_Dump_Fo...
You do not need to declare the wd: prefix if you are using the endpoint at https://query.wikidata.org
I think its safe to assume in context that the newbie sparql user is not setting up their own sparql endpoint but using the official wikidata endpoint.
The property path syntax ( https://www.w3.org/TR/sparql11-query/#propertypaths ) does look a lot like regex syntax, and the general triple pattern construct does kind of feel a bit DFA-ish.
It could really benefit from some linked examples on that page though - I stared at the interface for quite a while, unable to figure out how to use it for anything - then I dug around for an example link and it started to make sense to me.
https://query.wikidata.org/querybuilder/?query=%7B%22conditi...