5

基本上我有以下查询,它可以在所有在线 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);
}
4

1 回答 1

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> |
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
于 2012-05-16T22:58:17.363 回答