SPARQL 查询“COUNT"在 Virtuoso Jena API 中 - QueryParseException [英] SPARQL Query "COUNT" in Virtuoso Jena API - QueryParseException

查看:28
本文介绍了SPARQL 查询“COUNT"在 Virtuoso Jena API 中 - QueryParseException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

同样的查询在 DBpedia Endpoint(http://ko.dbpedia.org/sparql),但不是在我的 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();
}

这个 Jena 代码(没有 Virtuoso)给了我同样的错误信息.

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

推荐答案

这是非法的 SPARQL 语法:

This is illegal SPARQL syntax:

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

应该是

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

你在使用 ?class 时会遇到问题:

The you will have a problem with ?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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆