Reputation: 89
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
.
Upvotes: 3
Views: 9361
Reputation: 161
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'))
Upvotes: 9
Reputation: 6792
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()) }
Upvotes: 2
Reputation: 27
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 :)
Upvotes: -2
Reputation: 3565
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.
Upvotes: 8