Search code examples
javasemanticssesame

How to query two sesame repositories at a time?


I have two repositories in sesame, where one have whole data and one have data with few fields which links to primary data.

Example:

Primary Data Fields:

uri, skos:prefLabel , skosAltLabel etc.

Secondary Data Fields:

uri, customField

So basically i want to query secondary data on customField which will return uri which can be mapped to primary data and get other details.

So they are Linked data-sets.

So is it possible to query linked repositories which are on sesame at a time?


Solution

  • Using SPARQL 1.1 SERVICE queries

    SPARQL 1.1 supports the SERVICE clause, which allows you to combine results from multiple SPARQL endpoints in a single query result. Because Sesame Server exposes every repository as a SPARQL endpoint, you can use this to do queries over multiple repositories.

    For example, say you have a Sesame Server running at http://localhost:8080/openrdf-sesame with two repositories, Primary and Secondary. The SPARQL query endpoints for both repositories are http://localhost:8080/openrdf-sesame/repositories/Primary and http://localhost:8080/openrdf-sesame/repositories/Secondary, respectively.

    You can execute a SPARQL on one repository (say, Primary) which then in the query refers to the other one, like this:

     SELECT *
     WHERE { 
             # data from Primary dataset
             ?uri a skos:Concept ;
                  skos:prefLabel ?prefLabel ;
                  skos:altLabel ?altLabel .
    
             # data from Secondary dataset  
             SERVICE <http://localhost:8080/openrdf-sesame/repositories/Secondary>  {
                 ?uri :customField ?customFieldValue .
             }
    }  
    

    Using Sesame's FederationSail

    An alternative is to set up a Federated repository in Sesame, using the FederationSail. This a way to group several Sesame databases together to form a "virtual" repository - a Federation. You can execute queries on the Federation and the result will include data from all member databases of the Federation (without the need to specify which endpoints you want to query, like you need to do when using a SERVICE clause).

    A Federation can be set up programmatically, or (if you're using Sesame Server and Workbench) via the Workbench. Just choose 'New repository', and pick the 'Federation store' option in the store type drop-down. Give it an id and a description, then on the next screen you get to pick which databases should be part of the federation.