如何在 Gremlin 中进行高效的批量更新插入(插入新顶点或更新属性)?

Eas*_*ons 5 graph gremlin tinkerpop tinkerpop3 amazon-neptune

上下文

我确实有一个大约有 2000 个顶点和 6000 条边的图,随着时间的推移,这可能会增长到 10000 个顶点和 100000 条边。目前我正在使用以下遍历查询更新新顶点:

向上插入顶点和边

queryVertex = "g.V().has(label, name, foo).fold().coalesce(
               unfold(), addV(label).property(name, foo).property(model, 2)
               ).property(model, 2)"
Run Code Online (Sandbox Code Playgroud)

这里的目的是寻找名为 foo 的顶点,如果找到则更新其model属性,否则创建一个新顶点并设置该model属性。这发出两次:一次是针对源顶点,一次是针对目标顶点。
创建两个相关顶点后,将发出另一个查询以创建它们之间的边:

queryEdge = "g.V('id_of_source_vertex').coalesce(
             outE(edge_label).filter(inV().hasId('id_of_target_vertex')), 
             addE(edge_label).to(V('id_of_target_vertex'))
             ).property(model, 2)"
Run Code Online (Sandbox Code Playgroud)

在这里,如果两个顶点之间存在model边,则更新边上的属性,否则会在它们之间创建边。

执行此操作的伪代码如下:

for each edge in the list of new edges:
   //upsert source and target vertices:  
   execute queryVertex for edge.source
   execute queryVertex for edge.target
   // upsert edge: 
   execute queryEdge
Run Code Online (Sandbox Code Playgroud)

这行得通,但效率极低;例如,对于上述图形大小,它需要几分钟才能完成,并且通过一些应用内并发,它只将时间减少了几分钟。当然,对于如此小的图尺寸,必须有一种更有效的方法来做到这一点。

问题
* 如何使这些 upsert 更快?

ste*_*tte 2

批量加载通常应归咎于为处理此类任务而优化的提供商特定工具。Gremlin 实际上没有提供抽象来涵盖用于实现 TinkerPop 的各种图形数据库系统的各种批量加载工具。对于 Neptune,这就是您标记问题的方式,这意味着使用Neptune Bulk Loader

具体来说您的问题,尽管您可能会看到您所描述的方法的一些优化。从 Gremlin 的角度来看,我想通过结合现有的遍历,为每个边提交一个 Gremlin 请求,您会看到一些节省:

g.V().has(label, name, foo).fold().
  coalesce(unfold(), 
           addV(label).property(name, foo)).
  property(model, 2).as('source').
  V().has(label, name, bar).fold().
  coalesce(unfold(), 
           addV(label).property(name, bar)).
  property(model, 2).as('target').
  coalesce(inE(edge_label).where(outV().as('source')), 
           addE(edge_label).from('source').to('target')).
  property(model, 2)
Run Code Online (Sandbox Code Playgroud)

我想我是对的——未经测试,但希望你能明白。基本上,我们只是通过步骤标签引用内存中已有的顶点,这样我们就不需要重新查询它们。如果您继续使用 Gremlin 风格的批量加载(例如对边进行排序),您也可以尝试其他策略,以便可以将更多的边加载批处理在一起,以减少顶点查找量并以更动态的方式提交顶点/边数据,如此处所述