簡體   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