Federation Patterns
Common patterns for effective federated queries: entity linking, data enrichment, cross-validation, and combining data from multiple sources.
Why Patterns Matter
Federated queries can be slow and complex. These patterns help you structure queries efficiently, handle failures gracefully, and get the most out of distributed data.
Pattern 1: Entity Linking
Use owl:sameAs or external IDs to find the same entity across different
datasets. This is the foundation of most federated queries.
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?city ?cityLabel ?population
WHERE {
# Cities in Germany
?city wdt:P31 wd:Q515 ;
wdt:P17 wd:Q183 .
# Link to DBpedia via sameAs
SERVICE <http://dbpedia.org/sparql> {
?dbCity owl:sameAs ?city ;
dbo:populationTotal ?population .
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
ORDER BY DESC(?population)
LIMIT 10
Pattern 2: Data Enrichment
Add information from external sources that Wikidata doesn't have. This is the most common use case for federation.
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?museum ?museumLabel ?abstract
WHERE {
# Art museums
?museum wdt:P31 wd:Q207694 .
# Get rich description from DBpedia
SERVICE SILENT <http://dbpedia.org/sparql> {
?dbMuseum owl:sameAs ?museum ;
dbo:abstract ?abstract .
FILTER(LANG(?abstract) = "en")
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
LIMIT 5
Pattern 3: Cross-Validation
Compare data between sources to find discrepancies or validate information.
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?country ?countryLabel ?wdPop ?dbPop (ABS(?wdPop - ?dbPop) AS ?diff)
WHERE {
# Countries with population in Wikidata
?country wdt:P31 wd:Q6256 ;
wdt:P1082 ?wdPop .
# Get population from DBpedia
SERVICE SILENT <http://dbpedia.org/sparql> {
?dbCountry owl:sameAs ?country ;
dbo:populationTotal ?dbPop .
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
ORDER BY DESC(?diff)
LIMIT 10
Pattern 4: Filter First
Always filter locally before federating. Sending fewer bindings to remote endpoints dramatically improves performance.
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?scientist ?scientistLabel ?abstract
WHERE {
# Filter locally: Nobel Prize winners in Chemistry
?scientist wdt:P31 wd:Q5 ;
wdt:P166 wd:Q44585 .
# Only then federate with small result set
SERVICE SILENT <http://dbpedia.org/sparql> {
?dbScientist owl:sameAs ?scientist ;
dbo:abstract ?abstract .
FILTER(LANG(?abstract) = "en")
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
LIMIT 10
Anti-pattern: Don't query the remote endpoint first and then filter locally. This downloads massive amounts of data before filtering.
Pattern 5: OPTIONAL Federation
Use OPTIONAL with SERVICE SILENT to return results even
when the remote data doesn't exist or the endpoint fails.
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?uni ?uniLabel ?homepage
WHERE {
# Universities in France
?uni wdt:P31 wd:Q3918 ;
wdt:P17 wd:Q142 .
# Optionally get homepage from DBpedia
OPTIONAL {
SERVICE SILENT <http://dbpedia.org/sparql> {
?dbUni owl:sameAs ?uni ;
foaf:homepage ?homepage .
}
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en,fr" . }
}
LIMIT 20
Pattern 6: Multiple Endpoints
Combine data from several external sources in a single query. Use SERVICE SILENT for each to handle failures gracefully.
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?artist ?artistLabel ?abstract ?homepage
WHERE {
# Painters
?artist wdt:P31 wd:Q5 ;
wdt:P106 wd:Q1028181 .
# Source 1: DBpedia abstract
OPTIONAL {
SERVICE SILENT <http://dbpedia.org/sparql> {
?dbArtist owl:sameAs ?artist ;
dbo:abstract ?abstract .
FILTER(LANG(?abstract) = "en")
}
}
# Source 2: DBpedia homepage
OPTIONAL {
SERVICE SILENT <http://dbpedia.org/sparql> {
?dbArtist2 owl:sameAs ?artist ;
foaf:homepage ?homepage .
}
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
LIMIT 5
Best Practices Summary
- Filter first — Minimize data before federating
- Use SERVICE SILENT — Handle endpoint failures gracefully
- Use OPTIONAL — Return results even when remote data is missing
- Test separately — Debug each SERVICE block independently
- Limit results — Start small, scale up carefully
- Use entity links — owl:sameAs and external IDs are essential
- Monitor performance — Federated queries can be slow
Key Entities
| Entity | Description |
|---|---|
owl:sameAs |
Links identical entities across datasets |
skos:exactMatch |
Links equivalent concepts (weaker than sameAs) |
SERVICE SILENT |
Graceful failure handling for federation |
OPTIONAL |
Return results even if pattern doesn't match |