SPARQL 1.1 Federated Query Extension
The SERVICE keyword instructs a federated query processor to invoke a portion of a SPARQL query
against a remote SPARQL endpoint. This section presents examples of how to use the
SERVICE keyword. The following sections define the syntax and semantics of this extension.
Simple query to a remote SPARQL endpoint
This example shows how to query a remote SPARQL endpoint and join the returned data
with the data from the local RDF Dataset. Consider a query to find the names of the
people we know. Data about the names of various people is available at the http://people.example.org/sparql endpoint:
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix : <http://example.org/> .
:people15 foaf:name "Alice" .
:people16 foaf:name "Bob" .
:people17 foaf:name "Charles" .
:people18 foaf:name "Daisy" .
and one wants to combine with a local FOAF file
http://example.org/myfoaf.rdf that contains the single triple:
<http://example.org/myfoaf/I> <http://xmlns.com/foaf/0.1/knows> <http://example.org/people15> .
Query:
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name
FROM <http://example.org/myfoaf.rdf>
WHERE
{
<http://example.org/myfoaf/I> foaf:knows ?person .
SERVICE <http://people.example.org/sparql> {
?person foaf:name ?name . }
}
This query, on the data above, has one solution:
Query Result:
| name |
|---|
| "Alice" |
SPARQL query with OPTIONAL to two remote SPARQL endpoints
Imagine we want to query people and optionally obtain their interests and the names of people they know. Imagine for instance, two endpoints containing data about people:
Data in the default graph at remote SPARQL endpoint: http://people.example.org/sparql
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix : <http://example.org/> .
:people15 foaf:name "Alice" .
:people16 foaf:name "Bob" .
:people17 foaf:name "Charles" .
:people17 foaf:interest <http://www.w3.org/2001/sw/rdb2rdf/> .
and data in the default graph the remote SPARQL endpoint: http://people2.example.org/sparql
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix : <http://example.org/> .
:people15 foaf:knows :people18 .
:people18 foaf:name "Mike" .
:people17 foaf:knows :people19 .
:people19 foaf:name "Daisy" .
Query:
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?person ?interest ?known
WHERE
{
SERVICE <http://people.example.org/sparql> {
?person foaf:name ?name .
OPTIONAL {
?person foaf:interest ?interest .
SERVICE <http://people2.example.org/sparql> {
?person foaf:knows ?known . } }
}
}
This query, on the data above, has three solutions:
Query Result:
| person | interest | known |
|---|---|---|
| "Alice" | ||
| "Bob" | ||
| "Charles" | <http://www.w3.org/2001/sw/rdb2rdf/> | <http://example.org/people19> |
Notice that in the query above there is a nested SERVICE in the OPTIONAL clause. This query requires the SPARQL query service at http://people.example.org/sparql to support basic federated query.
Service Execution Failure
The execution of a SERVICE pattern may fail due to several reasons: the remote service may be down, the service
IRI may not be dereferenceable, or the endpoint may return an error to the query.
Normally, under such circumstances the invoked query containing a SERVICE pattern fails as a whole. Queries may explicitly allow failed SERVICE requests with the use of the SILENT keyword. The SILENT keyword indicates that errors encountered while accessing a remote SPARQL endpoint
should be ignored while processing the query. The failed SERVICE clause is treated as if it had a result of a single solution with no bindings.
In the following query the SILENT keyword is present. If the remote SPARQL endpoint is not available because the SPARQL
endpoint does not exist, it is down or it is not accessible the query will return
a solution sequence of one empty solution mapping. If the SILENT keyword is not present, the query will stop and return the error.
Data in <http://people.example.org/sparql> endpoint:
<http://example.org/people15> <http://xmlns.com/foaf/0.1/name> "Charles" .
Query:
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name
WHERE
{
SERVICE SILENT <http://people.example.org/sparql> {
<http://example.org/people15> foaf:name ?name . }
}
Query result if an error occurs while querying the remote SPARQL endpoint:
| name |
|---|
Interplay of SERVICE and VALUES (Informative)
SPARQL 1.1 Query includes the VALUES clause (VALUES), which can be used to provide an unordered solution sequence that is joined with
the results of the query evaluation. Implementers of SPARQL 1.1 Federated Query may
use the VALUES clause to constrain the results received from a remote endpoint based on solution
bindings from evaluating other parts of the query.
The following example shows how SERVICE and VALUES can work together. Suppose a query that asks for all instances of foaf:Person in
the default graph and also their known people in the remote endpoint http://example.org/sparql:
Data in the default graph:
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix : <http://example.org/> .
:a a foaf:Person ;
foaf:name "Alan" ;
foaf:mbox; "alan@example.org" .
:b a foaf:Person ;
foaf:name "Bob" ;
foaf:mbox "bob@example.org" .
and data in the default graph the remote SPARQL endpoint http://example.org/sparql:
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix : <http://example.org/> .
:a foaf:knows :b .
:b foaf:knows :c .
:c foaf:knows :a .
:a foaf:interest "SPARQL 1.1 Basic Federated Query" .
:b foaf:interest "SPARQL 1.1 Query" .
:c foaf:interest "RDB2RDF Direct mapping" .
Query:
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?s
{
?s a foaf:Person .
SERVICE <http://example.org/sparql> {?s foaf:knows ?o }
}
When the original query is executed naively, with an unconstrained service call the
endpoint may return more results than necessary. It may also happen that the SPARQL
endpoint will not return all of them. Many existing SPARQL endpoints have restrictions
in the number of results they return and may miss the ones matching subjects ?s from the local default graph. Thus, an implementation of a query planner for federated
queries may decide to decompose the query into two queries instead, where first the
bindings from the local default graph are evaluated:
Query:
PREFIX : <http://example.org/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?s
{
?s a foaf:Person
}
This query, on the data above, has two solutions:
Query Result:
| s |
|---|
| <http://example.org/a> |
| <http://example.org/b> |
Next, dispatch to the remote endpoint <http://example.org/sparql> a constrained query
with the solutions for ?s:
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX : <http://example.org/>
SELECT * {?s foaf:knows ?o } VALUES (?s) { (:a) (:b) }
The query process involving SERVICE limits the data returned to the data it needs for the overall query:
Query:
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?s ?o
{
?s a foaf:Person
SERVICE <http://example.org/sparql> {?s foaf:knows ?o }
}
This query, on the data above using VALUES, has the expected two solutions to the overall query:
Query Result:
| s | o |
|---|---|
| <http://example.org/a> | <http://example.org/b> |
| <http://example.org/b> | <http://example.org/c> |
Copyright © 2013 W3C® (MIT, ERCIM, Keio, Beihang). This software or document includes material copied from or derived from SPARQL 1.1 Federated Query.