在Open RDF中查询存储库并显示结果 [英] Querying a repository in Open RDF and displaying the results

查看:85
本文介绍了在Open RDF中查询存储库并显示结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将RDF数据存储区加载到OpenRDF存储库中,然后尝试查询存储库并根据查询检索某些数据.我正在运行一个非常简单的查询,以获取所有主题谓词和对象.但是,当我尝试打印查询结果时,它只是显示第一个三元组.我正在提供仅执行查询工作的方法的代码,请检查并让我知道出了什么问题.-

I have loaded an RDF data-store into an OpenRDF Repository, and then I am trying to query the Repository and retrieve certain data based on the query. I am running a very simple query to get all the subjects predicates and the objects. But when I am trying to print the query results it is just showing the first triple. I am giving the code of the method which is doing the query work only, please check and let me know what is wrong.-

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");
    }

}

推荐答案

TupleQueryResult是一个迭代器,因此,如果要查看所有结果,则需要对其进行迭代:

A TupleQueryResult is an iterator so if you want to see all results then you need to iterate it:

while (result.hasNext()) 
{
  BindingSet bindingSet = result.next();
  // Do what you want with the result
}
result.close();

如果您不知道如何使用迭代器,那么请看一下Oracle

If you don't know how to use iterators then take a look at the Oracle Collections tutorial for a brief introduction

这篇关于在Open RDF中查询存储库并显示结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆