Search code examples
javasparqljenasemantic-webdbpedia

Why am i getting a single result for this sparql query?


I executed the query at the sparql endpoint and got multiple results. However on executing it in Netbeans, i get just the one result. public class FetchCountryData {

public void fetchData() {
    ParameterizedSparqlString qs = new ParameterizedSparqlString("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"
            + "PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>\n"
            + "PREFIX dbo: <http://dbpedia.org/ontology/>\n"
            + "\n"
            + "SELECT distinct ?country ?capital ?caplat ?caplong\n"
            + "WHERE {\n"
            + "  ?country rdf:type dbo:Country .\n"
            + "  ?country  dbo:capital ?capital .\n"
            + "  OPTIONAL {\n"
            + "    ?capital geo:lat ?caplat ;\n"
            + "      geo:long ?caplong .\n"
            + "  }\n"
            + "}\n"
            + "ORDER BY ?country");

    QueryExecution exec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", qs.asQuery());
    exec.setTimeout(10000000);
    ResultSet results = exec.execSelect();
    if (results.hasNext()) {
        System.out.println(results.next());
    }
}

}

Output

( ?capital = http://dbpedia.org/resource/Nicosia ) ( ?caplong = "33.3667"^^xsd:float ) ( ?caplat = "35.1667"^^xsd:float ) ( ?country <=http://dbpedia.org/resource/1974_Cypriot_coup_d'état> )


Solution

  • You are not iterating through the results. Change the "if" to:

    while (results.hasNext()){
    System.out.println(results.next());
    }