在构造 sparql 查询中使用 union

发布于 2024-08-30 22:02:14 字数 533 浏览 4 评论 0原文

我有一个包含多个条目的 rdf 图。现在我想获取给定 id 的所有相关三元组。这是我的 sparql 查询:

select ?s ?p ?o from <http://localhost:8890/DAV/ranking> where {
 {<http://seekda.com/providers/cdyne.com/PhoneNotify> so:hasEndpoint ?s.
 ?s ?p ?o} union
 {<http://seekda.com/providers/cdyne.com/PhoneNotify> ?p ?o}
}

本例中的 ID 为

但我需要一个图形查询(constructdescribe)。所以我想我必须用一个union将它们打包在一起。我该怎么做?

I have an rdf graph with several entries. Now I want to get all related triples to a given id. This is my sparql query:

select ?s ?p ?o from <http://localhost:8890/DAV/ranking> where {
 {<http://seekda.com/providers/cdyne.com/PhoneNotify> so:hasEndpoint ?s.
 ?s ?p ?o} union
 {<http://seekda.com/providers/cdyne.com/PhoneNotify> ?p ?o}
}

The ID in this case is <seekda.com/providers/cdyne.com/PhoneNotify>.

But I need a graph query (construct or describe). So I think I have to pack them togheter with an union. How do I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

想挽留 2024-09-06 22:02:14

简短的回答是:没有区别。

更长的答案是:将 SPARQL 查询视为由两部分组成。

  1. 查询 (WHERE) 部分,生成变量绑定列表(尽管某些变量可能未绑定)。

    查询 (WHERE)

  2. 将结果组合在一起的部分。 SELECTASKCONSTRUCTDESCRIBE

SELECT * 实际上是查询返回的内容。 SELECT ?v1 ?v2 获取结果并生成另一个结果集,并删除其他变量。 ASK 只是查看是否有任何结果。

CONSTRUCT 使用模板根据结果生成 RDF。对于每个结果行,它绑定变量并将语句添加到结果模型中。如果模板三元组包含未绑定变量,则会跳过该变量。

DESCRIBE 是最不寻常的,因为它获取每个结果节点,找到与其关联的三元组,并将它们添加到结果模型中。与其他的不同,它可以包含比查询匹配更多的信息。

因此,所有形式都允许在查询中使用 UNIONOPTIONAL 等。由于未绑定的变量,它们可能会导致三元组丢失。

您的查询没有多大意义。这与 {?s ?p ?o} 没有什么不同。你想做什么? 好吧,现在更有意义了。

鉴于下面的澄清,听起来您想要以下内容:

construct { <http://seekda.com/providers/cdyne.com/PhoneNotify> ?p ?o }
from <http://localhost:8890/DAV/ranking> 
where {
  { <http://seekda.com/providers/cdyne.com/PhoneNotify> so:hasEndpoint ?s.
    ?s ?p ?o }
  union
  { <http://seekda.com/providers/cdyne.com/PhoneNotify> ?p ?o }
}

The short answer is: there is no difference.

The longer answer is: think about SPARQL queries as having two parts.

  1. The query (WHERE) part, which produces a list of variable bindings (although some variables may be unbound).

  2. The part which puts together the results. SELECT, ASK, CONSTRUCT, or DESCRIBE.

SELECT * is effectively what the query returns. SELECT ?v1 ?v2 takes results and produces another result set with the other variables removed. ASK just looks to see if there are any results.

CONSTRUCT uses a template to make RDF from the results. For each result row it binds the variables and adds the statements to the result model. If a template triple contains an unbound variable it is skipped.

DESCRIBE is the most unusual, since it takes each result node, finds triples associated with it, and adds them to a result model. Unlike the others it can contain more information than the query matches.

So having UNION, OPTIONAL, whatever, in the query is allowed for all forms. They may lead to missing triples due to unbound variables.

Your query doesn't make much sense. It's no different to {?s ?p ?o}. What are you trying to do? Ok, makes more sense now.

Given clarifications below it sounds like you want the following:

construct { <http://seekda.com/providers/cdyne.com/PhoneNotify> ?p ?o }
from <http://localhost:8890/DAV/ranking> 
where {
  { <http://seekda.com/providers/cdyne.com/PhoneNotify> so:hasEndpoint ?s.
    ?s ?p ?o }
  union
  { <http://seekda.com/providers/cdyne.com/PhoneNotify> ?p ?o }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文