jena.query.ResultSet和jena.query.QuerySolution:在SPARQL请求之后hasNext()始终返回false [英] jena.query.ResultSet and jena.query.QuerySolution: hasNext() returns alway false after SPARQL request
问题描述
我在接收SPARQL
响应时遇到问题.
问题是....当我使用以下源代码时,即使响应不应该为空,rs.hasNext()
总是返回false.
I have a problem with receiving SPARQL
response.
The problem is .... when I use the following source code, rs.hasNext()
always return false even though the response shouldn't be empty.
SPARQL查询:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?s ?sl <http://dbpedia.org/property/name> ?o ?ol
FROM <http://en.dbpedia.org/20120601/>
WHERE {
?s rdfs:label ?sl .
?s <http://dbpedia.org/property/name> ?o .
?o rdfs:label ?ol .
FILTER ( lang(?sl)= "en" && lang(?ol)= "en" ) }
LIMIT 100
此查询在DBPedia
Endpoint
中正常工作.
This query works correctly in DBPedia
Endpoint
.
此查询是使用Jena
如下执行的.
This query was executed using Jena
as below.
代码:
...
QueryEngineHTTP qexec = new QueryEngineHTTP("http://dbpedia.org/sparql", query));
// "query" is as above
qexec.addDefaultGraph(http://en.dbpedia.org/20120601/);
List<QuerySolution> resultSetList = new ArrayList<QuerySolution>();
ResultSet rs = qexec.execSelect();
while (rs.hasNext())
resultSetList.add(rs.next());
if (!resultSetList.isEmpty()) {
if (query.contains("?o rdfs:label ?ol")) {
func...1
func...2
}
else {
func...3
func...4
}
}
else {
qexec.close();
break;
}
...
在SPARQL
Endpoint
中没有问题,因为我可以从SPARQL
Query
获得结果.
但是,使用Jena
无法获得任何结果.
正如我提到的,即使响应不应该为empty
,rs.hasNext()
始终返回false
.
rs.hasNext()
无法正常工作并且无法将内容添加到resultSetList
的解决方案是什么?
There is no problem in SPARQL
Endpoint
because I could get the result from SPARQL
Query
.
However, I couldn't get any result using Jena
.
As I mentioned, rs.hasNext()
always return false
even though the response shouldn't be empty
.
What would be the resolution for that rs.hasNext()
doesn't work, and things cannot be added to resultSetList
?
我认为使用Jena
的方式很好...有什么问题吗?
I think the way of using Jena
is fine... Is there something wrong?
推荐答案
您没有提供 complete 和最少的示例,因此很难确切了解代码中出了什么问题.您的查询甚至都没有使用Jena进行解析,因此我不确定为什么您不只是得到一个错误,而不是一个空的结果集.这是一个使用您的查询的最小示例:
You didn't provide a complete and minimal example, so it's hard to see exactly what's going wrong in your code. You query doesn't even parse with Jena, so I'm not sure why you're not simply getting an error, as opposed to an empty result set. Here's a minimal example that uses your query:
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.ResultSet;
public class DBpediaExample {
public static void main(String[] args) {
String query = "" +
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" +
"SELECT ?s ?sl <http://dbpedia.org/property/name> ?o ?ol\n" +
"FROM <http://en.dbpedia.org/20120601/>\n" +
"WHERE {\n" +
" ?s rdfs:label ?sl .\n" +
" ?s <http://dbpedia.org/property/name> ?o .\n" +
" ?o rdfs:label ?ol .\n" +
" FILTER ( lang(?sl)= \"en\" && lang(?ol)= \"en\" )\n" +
"}\n" +
"LIMIT 100";
ResultSet rs = QueryExecutionFactory.sparqlService( "http://dbpedia.org/sparql", query ).execSelect();
while ( rs.hasNext() ) {
System.out.println( rs.next() );
}
}
}
Exception in thread "main" com.hp.hpl.jena.query.QueryParseException: Encountered " <IRIref> "<http://dbpedia.org/property/name> "" at line 3, column 15.
Was expecting one of:
<VAR1> ...
<VAR2> ...
"from" ...
"where" ...
"(" ...
"{" ...
at com.hp.hpl.jena.sparql.lang.ParserSPARQL11.perform(ParserSPARQL11.java:102)
at com.hp.hpl.jena.sparql.lang.ParserSPARQL11.parse$(ParserSPARQL11.java:53)
at com.hp.hpl.jena.sparql.lang.SPARQLParser.parse(SPARQLParser.java:37)
at com.hp.hpl.jena.query.QueryFactory.parse(QueryFactory.java:139)
at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:79)
at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:52)
at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:40)
at com.hp.hpl.jena.query.QueryExecutionFactory.sparqlService(QueryExecutionFactory.java:358)
at com.hp.hpl.jena.query.QueryExecutionFactory.sparqlService(QueryExecutionFactory.java:345)
at DBpediaExample.main(DBpediaExample.java:19)
即使Virtuoso接受了投影变量,也无法像使用一样在投影变量中使用URI引用.这不是合法的SPARQL,您可以使用 sparql.org的查询验证器进行检查.
You can't use a URI reference in the projection variables like you have, even if Virtuoso accepts it. It's not legal SPARQL, and you can check using sparql.org's query validator.
我不确定为什么在查询中使用FROM
,但是如果您实际上将IRI放入公共端点的默认数据集名称(图形IRI)"字段,则不会任何结果.也许您想查询标准数据集?
I'm not sure why you're using the FROM
in your query, but if you actually put that IRI into to the public endpoint's "Default Data Set Name (Graph IRI)" field, you don't get any results. Perhaps you want to query the standard data set instead?
还有一个问题,您实际上应该使用langMatches
比较语言标签.
As an additional issue, you should really be comparing language tags using langMatches
.
如果解决了这些问题,您将得到以下查询和代码,这些查询和代码显示了很多结果.
If you fix those problems, you'd end up with the following query and code, which shows plenty of results.
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.ResultSet;
public class DBpediaExample {
public static void main(String[] args) {
String query = "" +
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" +
"SELECT ?s ?sl (<http://dbpedia.org/property/name> as ?p) ?o ?ol\n" +
"WHERE {\n" +
" ?s rdfs:label ?sl .\n" +
" ?s <http://dbpedia.org/property/name> ?o .\n" +
" ?o rdfs:label ?ol .\n" +
" FILTER ( langMatches(lang(?sl),'en') && langMatches(lang(?ol),'en') )\n" +
"}\n" +
"LIMIT 100";
ResultSet rs = QueryExecutionFactory.sparqlService( "http://dbpedia.org/sparql", query ).execSelect();
while ( rs.hasNext() ) {
System.out.println( rs.next() );
}
}
}
( ?p = <http://dbpedia.org/property/name> ) ( ?sl = "1990–91 NOFV-Oberliga"@en ) ( ?s = <http://dbpedia.org/resource/1990%E2%80%9391_NOFV-Oberliga> ) ( ?o = <http://dbpedia.org/resource/Jens_Dowe> ) ( ?ol = "Jens Dowe"@en )
( ?p = <http://dbpedia.org/property/name> ) ( ?sl = "1. FC Heidenheim"@en ) ( ?s = <http://dbpedia.org/resource/1._FC_Heidenheim> ) ( ?o = <http://dbpedia.org/resource/Kevin_Kraus> ) ( ?ol = "Kevin Kraus"@en )
( ?p = <http://dbpedia.org/property/name> ) ( ?sl = "1905–06 FC Barcelona season"@en ) ( ?s = <http://dbpedia.org/resource/1905%E2%80%9306_FC_Barcelona_season> ) ( ?o = <http://dbpedia.org/resource/Carles_Comamala> ) ( ?ol = "Carles Comamala"@en )
( ?p = <http://dbpedia.org/property/name> ) ( ?sl = "1910–11 FC Barcelona season"@en ) ( ?s = <http://dbpedia.org/resource/1910%E2%80%9311_FC_Barcelona_season> ) ( ?o = <http://dbpedia.org/resource/Carles_Comamala> ) ( ?ol = "Carles Comamala"@en )
( ?p = <http://dbpedia.org/property/name> ) ( ?sl = "1910–11 FC Barcelona season"@en ) ( ?s = <http://dbpedia.org/resource/1910%E2%80%9311_FC_Barcelona_season> ) ( ?o = <http://dbpedia.org/resource/Francisco_Bru> ) ( ?ol = "Francisco Bru"@en )
( ?p = <http://dbpedia.org/property/name> ) ( ?sl = "1. FC Heidenheim"@en ) ( ?s = <http://dbpedia.org/resource/1._FC_Heidenheim> ) ( ?o = <http://dbpedia.org/resource/Michael_Thurk> ) ( ?ol = "Michael Thurk"@en )
( ?p = <http://dbpedia.org/property/name> ) ( ?sl = "1990–91 NOFV-Oberliga"@en ) ( ?s = <http://dbpedia.org/resource/1990%E2%80%9391_NOFV-Oberliga> ) ( ?o = <http://dbpedia.org/resource/Hilmar_Weilandt> ) ( ?ol = "Hilmar Weilandt"@en )
…
这篇关于jena.query.ResultSet和jena.query.QuerySolution:在SPARQL请求之后hasNext()始终返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!