Search code examples
gremlintinkerpop3

How to add multiple edges in a single gremlin query?


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.


Solution

  • 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()) }