我在接收SPARQL
回复时遇到问题。
问题是....当我使用以下源代码时,rs.hasNext()
总是返回false,即使响应不应为空。
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
中正常运行。
此查询是使用Jena
执行的,如下所示。
Soucre Code:
...
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
获得任何结果。
正如我所提到的,rs.hasNext()
总是返回false
,即使回复不应该是empty
。
什么是rs.hasNext()
的解决方案不起作用,并且无法将内容添加到resultSetList
?
我认为使用Jena
的方式很好......有什么问题吗?
答案 0 :(得分:2)
您没有提供完整的和最小示例,因此很难确切地看到代码中出现了什么问题。你的查询甚至不用Jena解析,所以我不确定为什么你不是简单地得到一个错误,而不是一个空的结果集。以下是使用查询的最小示例:
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's query validator进行检查。
我不确定您在查询中使用FROM
的原因,但如果您实际将IRI放入公共端点的“默认数据集名称(图形IRI)”字段,则不要得到任何结果。也许您想要查询标准数据集?
作为一个额外的问题,您应该使用langMatches
来比较语言标记。
如果你解决了这些问题,你最终会得到以下查询和代码,这会显示出很多结果。
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 )
…