I need to assign a unique, incrementing value to all Vertices in my graph. In SQL I'd use ROW_NUMBER
. Is there a simple way in Gremlin? I'm hoping for something like this:
g.V().<some_step>()
Output:
1
2
3
...
I don't think I would try this on a large graph but for a modest sized graph you could try using the index
step. Below is an example that uses the air-routes data set.
The index
adds an incrementing index value to a list.
gremlin> g.V().limit(5).fold().index().unfold()
==>[v[0],0]
==>[v[1],1]
==>[v[2],2]
==>[v[3],3]
==>[v[4],4]
Using local
scope you can access members of that list.
gremlin> g.V().limit(5).fold().index().unfold().limit(local,1)
==>v[0]
==>v[1]
==>v[2]
==>v[3]
==>v[4]
Using those techniques we could use the index value to add a new property to each airport. I just did the first 5 in this example.
gremlin> g.V().limit(5).
fold().index().unfold().as('list').
limit(local,1).
property('index',select('list').tail(local))
==>v[0]
==>v[1]
==>v[2]
==>v[3]
==>v[4]
And check the results.
gremlin> g.V(1).valueMap()
==>[country:[US],code:[ATL],longest:[12390],city:[Atlanta],index:[1],lon:[-84.4281005859375],type:[airport],elev:[1026
],icao:[KATL],region:[US-GA],runways:[5],lat:[33.6366996765137],desc:[Hartsfield - Jackson Atlanta International Airpo
rt]]
gremlin> g.V(2).valueMap()
==>[country:[US],code:[ANC],longest:[12400],city:[Anchorage],index:[2],lon:[-149.996002197266],type:[airport],elev:[15
1],icao:[PANC],region:[US-AK],runways:[3],lat:[61.1744003295898],desc:[Anchorage Ted Stevens]]