Search code examples
sparqlsemantic-webdbpediasesamelinked-data

How do I query a SPARQL endpoint such as DBPedia with Sesame?


I use Sesame triplestore to store my data. When I try to use the query interface with Sesame with external resources such as dbpedia, I get no results. This query returns results with snorql but not the Sesame after adding all the necessary prefixes:

select ?routes where {
  dbpedia:Polio_vaccine dbpprop:routesOfAdministration ?routes
}

What do I need to change?


Solution

  • You can query any SPARQL endpoint, including DBPedia, in various ways using Sesame, either programmatically or manually via the Sesame Workbench.

    Using the Workbench

    Using the Sesame Workbench tool, you can query DBPedia (or any public SPARQL endpoint) by creating a repository proxy for that endpoint, as follows:

    1. select 'New repository' and in the repository type menu select 'SPARQL endpoint proxy'. Give the proxy an identifier and optionally a title and click 'next'. creating sparql endpoint proxy

    2. fill in the SPARQL endpoint URL for the query endpoint. For the public DBPedia server, this should be http://dbpedia.org/sparql. enter image description here

    3. Finalize by clicking 'create'.

    Once you've set this up you can query it from the 'Query' menu:

    Sesame Workbench SPARQL query editor

    Result:

    enter image description here

    Programmatic access

    You can simply create a SPARQLRepository object that connects to the DBPedia endpoint:

    Repository repo = new SPARQLRepository("http://dbpedia.org/sparql");
    repo.initialize();
    

    Once you have that, you can use it to execute a SPARQL query just like you would on any other Sesame repository:

    RepositoryConnection conn = repo.getConnection();
    try {
        StringBuilder qb = new StringBuilder();
        qb.append("PREFIX dbpedia: <http://dbpedia.org/resource/> \n");    
        qb.append("PREFIX dbpprop: <http://dbpedia.org/property/> \n");
        qb.append("SELECT ?routes \n");
        qb.append("WHERE { dbpedia:Polio_vaccine dbpprop:routesOfAdministration ?routes } \n");
    
        TupleQueryResult result = 
             conn.prepareTupleQuery(QueryLanguage.SPARQL, qb.toString()).evaluate(); 
    
        while(result.hasNext()) {
             BindingSet bs = result.next();
             Value route = bs.getValue("routes");
             System.out.println("route = " + route.stringValue());
        }
     }
     finally {
        conn.close();
     }