ARQ 从头开始​​进行查询

发布于 2024-12-17 12:33:17 字数 794 浏览 6 评论 0原文

我在从头开始语法或代数构建查询时遇到问题,基于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

司马昭之心 2024-12-24 12:33:17

我通常不会通过代码构建查询,因为我只能解析查询字符串,或使用参数化 SPARQL 查询,但这里是使用 API 重建查询的方式。我在这里使用的大多数方法都是通过探索 Eclipse 中的自动完成选项以及查看 Javadoc 找到的。

import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.sparql.core.Var;
import com.hp.hpl.jena.sparql.expr.ExprAggregator;
import com.hp.hpl.jena.sparql.expr.ExprVar;
import com.hp.hpl.jena.sparql.expr.aggregate.AggCountVar;
import com.hp.hpl.jena.sparql.syntax.ElementTriplesBlock;
import com.hp.hpl.jena.vocabulary.RDF;

public class QueryBuilding {
    public static void main(String[] args) {
        // Create the query and make it a SELECT query.
        final Query query = QueryFactory.create();
        query.setQuerySelectType();

        // Set the projection expression.
        final ExprVar instance = new ExprVar( "instance" );
        query.getProject().add( Var.alloc( "count" ), new ExprAggregator( instance.asVar(), new AggCountVar( instance )));

        // Construct the triples pattern and add it.
        final ElementTriplesBlock triples = new ElementTriplesBlock();
        final Node film = Node.createURI( "http://data.linkedmdb.org/resource/movie/film" );
        triples.addTriple( new Triple( instance.getAsNode(), RDF.type.asNode(), film ));
        query.setQueryPattern( triples );

        // Show the query 
        System.out.println( 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> .}

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.

import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.sparql.core.Var;
import com.hp.hpl.jena.sparql.expr.ExprAggregator;
import com.hp.hpl.jena.sparql.expr.ExprVar;
import com.hp.hpl.jena.sparql.expr.aggregate.AggCountVar;
import com.hp.hpl.jena.sparql.syntax.ElementTriplesBlock;
import com.hp.hpl.jena.vocabulary.RDF;

public class QueryBuilding {
    public static void main(String[] args) {
        // Create the query and make it a SELECT query.
        final Query query = QueryFactory.create();
        query.setQuerySelectType();

        // Set the projection expression.
        final ExprVar instance = new ExprVar( "instance" );
        query.getProject().add( Var.alloc( "count" ), new ExprAggregator( instance.asVar(), new AggCountVar( instance )));

        // Construct the triples pattern and add it.
        final ElementTriplesBlock triples = new ElementTriplesBlock();
        final Node film = Node.createURI( "http://data.linkedmdb.org/resource/movie/film" );
        triples.addTriple( new Triple( instance.getAsNode(), RDF.type.asNode(), film ));
        query.setQueryPattern( triples );

        // Show the query 
        System.out.println( query );
    }
}

The output (i.e., the printed query) follows. It's the same as your query, modulo some whitespace location and newlines.

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> .}
玩套路吗 2024-12-24 12:33:17

尽管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"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文