简体   繁体   中英

How can I get the result of a “Select count(*)” query with Apache Jena?

I tried many times to get the result of a select query count with apache jena in JAVA but it doesn't work.

select count(*) where { 
        ?s dbo:award ?o.
        ?o rdf:type ?C.
        FILTER
        regex(?C,"http://dbpedia.org/ontology/Award")}

The result of this query in the endpoint is 33767

So I made this code in java to get the result.

public int CR() {

    int CR=0;
    String ch="http://dbpedia.org/ontology/";
    String service="http://dbpedia.org/sparql";
    String query="PREFIX dbo:<http://dbpedia.org/ontology/>" 
    + "PREFIX : <http://dbpedia.org/resource/>" 
    + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
    + "PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>"

    +"select * where { "
        +"?s dbo:award ?o."
        +"?o rdf:type ?C."
        +"FILTER"
        +"regex(?C,\"http://dbpedia.org/ontology/Award\")}";
    QueryExecution qe=QueryExecutionFactory.sparqlService(service, query);
    ResultSet rs=qe.execSelect(); 
    while (rs.hasNext()){ 
        QuerySolution s=rs.nextSolution();

        CR++;
        System.out.println(CR);
        }
    ;
    return CR;
}

The result that I've got in the console does not exceed 10000.

.
.
9992
9993
9994
9995
9996
9997
9998
9999
10000

Please I need an example of a select count query with apache jena, or find what's wrong with my code.

Thank you !

Dbpedia virtuoso, like many open sparql endpoints, has a limit in the number of results it will return. Use LIMIT and OFFSET to get the results in batches. This will give you the second batch of 10000:

SELECT * WHERE { 
    ?s dbo:award ?o .
    ?o rdf:type ?C .
    FILTER STRSTARTS(STR(?C), "http://dbpedia.org/ontology/Award")
}
LIMIT 10000
OFFSET 10000

Repeat with offset 0, 10000, 20000 etc. until the result is empty.

PS. Since your filter actually matches a complete type URI, you can drop the filter altogether and simply match ?o rdf:type <http://dbpedia.org/ontology/Award> (or equivalently: ?o rdf:type dbo:Award ) in the second condition. It's far more efficient.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM