Question

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

}
Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top