ARQ 从头开始进行查询
我在从头开始语法或代数构建查询时遇到问题,基于 https://jena.apache.org/documentation/query/manipulate_sparql_using_arq.html
例如,我有以下查询:
SELECT (count(?instance) AS ?count)
WHERE
{ ?instance <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
<http://data.linkedmdb.org/resource/movie/film> }
(project (?count)
(extend ((?count ?.0))
(group () ((?.0 (count ?instance)))
(bgp (triple ?instance <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://data.linkedmdb.org/resource/movie/film>)))))
任何人都可以用示例代码指导我如何从头开始构建上述查询吗? 我尝试按语法构建它,但不知道如何为上面的聚合添加别名。
如果有人至少可以指导我在投影中包含聚合及其别名,那就太好了。
I have a problem in building the query from scratch syntactically or in algebra, based on
https://jena.apache.org/documentation/query/manipulating_sparql_using_arq.html
For example I have the below query
SELECT (count(?instance) AS ?count)
WHERE
{ ?instance <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
<http://data.linkedmdb.org/resource/movie/film> }
(project (?count)
(extend ((?count ?.0))
(group () ((?.0 (count ?instance)))
(bgp (triple ?instance <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://data.linkedmdb.org/resource/movie/film>)))))
Can any one direct me with a sample code of how to build the above query from scratch?
I have tried to build it syntactically but failing to know about how to alias the aggregation above.
If anybody can at least guide me in including aggregation with its aliasing name in projection it will be very great.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通常不会通过代码构建查询,因为我只能解析查询字符串,或使用参数化 SPARQL 查询,但这里是使用 API 重建查询的方式。我在这里使用的大多数方法都是通过探索 Eclipse 中的自动完成选项以及查看 Javadoc 找到的。
输出(即打印的查询)如下。它与您的查询相同,以一些空白位置和换行符为模。
I don't typically construct queries through code, since I can just parse a query string, or use a parameterized SPARQL query, but here's a reconstruction of your query using the API. Most of the methods I used here I found by exploring the autocomplete options in Eclipse, and by looking at the Javadoc.
The output (i.e., the printed query) follows. It's the same as your query, modulo some whitespace location and newlines.
尽管Joshua提出的解决方案对我非常有帮助并且产生了正确的字符串输出,但我发现它包含一个问题;该行:
query.getProject().add( Var.alloc( "count" ), new ExprAggregator( instance.asVar(), new AggCountVar( instance )));
应替换为:
< code>query.getProject().add( Var.alloc( "count" ), query.allocAggregate( new AggCountVar( instance ) ));
否则如果执行针对模型的查询,您将收到异常“NotAVariableException:找到 Node_variable(不是 Var)”
Although the solution proposed by Joshua was very helpful to me and produces the correct String output, I have found that it contains a problem; the line :
query.getProject().add( Var.alloc( "count" ), new ExprAggregator( instance.asVar(), new AggCountVar( instance )));
Should be replaced by :
query.getProject().add( Var.alloc( "count" ), query.allocAggregate( new AggCountVar( instance ) ));
Otherwise if you execute the query against a Model you will get an exception "NotAVariableException: Node_variable (not a Var) found"