Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select random vertices in Gremlin

Tags:

graph

gremlin

I'm still at my earlier stages with graphs and gremlin.

Is it possible to randomly select graph vertices in Gremlin?

Consider the following pipeline that gets the cars owned by a user's friend:

u.out('Friend')[0..9].out('Drives').map()

But this code is only executed against the first 10 friends every time. How can I make the selection random efficiently?

Thank you for your time :)

like image 901
riflehawk Avatar asked Dec 11 '25 13:12

riflehawk


1 Answers

In Gremlin 2.x you could use random step as in:

g.v(1).out.random()

or in 3.x random has become coin:

as in:

g.V(1).out.coin()

in 3.x, you might also look at sample and order(shuffle) steps in 3.x.

like image 147
stephen mallette Avatar answered Dec 14 '25 09:12

stephen mallette



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!