Using Jena to create a SPARQL query on DBpedia

Marcom picture Marcom · Feb 15, 2010 · Viewed 12.1k times · Source

Im trying to create a SPARQL query using Jena to query DBpedia.

The query is working when I use it with standalone tools (Twinkle) but when I plug it in this Java code it returns an empty set.

String sparqlQueryString1 = "PREFIX dbont: <http://dbpedia.org/ontology/> " +
        "PREFIX dbp: <http://dbpedia.org/property/>" +
        "PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>" +
        "   SELECT ?musician  ?place" +
        "   FROM<http://dbpedia.org/resource/Daphne_Oram>" +
        "   WHERE { " +
        "       ?musician dbont:birthPlace ?place ." +
        "   }";

Query query = QueryFactory.create(sparqlQueryString1);
QueryExecution qexec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", query);

ResultSet results = qexec.execSelect();
ResultSetFormatter.out(System.out, results, query);       

qexec.close() ;

Any ideas what I'm doing wrong?

Answer

user205512 picture user205512 · Feb 15, 2010

The problem is your FROM <...> clause. Remove it and all is well. With that clause in place I suspect the endpoint is limiting the query to the graph with that name, but no such graph exists and thus there is no result.

The confusing thing is that it seems like the query works on the form at http://dbpedia.org/sparql. However there a default graph URI is set, so the query goes over that graph too. Clear it and the query doesn't work. As an alternative you can set it to retrieve remote data. That pulls in the named rdf which mentions three birthplaces.

If you're trying to find the birth place of that musician use:

{ <http://dbpedia.org/resource/Daphne_Oram> dbont:birthPlace ?place . }