How to add multiple edges in a single gremlin query?

流过昼夜 提交于 2019-12-11 06:37:01

问题


My scenario is to add multiple edges between vertices in a single query:

Assume the nodes below: These are the labels and ids I have

Users:

4100

Songs:

4200

4355

4676

I have to establish edges between these vertices

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

We can do it normally by creating single edge between node.it is not a efficient method if we want to create edge between more than 50 vertices at a time. I am using Tinkerpop 3.0.1.


回答1:


If you have the vertex ids, it is very efficient to lookup by id. If you are using Gremlin Server, each request to the Gremlin Server is treated as a single transaction. You can pass the multiple statements in a Gremlin query on a single request (with bindings) rather than sending multiple requests. Separate the statements in the Gremlin query with semicolons.

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



回答2:


Using the latest Tinkerpop. You could do the following:

Create a sample graph:

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]

Now add the edges in a single traversal:

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 shows another example of using addE within a traversal as a side effect.




回答3:


I had a similar problem. With the C# SDK I do it like this:

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



回答4:


Try this

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

Hope this helps :)



来源:https://stackoverflow.com/questions/38185325/how-to-add-multiple-edges-in-a-single-gremlin-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!