Sunday, 17 May 2026

Converting Multiple Traversers into a Single List Using Gremlin fold()

  

In Gremlin, traversals typically return one result per traverser. While this streaming behavior is powerful, real-world applications—such as APIs and dashboards—often require aggregated and structured outputs instead of multiple rows.

 

The fold() step helps solve this by collecting multiple traversal results into a single list. In this post, we’ll understand fold() using a simple graph that models authors and their books.

 

Sample Domain: Authors & Books

Let’s model:

·      Author vertices

·      Book vertices

·      WROTE edges from Author Book

 

Sample Data Setup

graph = TinkerGraph.open()
g = graph.traversal()

// Indian Authors
author1 = graph.addVertex(label, 'author', 'name', 'Rabindranath Tagore')
author2 = graph.addVertex(label, 'author', 'name', 'R. K. Narayan')

// Books
book1 = graph.addVertex(label, 'book', 'title', 'Gitanjali')
book2 = graph.addVertex(label, 'book', 'title', 'The Home and the World')
book3 = graph.addVertex(label, 'book', 'title', 'Malgudi Days')

// Relationships
author1.addEdge('wrote', book1)
author1.addEdge('wrote', book2)
author2.addEdge('wrote', book3)

gremlin> g.V().valueMap(true)
==>[id:0,label:author,name:[Rabindranath Tagore]]
==>[id:2,label:author,name:[R. K. Narayan]]
==>[id:4,label:book,title:[Gitanjali]]
==>[id:6,label:book,title:[The Home and the World]]
==>[id:8,label:book,title:[Malgudi Days]]
gremlin> 
gremlin> 
gremlin> g.E().valueMap(true)
==>[id:10,label:wrote]
==>[id:11,label:wrote]

gremlin> g.V().valueMap(true)
==>[id:0,label:author,name:[Rabindranath Tagore]]
==>[id:2,label:author,name:[R. K. Narayan]]
==>[id:4,label:book,title:[Gitanjali]]
==>[id:6,label:book,title:[The Home and the World]]
==>[id:8,label:book,title:[Malgudi Days]]
gremlin> 
gremlin> 
gremlin> g.E().valueMap(true)
==>[id:10,label:wrote]
==>[id:11,label:wrote]

   

Example 1: Fetch all books written by Rabindranath Tagore

 

Without fold method

 

g.V().
  has('author', 'name', 'Rabindranath Tagore').
  out('wrote').
  valueMap(true)

gremlin> g.V().
......1>   has('author', 'name', 'Rabindranath Tagore').
......2>   out('wrote').
......3>   valueMap(true)
==>[id:4,label:book,title:[Gitanjali]]
==>[id:6,label:book,title:[The Home and the World]]

Above example return each traversal as separate entry. Let's do the same with fold step.  

g.V().
  has('author', 'name', 'Rabindranath Tagore').
  out('wrote').
  valueMap(true).
  fold()

gremlin> g.V().
......1>   has('author', 'name', 'Rabindranath Tagore').
......2>   out('wrote').
......3>   valueMap(true).
......4>   fold()
==>[[id:4,label:book,title:[Gitanjali]],[id:6,label:book,title:[The Home and the World]]]

   

We can even replace out('wrote') with outE('wrote').inV().

 

g.V().
  has('author', 'name', 'Rabindranath Tagore').
  outE('wrote').
  inV().
  valueMap(true).
  fold()

gremlin> g.V().
......1>   has('author', 'name', 'Rabindranath Tagore').
......2>   outE('wrote').
......3>   inV().
......4>   valueMap(true).
......5>   fold()
==>[[id:4,label:book,title:[Gitanjali]],[id:6,label:book,title:[The Home and the World]]]

   

What Exactly Does fold() Do?

Internally, fold():

·      Waits for all incoming traversers

·      Combines their values into one list

·      Emits a single traverser

 

fold() vs unfold()

Step

Purpose

fold()

Multiple results Single list

unfold()

List Individual results     

 

 

g.V().
  has('author', 'name', 'Rabindranath Tagore').
  out('wrote').
  valueMap(true).
  fold().
  unfold()

gremlin> g.V().
......1>   has('author', 'name', 'Rabindranath Tagore').
......2>   out('wrote').
......3>   valueMap(true).
......4>   fold().
......5>   unfold()
==>[id:4,label:book,title:[Gitanjali]]
==>[id:6,label:book,title:[The Home and the World]]

Use fold() when you want Gremlin to stop streaming results and instead return a single, structured collection.

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment