我的场景是在单个查询中的顶点之间添加多条边:
假设以下节点:这些是我拥有的标签和 ID
用户:
4100
歌曲:
4200
4355
4676
我必须在这些顶点之间建立边
4100 --> 4200,
4100 --> 4355,
4100 --> 4676.
我们可以通过在节点之间创建一条边来正常地做到这一点。如果我们想一次在超过 50 个顶点之间创建边,这不是一种有效的方法。我正在使用 Tinkerpop 3.0.1
。
我的场景是在单个查询中的顶点之间添加多条边:
假设以下节点:这些是我拥有的标签和 ID
用户:
4100
歌曲:
4200
4355
4676
我必须在这些顶点之间建立边
4100 --> 4200,
4100 --> 4355,
4100 --> 4676.
我们可以通过在节点之间创建一条边来正常地做到这一点。如果我们想一次在超过 50 个顶点之间创建边,这不是一种有效的方法。我正在使用 Tinkerpop 3.0.1
。
使用最新的 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]
这addE
显示了在遍历中使用作为副作用的另一个示例。
我有一个类似的问题。使用 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'))
如果您有顶点 id,则通过 id 查找非常有效。如果您使用 Gremlin Server,则对 Gremlin Server 的每个请求都被视为单个事务。您可以在单个请求(使用绑定)上传递 Gremlin 查询中的多个语句,而不是发送多个请求。用分号分隔 Gremlin 查询中的语句。
l=[4200, 4355, 4676]; v=graph.vertices(4100).next(); l.each { v.addEdge("knows", graph.vertices(it).next()) }
试试这个
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
希望这可以帮助 :)