问题描述
相同的查询在DBpedia端点()中有效,但不是在我的Java代码。
我只是试图使用COUNT函数提取频率。
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();
错误如下。
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)
我使用virt_jena2.jar和virtjdbc4.jar。
我已经通过类似的问题和答案(Jena ARQ扩展和SPARQL 1.1支持这个聚合查询 - 但我找不到如何更改它 - 我想我使用SPARQL1.1从事实,错误消息包括PARSERSPARQL11.java),但不能弄清楚如何解决这一点。
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.
提前感谢。
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();
}
推荐答案
这是非法的SPARQL语法:
This is illegal SPARQL syntax:
SELECT ... count(distinct ?s) as ?count where
应该是
SELECT ... (count(distinct ?s) as ?count) where
您将遇到?class
in:
SELECT ?class (count(distinct ?s) as ?count) where
,因为它不是分组变量(使用 count
组的一切)。您是不是要有 GROUP BY?class
?
because it is not a grouped variable (using count
you have a group of everything). Did you mean to have a GROUP BY ?class
?
这篇关于SPARQL查询“COUNT”在Virtuoso Jena API - QueryParseException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!