Graph 在Gremlin中同时合并顶点和它们之间的边

Graph 在Gremlin中同时合并顶点和它们之间的边,graph,gremlin,Graph,Gremlin,我有一个数据源,我读取并尝试将其加载到我的CosmosDB图中。我获取的每一行都包含关于多个实体(个人、软件)的信息。我在这里要做的是: 验证此类顶点是否已存在,并为丢失的条目(人员、软件)生成单独的实体 验证边缘是否已存在(此人与此软件之间) 在它们之间创建一条边 我已经提到了以下主题: ,试图以某种方式将它们结合起来,但没有取得多大成功 我尝试了以下方法: g.V().has('person','name','vadas'). fold().coalesce(unfold(), ad

我有一个数据源,我读取并尝试将其加载到我的CosmosDB图中。我获取的每一行都包含关于多个实体(个人、软件)的信息。我在这里要做的是:

  • 验证此类顶点是否已存在,并为丢失的条目(人员、软件)生成单独的实体
  • 验证边缘是否已存在(此人与此软件之间)
  • 在它们之间创建一条边
我已经提到了以下主题: ,试图以某种方式将它们结合起来,但没有取得多大成功

我尝试了以下方法:

g.V().has('person','name','vadas').
  fold().coalesce(unfold(), addV('person').property('name','vadas')).as('v').
  V().has('software', 'name','ripple').
  fold().coalesce(unfold(), addV('software').property('name','ripple')).
  coalesce(__.inE('created').where(outV().as('v')), addE('created').from('v'))
但它只创建顶点,而不创建顶点之间的边

我还想知道是否有一种更常见的方法:

1. Upsert entity A and keep a reference to it
2. Upsert entity B and keep a reference to it
3. Upsert entity C and keep a reference to it
....
1. Upsert edge between A and B
2. Upsert edge between A and C

您有一个减少障碍步骤(即
fold()
),它位于步骤标签“v”和尝试在
where()
中检索它之间。注意下面的示例中更明确地发生了什么:

gremlin> g.V().has('person','name','vadas').
......1>   fold().coalesce(unfold(), addV('person').property('name','vadas')).as('v').
......2>   V().has('software', 'name','ripple').
......3>   fold()
==>[v[5]]
gremlin> g.V().has('person','name','vadas').
......1>   fold().coalesce(unfold(), addV('person').property('name','vadas')).as('v').
......2>   V().has('software', 'name','ripple').
......3>   fold().select('v')
gremlin>
如您所见,您不能
选择('v')
。该遍历器的路径历史记录已消失。这是因为您已将流从多个遍历器减少为一个,所以“v”在该合并中丢失了上下文

当这种情况发生时,您通常只需要重新编写一点遍历。在您的情况下,我可能会这样做,因为它引起的更改最少,并且可以很好地保持可读性:

gremlin> g.V().has('person','name','vadas').
......1>   fold().
......2>   coalesce(unfold(), addV('person').property('name','vadas')).
......3>   V().has('software', 'name','ripple').
......4>   fold().
......5>   coalesce(unfold(), addV('software').property('name','ripple')).
......6>   coalesce(__.inE('created').where(outV().has('person','name','vadas')), 
......7>            addE('created').from(V().has('person','name','vadas')))
==>e[24][2-created->5]