SPARQL Query “COUNT” in Virtuoso Jena API - QueryP

2019-02-21 02:25发布

问题:

Same query works in DBpedia Endpoint(http://ko.dbpedia.org/sparql), but not in my Java code. I am just trying to extract the frequency using "COUNT" function.

VirtGraph set = new VirtGraph("http://ko.dbpedia.org", HOST, USERNAME, PASSWORD);
Query freqsparql = QueryFactory.create("SELECT ?class count(distinct ?s) as ?count where{?s <http://ko.dbpedia.org/property/이름> ?o. ?s a ?class.} order by DESC(?count)");
VirtuosoQueryExecution freqvqe = VirtuosoQueryExecutionFactory.create(freqsparql, set);
ResultSet freqresults = freqvqe.execSelect();

And the error is as follows.

Exception in thread "main" com.hp.hpl.jena.query.QueryParseException: Encountered " "count" "count "" at line 1, column 15.
Was expecting one of:
<VAR1> ...
<VAR2> ...
"from" ...
"where" ...
"(" ...
"{" ...

at com.hp.hpl.jena.sparql.lang.ParserSPARQL11.perform(ParserSPARQL11.java:102)
at com.hp.hpl.jena.sparql.lang.ParserSPARQL11.parse$(ParserSPARQL11.java:53)
at com.hp.hpl.jena.sparql.lang.SPARQLParser.parse(SPARQLParser.java:37)
at com.hp.hpl.jena.query.QueryFactory.parse(QueryFactory.java:148)
at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:80)
at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:53)
at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:41)

I am using virt_jena2.jar and virtjdbc4.jar. I have been looked through similar questions and answers(Jena ARQ extension and SPARQL 1.1 supports this aggregated query - But I couldn't find how to change it - I think I'm using SPARQL1.1 from the fact that error message includes PARSERSPARQL11.java ), but can't figure out how to solve this at this point.

Thanks in advance.


String sparqlQueryString = "SELECT ?class count(distinct ?s) as ?count    where{?s <http://ko.dbpedia.org/property/이름> ?o. ?s a ?class.} order by DESC(?count)";
Query query = QueryFactory.create(sparqlQueryString);
QueryExecution qexec = QueryExecutionFactory.sparqlService(
                "http://ko.dbpedia.org/sparql", query);
try {
    ResultSet results = qexec.execSelect();
    while(results.hasNext()){
        QuerySolution freqresult = results.nextSolution();
        RDFNode domain = freqresult.get("class");
        RDFNode freqcount = freqresult.get("count");
        System.out.println(freqresult);
        System.out.println(domain + "---" + freqcount);
    }
} catch (Exception e) {
    e.printStackTrace();
} finally {
    qexec.close();
}

This Jena code (without Virtuoso) gives me same error message.

回答1:

This is illegal SPARQL syntax:

SELECT ... count(distinct ?s) as ?count where

It should be

SELECT ... (count(distinct ?s) as ?count) where

The you will have a problem with ?class in:

SELECT ?class (count(distinct ?s) as ?count) where

because it is not a grouped variable (using count you have a group of everything). Did you mean to have a GROUP BY ?class?



回答2:

The keyword is Encountered " "count" "COUNT ""