SPARQL/Federated query
Appearance
< SPARQL
Federated query
[edit | edit source]Federated query is the ability to take a query and provide solutions based on information from many different sources.
A building block is the ability to have one query be able to issue a query on another SPARQL endpoint during query execution.
Example
[edit | edit source]Federated query comparing Wikidata and UK Parliament database, to show differences of more than 10km in locations of UK parliamentary constituencies (source Wikidata:Wikidata:Status updates/2019 06 10)
# compare lat/long of Parliament and Wikidata constituency records
#defaultView:Map{"hide":["?line"]}
PREFIX parliament:<https://id.parliament.uk/schema/>
SELECT DISTINCT ?constituency ?parlcoord ?item ?itemLabel ?wdcoord ?dist ?line WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
SERVICE <https://api.parliament.uk/sparql>
{ ?constituency parliament:constituencyGroupHasConstituencyArea ?area .
?area parliament:latitude ?lat . ?area parliament:longitude ?long .
bind(SUBSTR(str(?constituency),26) as ?parlid) . }
BIND(concat("Point(",str(?long)," ",str(?lat),")") as ?parlcoord)
# get constituencies from Parliament with coordinates
?item wdt:P6213 ?parlid . ?item wdt:P31 wd:Q27971968 . ?item wdt:P625 ?wdcoord .
# now get them from Wikidata with coordinates
BIND(geof:distance(?parlcoord, ?wdcoord) as ?dist) . filter (?dist >= 10)
# now find out the distance (in kms)
?item p:P625 ?statementnode. ?statementnode psv:P625 ?valuenode.
?valuenode wikibase:geoLatitude ?wikilat . ?valuenode wikibase:geoLongitude ?wikilon.
BIND(CONCAT('LINESTRING (', STR(?wikilon), ' ', STR(?wikilat), ',', STR(?long), ' ', STR(?lat), ')') AS ?str) .
BIND(STRDT(?str, geo:wktLiteral) AS ?line)
}
SPARQL Federation endpoints
[edit | edit source]Not all endpoints are allowed by Wikidata Query Service. See the list of allowed endpoints on SPARQL Federation endpoints.
You can nominate more endpoints at the Wikidata:SPARQL federation input.
References
[edit | edit source]- www.w3.org/2009/sparql/wiki/Feature:BasicFederatedQuery
- www.oclc.org/developer/news/2016/federated-queries-with-sparql.en.html
- Wikidata Query Service/User Manual#Federation