Monday, 13 April 2026

Recording Gremlin Console Sessions to a File

During interactive exploration, debugging, or teaching, it is often useful to preserve a complete record of a Gremlin Console session. Recording allows commands and their corresponding results to be written to a file as they are executed. This is particularly valuable for:

 

·      Auditing or reviewing exploratory queries

·      Sharing reproducible examples with colleagues or students

·      Capturing output for documentation or reports

·      Retaining evidence of graph state during troubleshooting

 

The Gremlin Console provides built-in support for this workflow through the :record command.

 

The :record Command

Session recording is controlled entirely from within the console using the :record command. Recording can be started and stopped explicitly, allowing precise control over which portions of a session are captured.

 

The general form is:

 

·      :record start <file-path> - begin recording all subsequent commands and output

·      :record stop — end the recording and close the file

 

While recording is active, every Gremlin command entered and every result produced by the console is written to the specified file.

 

The following example demonstrates how to record a short Gremlin Console session to a file named mylog.txt.

 

Execute following statement from Gremlin console.

:record start /Users/Shared/mylog.txt

gremlin> :record start /Users/Shared/mylog.txt
Recording session to: "/Users/Shared/mylog.txt"
==>/Users/Shared/mylog.txt

   

From this point onward, all commands and results are captured until recording is stopped.

 

Let's create a Graph and Graph traversal instance by executing below statements.

 

graph = TinkerFactory.createModern()
g = traversal().with(graph)

gremlin> graph = TinkerFactory.createModern()
==>tinkergraph[vertices:6 edges:6]
gremlin> g = traversal().with(graph)
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]

The traversal source, typically named g, serves as the entry point for all Gremlin queries against the graph.

 

With recording enabled, traversals and their results are written to the log file automatically.

 

The following traversal counts the total number of vertices in the graph:

gremlin> g.V().count().next()
==>6

   

Here, V() selects all vertices, count() computes the total, and next() retrieves the scalar result from the traversal. The next traversal retrieves a detailed view of each vertex, including identifiers, labels, and properties:

 

gremlin> g.V().valueMap(true)
==>[id:1,label:person,name:[marko],age:[29]]
==>[id:2,label:person,name:[vadas],age:[27]]
==>[id:3,label:software,name:[lop],lang:[java]]
==>[id:4,label:person,name:[josh],age:[32]]
==>[id:5,label:software,name:[ripple],lang:[java]]
==>[id:6,label:person,name:[peter],age:[35]]

The valueMap(true) step includes both element identifiers and labels, making it useful for inspection and debugging. A detailed explanation of Gremlin traversals and steps follows in subsequent sections of this tutorial.

 

Stopping the Recording

Once the relevant portion of the session has been captured, recording is stopped explicitly by exeucting below command.

:record stop

gremlin> :record stop
Recording stopped; session saved as: "/Users/Shared/mylog.txt" (435 bytes)
==>/Users/Shared/mylog.txt

At this point, the file is closed and no further console activity is written to it.

 

Inspecting the Recorded Output

Opening the generated mylog.txt file reveals a structured, chronological record of the session.

$cat /Users/Shared/mylog.txt 
// OPENED: Fri Jan 09 14:14:12 IST 2026
// RESULT: /Users/Shared/mylog.txt
graph = TinkerFactory.createModern()
// RESULT: tinkergraph[vertices:6 edges:6]
g = traversal().with(graph)
// RESULT: graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
g.V().count().next()
// RESULT: 6
g.V().valueMap(true)
// RESULT: [TinkerGraphStep(vertex,[]), PropertyMapStep(null,value)]
:record stop
// CLOSED: Fri Jan 09 14:17:41 IST 2026

Each command is followed by a // RESULT: line containing the console’s response. The file also includes timestamps marking when recording began and ended, providing useful contextual information for later review.

 

When using session recording, keep the following points in mind:

 

·      Recording captures both commands and results, not just output

·      Absolute or relative file paths may be used, depending on the environment

·      Sensitive data should be handled carefully when sharing recorded files

·      Recorded sessions can serve as executable documentation, but may require minor cleanup for presentation purposes

 

Used thoughtfully, the :record command transforms the Gremlin Console into a powerful tool for reproducibility, learning, and collaboration.

 


 

Previous                                                    Next                                                    Home

Determining the Installed Apache TinkerPop Version

Knowing the exact version of Apache TinkerPop and the Gremlin Console in use is a fundamental operational concern. Version differences can affect:

 

·      Available Gremlin steps and traversal semantics

·      Behavior of drivers, plugins, and serialization formats

·      Compatibility with graph providers and remote servers

·      Reproducibility of examples and troubleshooting outcomes

 

For these reasons, checking the running version should be one of the first diagnostic steps when working with Gremlin, especially in shared environments or across multiple machines.

 

Checking the Version from Inside the Gremlin Console

The most direct way to determine the version is from within an active Gremlin Console session. The console exposes version information through the Gremlin utility class:

Gremlin.version()

gremlin> Gremlin.version()
==>3.8.0

This value represents:

·      The version of the Apache TinkerPop distribution

·      The version of the Gremlin traversal engine

·      The version of the Gremlin Console runtime

 

This call returns the version of the Apache TinkerPop distribution that the console is currently using. It reflects the runtime environment, not merely the version of a script or configuration file. This distinction is important when diagnosing issues caused by mismatched installations or stale binaries.

 

Conceptually, this method answers the question “Which TinkerPop runtime is executing my traversals right now?”

 

Checking the Version from the Command Line

When the console is not yet running or when scripting and automation are involved the version can be retrieved directly from the command line using the Gremlin startup script.

 

Two equivalent options are available:

·      gremlin.sh -v

·      gremlin.sh --version

 

$gremlin.sh -v
gremlin 3.8.0
$
$gremlin.sh --version
gremlin 3.8.0

   

Both commands print the version and exit immediately without starting an interactive console session. This approach is particularly useful in:

 

·      Shell scripts and CI/CD pipelines

·      Server provisioning and validation steps

·      Environments with multiple Gremlin installations

 

In effect, this method answers a slightly different question: “Which Gremlin distribution will be launched if this script is executed?”

 

As a best practice:

·      Use Gremlin.version() when already inside the console and validating runtime behavior

·      Use gremlin.sh -v or --version when validating installations, scripts, or deployment environments

 

Maintaining clarity about the exact TinkerPop version in use ensures consistency, prevents subtle bugs, and makes collaboration and troubleshooting significantly more predictable.

 

References

https://tinkerpop.apache.org/javadocs/current/core/org/apache/tinkerpop/gremlin/util/Gremlin.html#version()

 

 

Previous                                                    Next                                                    Home

Monday, 6 April 2026

Discovering Gremlin Command Line Options via --help

The Gremlin Console provides a rich set of command-line options that control how the console starts, executes scripts, and reports output. These options are especially useful when integrating Gremlin into automation workflows, debugging scripts, or tailoring the console experience for different environments.

 

To view all supported command-line flags, invoke the console with the --help option:

gremlin.sh --help

$gremlin.sh --help

Usage: gremlin.sh [-CDhlQvV] [-e=<SCRIPT ARG1 ARG2 ...>]... [-i=<SCRIPT ARG1 
                  ARG2 ...>...]...
  -C, --color     Disable use of ANSI colors
  -D, --debug     Enabled debug Console output
  -e, --execute=<SCRIPT ARG1 ARG2 ...>
                  Execute the specified script (SCRIPT ARG1 ARG2 ...) and close
                    the console on completion
  -h, --help      Display this help message
  -i, --interactive=<SCRIPT ARG1 ARG2 ...>...
                  Execute the specified script and leave the console open on
                    completion
  -l              Set the logging level of components that use standard logging
                    output independent of the Console
  -Q, --quiet     Suppress superfluous Console output
  -v, --version   Display the version
  -V, --verbose   Enable verbose Console output

This command prints a usage summary along with a detailed description of each available option.

 

Understanding these command-line flags significantly expands how the Gremlin Console can be used:

 

·      As an interactive exploration tool for learning and experimentation

·      As a script execution engine for automation and testing

·      As a diagnostic utility when investigating configuration or runtime issues

 

In practice, the Gremlin Console is not just a shell for typing traversals, it is a flexible execution environment whose behavior can be precisely controlled from the command line. Mastery of these options enables more efficient workflows and smoother integration with larger data and graph processing systems.

 

Previous                                                    Next                                                    Home

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

Sunday, 5 April 2026

The Gremlin Console

The Gremlin Console is an interactive REPL (Read–Eval–Print Loop) environment designed for exploring and executing Gremlin traversals. It is one of the most approachable entry points into the Apache TinkerPop ecosystem and serves as an essential tool for learning, experimentation, debugging, and rapid prototyping.

 

Technically, the console is built on top of the Groovy Console. As a result, developers familiar with interactive shells such as those provided by Scala, Python, or Ruby will find the experience immediately familiar. Commands are entered interactively, evaluated on the fly, and results are printed directly to the terminal.

 

What makes the Gremlin Console particularly valuable is its low setup cost and low barrier to entry. A fully functional environment can be available within minutes, making it ideal for developers who want to start working with graph data without first building an application or configuring a server.

 

1. Local and Remote Graphs

The Gremlin Console is capable of interacting with both:

 

·      Local graphs, embedded directly within the console process

·      Remote graphs, accessed via a Gremlin Server connection

 

Although both modes are important in real-world systems, this post and upcoming tutorials deliberately focuses on local graphs for most examples. Local graphs reduce conceptual overhead and allow attention to remain on Gremlin syntax, traversal semantics, and graph modeling concepts rather than on deployment and networking concerns. Remote execution and Gremlin Server integration are addressed later, once a solid foundation has been established.

 

2. Downloading the Gremlin Console

To begin, download the Gremlin Console binary distribution from the official Apache TinkerPop website. Apache provides a geographically distributed set of mirrors, the following link will automatically redirect to a nearby mirror:

 

https://www.apache.org/dyn/closer.lua/tinkerpop/3.8.0/apache-tinkerpop-gremlin-console-3.8.0-bin.zip

 

This archive contains everything required to run the console, including all necessary dependencies.

 

3. Installing and Launching the Console

After downloading the binary archive, extract its contents and start the console from the command line.

$ unzip apache-tinkerpop-gremlin-console-3.8.0-bin.zip
$ cd apache-tinkerpop-gremlin-console-3.8.0
$ bin/gremlin.sh

On startup, the console displays a banner followed by a prompt similar to the following:

         \,,,/
         (o o)
-----oOOo-(3)-oOOo-----
plugin activated: tinkerpop.server
plugin activated: tinkerpop.utilities
plugin activated: tinkerpop.tinkergraph
gremlin>

   

Several important details are visible here:

·      The banner confirms that the console has started successfully.

·      A number of plugins are automatically activated.

·      The gremlin> prompt indicates that the environment is ready to accept input.

 

By default, the console enables plugins such as:

·      tinkergraph – an in-memory reference graph implementation

·      server – support for remote connections

·      utilities – helper functions and conveniences

 

These defaults make it possible to begin executing Gremlin traversals immediately, without additional configuration.

 

4. The Console Command Model

The Gremlin Console supports two broad categories of input:

 

·      Gremlin and Groovy expressions

·      Console commands

 

To clearly distinguish console specific commands from Gremlin traversals, all console commands are prefixed with a colon (:). This design avoids ambiguity and allows Gremlin syntax to remain clean and expressive.

 

For example, a Gremlin traversal might look like:

g.V().count()

Whereas a console command always begins with a colon:

:help

This separation is especially important because the console is not limited to Gremlin alone, it is a Groovy environment with additional Gremlin bindings layered on top.

 

5. Discovering Available Commands

Typing :help displays a complete list of commands supported by the console:

gremlin> :help

For information about Groovy, visit:
    http://groovy-lang.org 

Available commands:
  :help       (:h  ) Display this help message
  ?           (:?  ) Alias to: :help
  :exit       (:x  ) Exit the shell
  :quit       (:q  ) Alias to: :exit
  import      (:i  ) Import a class into the namespace
  :display    (:d  ) Display the current buffer
  :clear      (:c  ) Clear the buffer and reset the prompt counter
  :show       (:S  ) Show variables, classes or imports
  :inspect    (:n  ) Inspect a variable or the last result with the GUI object browser
  :purge      (:p  ) Purge variables, classes, imports or preferences
  :edit       (:e  ) Edit the current buffer
  :load       (:l  ) Load a file or URL into the buffer
  .           (:.  ) Alias to: :load
  :save       (:s  ) Save the current buffer to a file
  :record     (:r  ) Record the current session to a file
  :history    (:H  ) Display, manage and recall edit-line history
  :alias      (:a  ) Create an alias
  :grab       (:g  ) Add a dependency to the shell environment
  :register   (:rc ) Register a new command with the shell
  :doc        (:D  ) Open a browser window displaying the doc for the argument
  :set        (:=  ) Set (or list) preferences
  :uninstall  (:-  ) Uninstall a Maven library and its dependencies from the Gremlin Console
  :install    (:+  ) Install a Maven library and its dependencies into the Gremlin Console
  :plugin     (:pin) Manage plugins for the Console
  :remote     (:rem) Define a remote connection
  :submit     (:>  ) Send a Gremlin script to Gremlin Server
  :bytecode   (:bc ) Gremlin bytecode helper commands
  :cls        (:C  ) Clear the screen.

For help on a specific command type:
    :help command 

The help output includes both the command names and their aliases, along with concise descriptions. Commands cover a wide range of functionality, including:

 

·      Session management

·      Script loading and saving

·      Dependency management

·      Plugin control

·      Remote server interaction

 

This command set allows the console to function not merely as a query shell, but as a lightweight development and exploration environment.

 

Command-Specific Help

Detailed help for any individual command can be obtained by appending the command name to :help.

:help command

This provides usage details, options, and expected behavior, which is particularly useful when working with less frequently used commands such as remote connections or bytecode inspection.

gremlin> :help :load

usage: :load (<file|url>)+

Load one or more files (or urls) into the buffer.

gremlin>

6. Relationship to Groovy

Because the Gremlin Console is Groovy based, it inherits the expressive power of the Groovy language. Variables, control structures, functions, and imports all behave as expected in a Groovy shell. This allows:

 

·      Storing traversal results in variables

·      Writing small helper functions

·      Executing multi-line scripts

·      Loading reusable logic from files

 

At the same time, Gremlin remains the primary abstraction for graph traversal. Groovy acts as the host language, while Gremlin provides the domain specific language for querying and manipulating graph structures.

gremlin> def add(int a, int b) {
......1>     return a + b
......2> }
==>true
gremlin> 
gremlin> add(10, 20)
==>30

   

For learning and exploration, the Gremlin Console plays a role similar to a database shell in the relational world. It enables:

 

·      Rapid experimentation with graph models

·      Interactive traversal development

·      Immediate feedback during learning

·      Debugging complex traversal logic

 

Throughout this tutorial series, the Gremlin Console is used as the primary environment for examples and exercises. Mastery of the console leads directly to confidence with Gremlin itself, whether the ultimate target is an embedded graph, a remote Gremlin Server, or a production-grade graph database.

 

 


Previous                                                    Next                                                    Home