如何在单个 gremlin 查询中添加多条边?

How to add multiple edges in a single gremlin query?

我的方案是在单个查询中的顶点之间添加多条边:

假设以下节点: 这些是我的标签和 ID

用户:

4100

歌曲:

4200

4355

4676

我必须在这些顶点之间建立边

4100 --> 4200, 
4100 --> 4355, 
4100 --> 4676.

我们通常可以通过在 node.it 之间创建单个边来做到这一点,如果我们想一次在超过 50 个顶点之间创建边,这不是一种有效的方法。我正在使用 Tinkerpop 3.0.1

如果你有顶点id,通过id查找是非常有效的。如果您使用的是 Gremlin 服务器,则对 Gremlin 服务器的每个请求都被视为单个事务。您可以针对单个请求(使用绑定)在 Gremlin 查询中传递多个语句,而不是发送多个请求。用分号分隔 Gremlin 查询中的语句。

l=[4200, 4355, 4676]; v=graph.vertices(4100).next(); l.each { v.addEdge("knows", graph.vertices(it).next()) }

使用最新的 Tinkerpop。您可以执行以下操作:

创建示例图:

gremlin> graph = TinkerGraph.open();
gremlin> graph.addVertex("User").property("id", 4100);
==>vp[id->4100]
gremlin> graph.addVertex("Song").property("id", 4200);
==>vp[id->4200]
gremlin> graph.addVertex("Song").property("id", 4355);
==>vp[id->4355]
gremlin> graph.addVertex("Song").property("id", 4676);
==>vp[id->4676]

现在在一次遍历中添加边:

gremlin> graph.traversal().V().hasLabel("User").as("a").
         V().hasLabel("Song").
         addE("edge to song").from("a");
==>e[8][0-edge to song->2]
==>e[9][0-edge to song->4]
==>e[10][0-edge to song->6]

This 显示了另一个在遍历中使用 addE 作为副作用的示例。

试试这个

gremlin> songs = g.V().has("album","albumname")).toList();user = g.V().has('fullName','arunkumar').next(); songs.each{user.addEdge("in",it)} 

gremlin> g.E() //check the edge

希望这对您有所帮助:)

我遇到了类似的问题。使用 C# SDK,我是这样做的:

g.V('4100')
.addE('knows').to(g.V('4200')).outV()
.addE('knows').to(g.V('4355')).outV()
.addE('knows').to(g.V('4676'))