Created
May 27, 2021 13:26
-
-
Save naturzukunft/8bad67a2b44d66ff13f80c9e44e18635 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There's a couple of things wrong with this.
result.next()
twice in the loop, which will lead to skipping half of your resultsSimple fix:
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:
Alternatively, if you want to keep track of the result outside the block, materialize the result iterator to a List: