Querying a repository in Open RDF and displaying the results

柔情痞子 提交于 2020-01-24 15:17:07

问题


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

}

回答1:


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();

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



来源:https://stackoverflow.com/questions/23613416/querying-a-repository-in-open-rdf-and-displaying-the-results

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!