Skip to main content

Gremlin Tricks: Replicate a Table View

This guide explains how to replicate the table view in a Gremlin query, while showing off some querying tricks along the way

Written by Kristine Marhilevica

The Tables view allows for a familiar tabular look at the contents of a workspace, and also lets you select some relational columns like "path", "parent", and incoming/outgoing references. However, things get complicated if you want a gremlin query to produce a similar result, either to serve as a starting point or in order to export data into an excel-file.

By following this guide, you can easily create a query that lets you emulate a table view, and then expand upon it to create more complex tabular outputs

Note: There are some limitations with gremlin to be aware of:

  • Workspace names cannot be returned using gremlin, only the workspace ID.

Reference-columns

Perhaps the most interesting line is the “Is Realized By” column that show referenced components:

by(__.in('Is Realized By').values('name').fold().map{it.get().join(', ')})

This creates a comma-separated list of referenced components (by name). Note that the incoming-function starts with “__.in”. This is because “in” is a reserved keyword in Groovy, and would cause an error if you tried to invoke it in a “sub-traversal”.

Field columns

Next, add a line that safely returns a field value even if the field is missing:

by(coalesce(values('lifecycle_phase'), constant(null))).

Tags

You can also include tags.

by(values('tags').fold().map{it.get().join(', ')}).

Putting It All Together

g.V().
hasLabel('Application').
project('Name', 'Tags', 'Lifecycle Phase', 'Is Realized By').
by('name').
by(values('tags').fold().map{it.get().join(', ')}).
by(coalesce(values('lifecycle_phase'), constant(null))).
by(__.in('Is Realized By').values('name').fold().map{it.get().join(', ')})

What About The Reference Table View?

Replicating a reference table view is also pretty straight forward, but does require some modifications.

First, instead of selecting components (vertices), we instead select references (edges):

g.E()...

Then introduce outV() & inV() for selecting the incoming and outgoing component:

by(outV().values('name')).
by(inV().values('name')).

Full query:

g.E().
hasLabel('Is Realized By').
project('Source', 'Type', 'Target', 'Display Text').
by(outV().values('name')).
by(label()).
by(inV().values('name')).
by(coalesce(values('displayText'), constant(null)))

These changes now show a "reference table"-style output:

Did this answer your question?