简体   繁体   English

如何将SPARQL查询的结果重用于另一个查询?

[英]How can I reuse the result of a SPARQL query to another query?

For example I have this query 例如我有这个查询

SELECT ?x
WHERE {?x :has_input "z"}

then i want to use the result/s of ?x as object to another query 然后我想使用?x的结果作为另一个查询的对象

SELECT ?y
WHERE {?y :uses "x"}

Any ideas how to achieve that? 有什么想法要实现吗? Thanks in advance 提前致谢

For the sake of the example, let's define some data: 为了举例说明,让我们定义一些数据:

@prefix : <http://example.org/> .

:node0 :has_input "w", "z" .
:node1 :has_input "x", "y" .
:node2 :has_input "y", "z" .
:node3 :uses :node2 .
:node4 :uses :node1 .

Based on this data, and with specifying any particular API (because you didn't), you've got a few SPARQL level options. 基于此数据,并指定了任何特定的API(因为没有指定),您就有了一些SPARQL级别的选项。 The first is simply combining the queries, which is easy enough in this case: 首先是简单地组合查询,在这种情况下这很容易:

prefix : <http://example.org/>

select ?y where { 
  ?x :has_input "z" .
  ?y :uses ?x .
}

$ arq --data data.n3 --query combined-query.sparql 
----------
| y      |
==========
| :node3 |
----------

Another option is to use a subquery 另一种选择是使用子查询

prefix : <http://example.org/>

select ?y where { 
  {
    select ?x where { 
      ?x :has_input "z" .
    }
  }
  ?y :uses ?x .
}


$ arq --data data.n3 --query subquery.sparql
----------
| y      |
==========
| :node3 |
----------

A third, which may be what you actually need, if you have to execute the queries separately, is to execute a query that finds values for ?x for you, and then execute a query that finds ?y , but with the ?x values embedded with values . 第三种可能是您实际需要的,如果您必须分别执行查询,则是执行一个查询,该查询为您找到?x值,然后执行一个查询,找到?y ,但带有?x值嵌入values The first query looks like and returns: 第一个查询如下所示,并返回:

prefix : <http://example.org/>

select ?x where { 
  ?x :has_input "z" .
}

$ arq --data data.n3 --query xquery.sparql
----------
| x      |
==========
| :node2 |
| :node0 |
----------

Then, based on those values, you create the query for ?y : 然后,基于这些值,为?y创建查询:

prefix : <http://example.org/>

select ?y where { 
  values ?x { :node2 :node0 }
  ?y :uses ?x .
}

$ arq --data data.n3 --query yquery.sparql
----------
| y      |
==========
| :node3 |
----------

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM