Apache Jena - got 3 identical results for one request

This is my first question here on StackOverflow. I am using Apache Jena to query DBPedia and the results are quite strange. Here's my code, with a simple query:

    String sparqlQuery = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> select ?o where { <http://dbpedia.org/ontology/Agent> rdfs:subClassOf  ?o}";
    System.out.println("Query : " + sparqlQuery);
    Query query = QueryFactory.create(sparqlQuery);
    QueryExecution qexec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", query); 
    ResultSet results = qexec.execSelect();
    ResultSetFormatter.out(System.out, results, query);
    qexec.close();

      

And this is what I got back:

<http://www.w3.org/2002/07/owl#Thing> <http://www.w3.org/2002/07/owl#Thing> <http://www.w3.org/2002/07/owl#Thing>

Any idea why I am not getting any resources? I tried with other resources, with the same problem.

Thanks for your help and have a nice day!

+3


source to share


2 answers


It's kind of strange, when I check your request at http://dbpedia.org/sparql I only have one resource.

Can you check if yours has ResultSet

three results?

int count = 0;
while( results.hasNext() ){
  results.next();
  count++;
}
System.out.println("I see "+count+" rows !");

      



As a workaround, you can use the keyword DISTINCT

:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
SELECT DISTINCT ?o
WHERE { 
  <http://dbpedia.org/ontology/Agent> rdfs:subClassOf  ?o
}

      

+2


source


The request is sent to dbpedia, so it gives three responses. Yen only formats the results.

Perhaps this is due to the fact that there are three triplets in different named graphs - by default dbpedia graph is the union of all named graphs.

Try:



select *{ GRAPH ?g { <http://dbpedia.org/ontology/Agent> rdfs:subClassOf  ?o} }

      

Also check the results: execute the request with wget or curl and see the bytes sent back.

(The answer you are showing does not match the ResultSetFormatter output)

+2


source







All Articles