使用外部ID

Dgraph的输入语言RDF也支持<a_fixed_identifier> <predicate> literal/node的三元组及其变体,其中标签a_fixed_identifier`是一个节点的唯一标识符。例如,混合使用schema.org标识符、电影数据库标识符和空节点:

_:userA <http://schema.org/type> <http://schema.org/Person> .
_:userA <dgraph.type> "Person" .
_:userA <http://schema.org/name> "FirstName LastName" .
<https://www.themoviedb.org/person/32-robin-wright> <http://schema.org/type> <http://schema.org/Person> .
<https://www.themoviedb.org/person/32-robin-wright> <http://schema.org/name> "Robin Wright" .

Dgraph原生不支持节点标识符这样的外部id,但外部id可以被存储为带有xid边的节点的属性。例如,在上面的例子中,谓词名称在Dgraph中是有效的,但是用<http: schema.org="" person="">标识的节点可以用UID(比如0x123)和一条边在Dgraph中标识:

<0x123> <xid> "http://schema.org/Person" .
<0x123> <dgraph.type> "ExternalType" .

Robin Wright可能获得的UID0x321和三元组:

<0x321> <xid> "https://www.themoviedb.org/person/32-robin-wright" .
<0x321> <http://schema.org/type> <0x123> .
<0x321> <http://schema.org/name> "Robin Wright" .
<0x321> <dgraph.type> "Person" .

相应的Schema应该是下面这样子的:

xid: string @index(exact) .
<http://schema.org/type>: [uid] @reverse .

查询所有人的例子:

{
  var(func: eq(xid, "http://schema.org/Person")) {
    allPeople as <~http://schema.org/type>
  }

  q(func: uid(allPeople)) {
    <http://schema.org/name>
  }
}

通过外部ID查询Robin Wright的例子:

{
  robin(func: eq(xid, "https://www.themoviedb.org/person/32-robin-wright")) {
    expand(_all_) { expand(_all_) }
  }
}

注意:xid边不会在变更中自动添加。一般来说,用户应该自己管理xid,并在必要时添加节点和xid边。Dgraph将所有此类xid的唯一性检查留给外部程序。