【问题标题】:Is it possible to use a nested delete in SPARQL queries?是否可以在 SPARQL 查询中使用嵌套删除?
【发布时间】:2016-09-21 08:10:07
【问题描述】:

我想使用查询来使用唯一 ID 对资源进行重复数据删除。插入/删除 - 查询不起作用,因为创建的节点比删除的节点少。是否可以使用类似的东西?

insert {
    ?new a mails:Account.
    ?new mails:hasID ?id.
    ?new rdfs:label ?label
  }
where {
    {
        select distinct ?id ?label where {
            ?account a mails:Account.
            ?account mails:hasID ?id.
            ?account rdfs:label ?label
        }
    }
    bind(bnode() as ?new)
    {
        delete where {
            ?account mails:hasID ?id
        }
    }
}

【问题讨论】:

    标签: sparql blank-nodes


    【解决方案1】:

    只是“因为必须创建的节点少于删除的节点”并不一定意味着您不能使用正常的插入/删除。 RDF 是基于集合的表示;如果多次插入相同的三元组,则与插入一次相同。如果你想标准化一堆三元组,你可以通过使用带有参数的 bnode 来为查询结果创建相同的空白节点:(添加了重点):

    BNODE 函数构造了一个与所有节点不同的空白节点 正在查询的数据集中的空白节点与所有空白不同 通过调用此构造函数为其他查询解决方案创建的节点。 如果使用无参数形式,则每次调用都会产生不同的 空白节点。 如果使用带有简单文字的表单,则每次调用 为不同的简单文字产生不同的空白节点,并且 具有相同简单文字的调用的相同空白节点 一种解决方案映射的表达式。

    这意味着你可以这样做:

    insert {
      ?new a mails:Account.
      ?new mails:hasID ?id.
      ?new rdfs:label ?label
    }
    delete {
      ?account mails:hasId ?id
    }
    where {
      ?account a mails:Account.
      ?account mails:hasID ?id.
      ?account rdfs:label ?label
    
      #-- One new bnode is created for each *distinct*
      #-- ?id value.  If two accounts have the same 
      #-- ?id value, then they will get the same bnode().
      bind (bnode(str(?id)) as ?new)
    }
    

    如果您尝试将 所有 帐户合并为一个,即使它们具有不同的 ID,那么您可以将一个常量值传递给 bnode 函数,例如,

    bind (bnode("") as ?new)
    

    【讨论】:

    • 很好的解决方案。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-01
    • 1970-01-01
    • 2017-10-02
    • 2017-02-19
    • 1970-01-01
    • 2015-04-04
    相关资源
    最近更新 更多