3

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.

Filipe Teixeira
  • 3,565
  • 1
  • 25
  • 45
mano haran
  • 79
  • 2
  • 7

4 Answers4

9

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'))
flq
  • 22,247
  • 8
  • 55
  • 77
Fabian
  • 161
  • 2
  • 4
8

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.

Filipe Teixeira
  • 3,565
  • 1
  • 25
  • 45
  • I don't think this approach will work with `3.0.1` as the second `V()` is not supported in that version, I think it is only supported from `3.1.x` onwards. If you are stuck with `3.0.1` I think you will need to do two traversals. One to get the ids and the second to build the edges as suggested by Jason in the other answer. – Filipe Teixeira Jul 04 '16 at 14:56
2

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()) }
Jason Plurad
  • 6,682
  • 2
  • 18
  • 37
  • The above query didn't work can you suggest for gremlin 3.0.1 – mano haran Jul 05 '16 at 05:42
  • I forgot the `next()` call. Updated my answer. Vertex lookup by id should be faster than an indexed lookup. It isn't clear from your question whether the ids you are talking about are the actual vertex ids or some other indexed property (which the other answers are assuming). – Jason Plurad Jul 05 '16 at 20:09
  • New to gremlin. I'm assuming "it" a reserved word. Hard to search for "it" on google. Can someone point me in the direction on some documentation on the use of each and "it" – Milk Man Aug 23 '18 at 02:13
  • `it` is the implicit parameter for the `each` closure. Those are both Groovy language constructs. `each` is for [iterating a list](http://docs.groovy-lang.org/next/html/documentation/working-with-collections.html#_iterating_on_a_list) where `it` is the current element. – Jason Plurad Aug 23 '18 at 12:14
-2

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 :)