Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
690 views
in Technique[技术] by (71.8m points)

java - Using Jena to create a SPARQL query on DBpedia

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 . }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...