Search code examples
titangremlinrexster

How to loop over verticies in Gremlin and adding edge


I'm trying to figure out how to loop over verticies within a container and adding edges between the current vertice in the loop and a given vertice.

This is what I have so far: This gives me an array of all vertice id's within the given container:

v=g.V('containerName','MyContainer').outE.inV.id

result:

{"results":[12,320004,280004,240004,200004,160004,120004,80004,40004],"success":true,"version":"2.4.0","queryTime":35.089565} 

Now I want to loop/iterate over all the id's in this array, get the vertice of the id and add an edge between a given vertice and the current vertice in the loop.

Anyone that knows how this can be done?


Solution

  • You can make a sideEffect that adds the new edges.

    With the toy tinker graph:

    g = TinkerGraphFactory.createTinkerGraph()
    

    let's assume you want to link peter to all neighbors of marko (out only)

    givenVertex = g.V('name','peter').next()
    g.V('name','marko').out.sideEffect{g.addEdge(givenVertex,it, 'yourLabel')}
    

    verify result:

    g.V('name','peter').out.name
    ==>lop // lop appears twice because edge peter -> lop was present previous to operation
    ==>lop
    ==>vadas
    ==>josh