我正在使用Jena启动SPARQL查询。我有这个代码,产生错误。 我不明白这个错误的原因,因为将查询放入DBpedia SPARQL endpoint工作!我认为我正确地编写了查询字符串。什么是错误?
String sparqlQueryString=
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "+
"select ?sub ?super (count(?mid) as ?length) where {"+
"values ?sub { <http://dbpedia.org/ontology/Writer> }" +
"?sub rdfs:subClassOf* ?mid ."+
"?mid rdfs:subClassOf+ ?super .}"+
"group by (?sub ?super)"+
"order by (?length)";
query = QueryFactory.create(sparqlQueryString);
QueryExecution qexec =
QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql",query);
Exception in thread "main" com.hp.hpl.jena.query.QueryParseException: Encountered "
<VAR1> "?super "" at line 1, column 231.
Was expecting one of:
"not" ...
"as" ...
"in" ...
<INTEGER_POSITIVE> ...
<DECIMAL_POSITIVE> ...
<DOUBLE_POSITIVE> ...
<INTEGER_NEGATIVE> ...
<DECIMAL_NEGATIVE> ...
<DOUBLE_NEGATIVE> ...
")" ...
"=" ...
"!=" ...
">" ...
"<" ...
"<=" ...
">=" ...
"||" ...
"&&" ...
"+" ...
"-" ...
"*" ...
"/" ...
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:156)
at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:79)
at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:52)
at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:40)
at Query.QueryRDF.retrieveSuperClasses(QueryRDF.java:87)
at Query.QueryRDF.main(QueryRDF.java:144)
答案 0 :(得分:3)
不要在GROUP BY
变量周围加上括号。也就是说,它应该是group by ?sub ?super
,
而不是group by (?sub ?super)
。如果您在查询中添加带\n
的换行符,则非常清楚,以便更容易查看错误的位置。例如,当我尝试编译以下代码时,我得到以下运行时错误。
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
public class ParseError {
@SuppressWarnings("unused")
public static void main(String[] args) {
String sparqlQueryString=
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> \n"+
"select ?sub ?super (count(?mid) as ?length) where {\n"+
"values ?sub { <http://dbpedia.org/ontology/Writer> }\n" +
"?sub rdfs:subClassOf* ?mid .\n"+
"?mid rdfs:subClassOf+ ?super .}\n"+
"group by (?sub ?super)\n"+
"order by (?length)\n";
Query query = QueryFactory.create(sparqlQueryString);
QueryExecution qexec =
QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql",query);
}
}
线程“main”中的异常com.hp.hpl.jena.query.QueryParseException:在第6行第16列遇到“”?super“”。
错误指向有问题的行。这里不需要括号,因为语法中的GroupClause
生成需要一个或多个GroupCondition
s,其具有由此生产定义的形式:
GroupCondition :: = BuiltInCall | FunctionCall | '('表达式('AS'Var)?')'| VAR
如果有GROUP BY (...)
它应该是
GROUP BY ( ?a+?b )
GROUP BY ( ?a+?b as ?abSum )
您也可以通过粘贴查询对此进行测试
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select ?sub ?super (count(?mid) as ?length) where {
values ?sub { <http://dbpedia.org/ontology/Writer> }
?sub rdfs:subClassOf* ?mid .
?mid rdfs:subClassOf+ ?super .}
group by (?sub ?super)
order by (?length)
进入sparql.org's query validator,您将从中获得输出:
输入:
1 PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 2 select ?sub ?super (count(?mid) as ?length) where { 3 values ?sub { <http://dbpedia.org/ontology/Writer> } 4 ?sub rdfs:subClassOf* ?mid . 5 ?mid rdfs:subClassOf+ ?super .} 6 group by (?sub ?super) 7 order by (?length)
语法错误
Encountered " "?super "" at line 6, column 16. Was expecting one of: "not" ... "as" ... "in" ... ...