繁体   English   中英

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

[英]SPARQL query using Jena producing no results — but works online

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

除非您将整个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> |
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM