Monday, 6 April 2026

Running Groovy Scripts with the Gremlin Command Line Tool

 The Gremlin Console is most commonly used in interactive mode, where queries are typed and evaluated one at a time. This mode is ideal for exploration, learning, and ad-hoc analysis. However, the console is not limited to interactive usage. It can also execute prewritten Groovy scripts, making it a practical tool for automation, repeatable experiments, and scripted graph operations.

 

Apache TinkerPop provides built-in support for executing Groovy files directly from the command line through the Gremlin Console.

 

Executing a Script and Exiting the Console

To run a Groovy script non-interactively, pass the script file to the Gremlin Console using the -e (execute) option. When invoked this way, the console evaluates the script and then terminates immediately after execution.

 

For example, given a Groovy script named myscript.groovy, it can be executed as follows:

gremlin.sh -e myscript.groovy

In this mode:

 

·      The script is executed in the same environment as an interactive console session.

·      All standard Gremlin objects (such as graph, g, and imports provided by the console) are available.

·      Once the script finishes running, the Gremlin Console exits automatically.

 

myscript.groovy

// ---------------------------------------------
// myscript.groovy
// Simple Gremlin script: graph creation + stats
// ---------------------------------------------

// Create an in-memory TinkerGraph
graph = TinkerGraph.open()
g = graph.traversal()

println "Graph initialized"

// ---------------------------------------------
// Add vertices
// ---------------------------------------------
vHari     = graph.addVertex(label, 'person', 'name', 'Hari',     'age', 30)
vKrishna  = graph.addVertex(label, 'person', 'name', 'Krishna',  'age', 28)
vRam      = graph.addVertex(label, 'person', 'name', 'Ram',      'age', 35)
vSita     = graph.addVertex(label, 'person', 'name', 'Sita',     'age', 32)

println "Vertices added"

// ---------------------------------------------
// Add edges
// ---------------------------------------------
vHari.addEdge('knows', vKrishna, 'since', 2020)
vKrishna.addEdge('knows', vRam, 'since', 2019)
vRam.addEdge('knows', vSita, 'since', 2018)
vHari.addEdge('knows', vSita, 'since', 2021)

println "Edges added"

// ---------------------------------------------
// Print basic graph statistics
// ---------------------------------------------
println "---------------------------------"
println "Graph Statistics"
println "---------------------------------"

println "Total vertices      : " + g.V().count().next()
println "Total edges         : " + g.E().count().next()

println "Person vertices     : " + g.V().hasLabel('person').count().next()
println "Knows relationships : " + g.E().hasLabel('knows').count().next()

println "Average age         : " + g.V().hasLabel('person').values('age').mean().next()

println "---------------------------------"

// ---------------------------------------------
// Print per-vertex degree information
// ---------------------------------------------
println "Vertex Degree Details"
println "---------------------------------"

g.V().hasLabel('person').forEachRemaining { v ->
    def name = v.value('name')
    def outDegree = g.V(v).outE().count().next()
    def inDegree  = g.V(v).inE().count().next()

    println "${name} -> outDegree=${outDegree}, inDegree=${inDegree}"
}

println "---------------------------------"
println "Script execution completed"

 Run the Script.

$gremlin.sh -e myscript.groovy
Graph initialized
Vertices added
Edges added
---------------------------------
Graph Statistics
---------------------------------
Total vertices      : 4
Total edges         : 4
Person vertices     : 4
Knows relationships : 4
Average age         : 31.25
---------------------------------
Vertex Degree Details
---------------------------------
Hari -> outDegree=2, inDegree=0
Krishna -> outDegree=1, inDegree=1
Ram -> outDegree=1, inDegree=1
Sita -> outDegree=0, inDegree=2
---------------------------------
Script execution completed

   

This approach is well suited for:

·      Batch processing of graph mutations

·      Automated data loading or cleanup tasks

·      Running repeatable queries as part of a build or deployment pipeline

 

Conceptually, this usage mirrors running a SQL script against a database from the command line, the script is executed once, produces its effects or results, and the process ends.

 

Executing a Script and Staying in Interactive Mode

In some cases, it is useful to run a script to initialize state and then continue working interactively. For example, a script may load a graph, define helper functions, or configure traversal sources.

 

For this purpose, the Gremlin Console provides the -i (initialize) option. When a script is supplied with -i, the console executes the script and then remains open for further interactive commands.

$gremlin.sh -i myscript.groovy

         \,,,/
         (o o)
-----oOOo-(3)-oOOo-----
plugin activated: tinkerpop.server
plugin activated: tinkerpop.utilities
plugin activated: tinkerpop.tinkergraph
Graph initialized
Vertices added
Edges added
---------------------------------
Graph Statistics
---------------------------------
Total vertices      : 4
Total edges         : 4
Person vertices     : 4
Knows relationships : 4
Average age         : 31.25
---------------------------------
Vertex Degree Details
---------------------------------
Hari -> outDegree=2, inDegree=0
Krishna -> outDegree=1, inDegree=1
Ram -> outDegree=1, inDegree=1
Sita -> outDegree=0, inDegree=2
---------------------------------
Script execution completed
gremlin>

   

With this option:

 

·      The script is executed as part of the console startup sequence.

·      Any variables, functions, or traversal sources defined in the script remain available.

·      The console does not exit, allowing immediate continuation in interactive mode.

 

This pattern is commonly used for:

 

·      Preloading sample datasets

·      Setting up remote connections or traversal aliases

·      Defining reusable Groovy functions for complex traversals

 

Choosing Between -e and -i

The distinction between -e and -i reflects two different usage styles:

 

·      Use -e when the script is the entire task and no further interaction is required.

·      Use -i when the script prepares the environment for an interactive session.

 

Both options enable a smooth transition between scripted automation and exploratory graph work, reinforcing the Gremlin Console’s role as both a development and operational tool within the Apache TinkerPop ecosystem.

 


Previous                                                    Next                                                    Home

No comments:

Post a Comment