sparqlrdflinked-datatriplestorefederated-queries

How to make, in one SPARQL query, three independent queries on three different SPARQL endpoints?


is there a way that I can do three independent queries on on three different SPARQL endpoints, in one SPARQL query? For example, I have three SPARQL endpoints (i.e., url1, url2 and url3) and three independent queries I want to make, i.e., query1 on url1, query2 on url2 and query3 on url3. Is it possible that I can conduct the three queries once in one SPARQL query?

I tried the pattern like

SELECT * WHERE {

 SERVICE <url1> {query1} 

 SERVICE <url2> {query2} 

 SERVICE <url3> {query3} 
}

It seems that the query tries to connect results from three queries, because the three queries are independent, the final result is empty. If I do three times query one endpoint by one endpoint, each query has returned results.

Thank you very much for your help!


Solution

  • You can use UNION to make 3 separate calls: it is roughly the same as making 3 queries.

    SELECT * WHERE {
     { SERVICE <url1> {query1} }
     UNION
     { SERVICE <url2> {query2} }
      UNION
     { SERVICE <url3> {query3} }
    }
    

    The app may need to know which rows come from which call, so labelling the results can be useful:

    SELECT * WHERE {
     { SERVICE <url1> { query1 } BIND (<url1> AS ?serviceLabel) }
     UNION
     { SERVICE <url2> { query2 } BIND (<url2> AS ?serviceLabel) }
      UNION
     { SERVICE <url3> { query3 } BIND (<url2> AS ?serviceLabel) }
    }