我已经将RDF数据存储加载到OpenRDF存储库中,然后我尝试查询存储库并根据查询检索某些数据。我正在运行一个非常简单的查询来获取所有主题谓词和对象。但是,当我尝试打印查询结果时,它只显示第一个三元组。我提供的方法代码只进行查询工作,请检查并告诉我有什么问题.-
private static void queryingRDF(Repository repo) {
try{
RepositoryConnection con = repo.getConnection();
try{
String queryString = "SELECT ?s ?p ?o WHERE { ?s ?p ?o } ";
TupleQuery tupleQuery = con.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
TupleQueryResult result = tupleQuery.evaluate();
try {
BindingSet bindingSet = result.next();
Value valueOfX = bindingSet.getValue("s");
Value valueOfY = bindingSet.getValue("p");
Value valueOfZ = bindingSet.getValue("o");
System.out.println(valueOfX);
System.out.println(valueOfY);
System.out.println(valueOfZ);
}
finally {
result.close();
}
}
finally{
con.close();
}
}
catch(OpenRDFException e){
System.out.println("Query error");
}
}
答案 0 :(得分:3)
TupleQueryResult
是一个迭代器,所以如果你想查看所有结果,那么你需要迭代它:
while (result.hasNext())
{
BindingSet bindingSet = result.next();
// Do what you want with the result
}
result.close();
如果您不知道如何使用迭代器,那么请查看Oracle Collections教程以获得简要介绍