使用Jena的SPARQL查询没有产生任何结果 - 但在线工作

时间:2012-05-16 21:16:07

标签: java sparql jena dbpedia

基本上我有以下查询,它可以在所有在线SPARQL测试器中运行而没有问题,但是当使用Java和Jena 2.6.4时,我从来没有得到任何结果。为了演示目的,我已将值写入查询中。

PREFIX  g:    <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX  rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX  onto: <http://dbpedia.org/ontology/>

SELECT  ?subject ?stadium ?lat ?long
WHERE
  { ?subject g:lat ?lat .
    ?subject g:long ?long .
    ?subject <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> onto:Stadium .
    ?subject rdfs:label ?stadium
    FILTER ( ( ( ( ( ?lat >= 52.4814 ) && ( ?lat <= 57.4814 ) ) && ( ?long >= -1.89358 ) ) && ( ?long <= 3.10642 ) ) && ( lang(?stadium) = "en" ) )
  }
LIMIT   5

有些Java,请注意我尝试过几种不同的方法,但是我在整个项目中使用SPARQL并没有问题。

Query query = QueryFactory.create(s2); //s2 = the query above
QueryExecution qExe = QueryExecutionFactory.create(query, model);
ResultSet resultsRes = qExe.execSelect();

try {
  while (resultsRes.hasNext()) {                
    QuerySolution soln = resultsRes.nextSolution();
    //never any results
  }
} catch (Exception ex) {
  System.out.println(ex);
}

1 个答案:

答案 0 :(得分:9)

除非您将整个DBpedia数据集加载到本地模型中,要获得与在DBpedia SPARQL表单中运行查询相同的效果,您必须将查询发送到DBpedia SPARQL端点。我对你的程序进行了以下修改:

package example;

import com.hp.hpl.jena.query.*;

public class AshTest
{
    public static void main( String[] args ) {
        String s2 = "PREFIX  g:    <http://www.w3.org/2003/01/geo/wgs84_pos#>\n" +
                "PREFIX  rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" +
                "PREFIX  onto: <http://dbpedia.org/ontology/>\n" +
                "\n" +
                "SELECT  ?subject ?stadium ?lat ?long\n" +
                "WHERE\n" +
                "  { ?subject g:lat ?lat .\n" +
                "    ?subject g:long ?long .\n" +
                "    ?subject <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> onto:Stadium .\n" +
                "    ?subject rdfs:label ?stadium\n" +
                "    FILTER ( ( ( ( ( ?lat >= 52.4814 ) && ( ?lat <= 57.4814 ) ) && ( ?long >= -1.89358 ) ) && ( ?long <= 3.10642 ) ) && ( lang(?stadium) = \"en\" ) )\n" +
                "  }\n" +
                "LIMIT   5\n" +
                "";

        Query query = QueryFactory.create(s2); //s2 = the query above
        QueryExecution qExe = QueryExecutionFactory.sparqlService( "http://dbpedia.org/sparql", query );
        ResultSet results = qExe.execSelect();
        ResultSetFormatter.out(System.out, results, query) ;
    }
}

并得到以下结果:

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| subject                                                       | stadium                              | lat                                                 | long                                                  |
======================================================================================================================================================================================================================
| <http://dbpedia.org/resource/Welford_Road_Stadium>            | "Welford Road Stadium"@en            | "52.6242"^^<http://www.w3.org/2001/XMLSchema#float> | "-1.13306"^^<http://www.w3.org/2001/XMLSchema#float>  |
| <http://dbpedia.org/resource/Hillsborough_Stadium>            | "Hillsborough Stadium"@en            | "53.4114"^^<http://www.w3.org/2001/XMLSchema#float> | "-1.50056"^^<http://www.w3.org/2001/XMLSchema#float>  |
| <http://dbpedia.org/resource/Gateshead_International_Stadium> | "Gateshead International Stadium"@en | "54.9611"^^<http://www.w3.org/2001/XMLSchema#float> | "-1.57972"^^<http://www.w3.org/2001/XMLSchema#float>  |
| <http://dbpedia.org/resource/Filbert_Street>                  | "Filbert Street"@en                  | "52.6236"^^<http://www.w3.org/2001/XMLSchema#float> | "-1.14056"^^<http://www.w3.org/2001/XMLSchema#float>  |
| <http://dbpedia.org/resource/Craven_Park,_Hull>               | "Craven Park, Hull"@en               | "53.7539"^^<http://www.w3.org/2001/XMLSchema#float> | "-0.264722"^^<http://www.w3.org/2001/XMLSchema#float> |
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------