Skip to content

Instantly share code, notes, and snippets.

@naturzukunft
Created May 27, 2021 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naturzukunft/8bad67a2b44d66ff13f80c9e44e18635 to your computer and use it in GitHub Desktop.
Save naturzukunft/8bad67a2b44d66ff13f80c9e44e18635 to your computer and use it in GitHub Desktop.
package de.naturzukunft.rdf4j.sparql.spring.controller;
import org.eclipse.rdf4j.query.TupleQuery;
import org.eclipse.rdf4j.query.TupleQueryResult;
import org.eclipse.rdf4j.repository.RepositoryConnection;
import org.eclipse.rdf4j.repository.sparql.SPARQLRepository;
public class TestSparqlRepository {
public static void main(String[] args) {
String query = "select distinct ?Concept where {[] a ?Concept} LIMIT 100";
String url = "https://dbpedia.org/sparql";
SPARQLRepository repo = new SPARQLRepository(url);
TupleQueryResult result = null;
try (RepositoryConnection conn = repo.getConnection()) {
TupleQuery tupleQuery = conn.prepareTupleQuery(query);
result = tupleQuery.evaluate();
}
int count = 0;
while (result.hasNext()) {
System.out.println(result.next());
result.next();
count++;
}
System.out.println(count);
}
}
@abrokenjester
Copy link

abrokenjester commented May 27, 2021

There's a couple of things wrong with this.

  1. you're calling result.next() twice in the loop, which will lead to skipping half of your results
  2. you're iterating over the result iteration outside the try-with-resources block. That means the connection has been closed and you won't get any results.

Simple fix:

	public static void main(String[] args) throws Exception {
		String query = "select distinct ?Concept where {[] a ?Concept} LIMIT 100";
		String url = "http://dbpedia.org/sparql";
		SPARQLRepository repo = new SPARQLRepository(url);
		try (RepositoryConnection conn = repo.getConnection()) {
			TupleQuery tupleQuery = conn.prepareTupleQuery(query);
			TupleQueryResult result = tupleQuery.evaluate();
			int count = 0;
			while (result.hasNext()) {
				System.out.println(result.next());
				count++;
			}
			System.out.println(count);
		}
	}

Btw using hasNext()/next() can lead to mistakes (as in your example). Easier is to use a for-each loop instead of a while loop:

for(BindingSet bs: result) {
   System.out.println(bs);
   count++;
}

Alternatively, if you want to keep track of the result outside the block, materialize the result iterator to a List:

	public static void main(String[] args) throws Exception {
		String query = "select distinct ?Concept where {[] a ?Concept} LIMIT 100";
		String url = "http://dbpedia.org/sparql";
		SPARQLRepository repo = new SPARQLRepository(url);
                List<BindingSet> result = null;
		try (RepositoryConnection conn = repo.getConnection()) {
			TupleQuery tupleQuery = conn.prepareTupleQuery(query);
			result = QueryResults.asList(tupleQuery.evaluate());
		}
                result.forEach(System.out::println);
                System.out.println(result.size());
	}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment