gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 19:34:33 25 4
gpt4 key购买 nike

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

关于java - 使用 Jena 的 SPARQL 查询没有产生任何结果——但在线工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10626842/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com