Gremlin remove all Vertex

In more recent terms as of Gremlin 2.3.0, removal of all vertices would be best accomplished with: g.V.remove() UPDATE: For version Gremlin 3.x you would use drop(): gremlin> graph = TinkerFactory.createModern() ==>tinkergraph[vertices:6 edges:6] gremlin> g = graph.traversal() ==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard] gremlin> g.V().drop().iterate() gremlin> graph ==>tinkergraph[vertices:0 edges:0] Note that drop() does not automatically iterate the Traversal … Read more

CosmosDB Graph : “upsert” query pattern

There are a number of ways to do this but I think that the TinkerPop community has generally settled on this approach: g.V().has(‘event’,’id’,’1′). fold(). coalesce(unfold(), addV(‘event’).property(‘id’,’1′)) Basically, it looks for the “event” with has() and uses fold() step to coerce to a list. The list will either be empty or have a Vertex in it. … Read more

How to perform pagination in Gremlin

From a functional perspective, a nice looking bit of Gremlin for paging would be: gremlin> g.V().hasLabel(‘person’).fold().as(‘persons’,’count’). select(‘persons’,’count’). by(range(local, 0, 2)). by(count(local)) ==>[persons:[v[1],v[2]],count:4] gremlin> g.V().hasLabel(‘person’).fold().as(‘persons’,’count’). select(‘persons’,’count’). by(range(local, 2, 4)). by(count(local)) ==>[persons:[v[4],v[6]],count:4] In this way you get the total count of vertices with the result. Unfortunately, the fold() forces you to count all the vertices which will … Read more

Gremlin – Choose one item at random

You’re pretty close to your answer: gremlin> g = TinkerGraph.open().traversal() ==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard] gremlin> g.addV(‘user’).property(‘id’,1).as(‘1’). ……1> addV(‘user’).property(‘id’,2).as(‘2’). ……2> addV(‘user’).property(‘id’,3).as(‘3’). ……3> addV(‘user’).property(‘id’,4).as(‘4’). ……4> addV(‘post’).property(‘postId’,’post1′).as(‘p1’). ……5> addV(‘post’).property(‘postId’,’post2′).as(‘p2’). ……6> addE(‘follow’).from(‘1’).to(‘2’). ……7> addE(‘follow’).from(‘1’).to(‘3’). ……8> addE(‘follow’).from(‘1’).to(‘4’). ……9> addE(‘posted’).from(‘2’).to(‘p1’). …..10> addE(‘posted’).from(‘2’).to(‘p2’). …..11> addE(‘liked’).from(‘1’).to(‘p2’). …..12> addE(‘liked’).from(‘3’).to(‘p2’). …..13> addE(‘liked’).from(‘4’).to(‘p2’).iterate() gremlin> g.V().has(‘id’,1).as(‘me’). ……1> out(‘follow’). ……2> aggregate(‘followers’). ……3> out(‘posted’). ……4> group(). ……5> by(‘postId’). ……6> by(project(‘likedBySelf’,’likedByFollowing’). … Read more