TinkerGraph is the reference, in-memory graph implementation provided by Apache TinkerPop. It is lightweight, fast to start, and requires no external dependencies, making it ideal for learning Gremlin, experimenting with traversals, and prototyping graph based solutions.
This post explains how TinkerGraph is enabled and used within the Gremlin Console, how to initialize a graph instance, and how to prepare the traversal infrastructure required to execute Gremlin queries. Along the way, it establishes conventions that will be used consistently throughout this tutorial series.
1. TinkerGraph Support in the Gremlin Console
When the Gremlin Console is launched, support for TinkerGraph is typically enabled by default. Starting the console using the gremlin.sh script produces output similar to the following.
$ gremlin.sh \,,,/ (o o) -----oOOo-(3)-oOOo----- plugin activated: tinkerpop.server plugin activated: tinkerpop.utilities plugin activated: tinkerpop.tinkergraph gremlin>
The presence of plugin activated: tinkerpop.tinkergraph confirms that the TinkerGraph plugin is already available in the console session.
Enabling the TinkerGraph Plugin Manually
In rare cases such as after a customized console setup or plugin changes, the TinkerGraph plugin may not be active. If this happens, it can be enabled explicitly using the following command:
:plugin use tinkerpop.tinkergraph
Once executed, the console confirms activation.
gremlin> :plugin use tinkerpop.tinkergraph ==>tinkerpop.tinkergraph activated
After enabling the plugin manually, it is recommended to close and restart the Gremlin Console. Restarting ensures that all classes and bindings associated with TinkerGraph are properly loaded and available for use.
2. Creating a TinkerGraph Instance
With the plugin enabled and the console restarted, a new in-memory graph instance can be created directly from the console.
graph = TinkerGraph.open() gremlin> graph = TinkerGraph.open() ==>tinkergraph[vertices:0 edges:0]
This call creates an empty TinkerGraph instance using default configuration settings. At this stage:
· The graph resides entirely in memory.
· All data is lost when the console session ends unless explicitly persisted.
· The graph is suitable for interactive exploration and learning.
In more advanced scenarios, parameters can be passed to the open() method to control aspects such as persistence, indexing, or custom configuration. These options will be explored in later posts.
3. Establishing a Graph Traversal Source
Creating a graph instance alone is not sufficient to run Gremlin queries. Gremlin traversals are executed through a graph traversal source, which acts as the entry point for all traversal operations. A traversal source is created as follows:
g = graph.traversal() gremlin> g = graph.traversal() ==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
The variable g now represents a traversal source bound to the graph instance. All Gremlin queries begin from this object. Conceptually:
graph represents the data structure and storage.
g represents the query context and execution engine for traversals.
4. Creating a Simple Graph
With the graph and traversal source in place, it is now possible to create a simple graph and run basic traversals.
4.1 Adding Vertices
The following commands add two vertices representing people:
v1 = g.addV('person').property('name', 'Alice').next() v2 = g.addV('person').property('name', 'Bob').next()
Each vertex is labeled person and has a name property.
gremlin> v1 = g.addV('person').property('name', 'Alice').next() ==>v[0] gremlin> v2 = g.addV('person').property('name', 'Bob').next() ==>v[2] gremlin> gremlin> graph ==>tinkergraph[vertices:2 edges:0] gremlin> gremlin> g ==>graphtraversalsource[tinkergraph[vertices:2 edges:0], standard]
4.2 Adding an Edge
Next, an edge is created to represent a relationship between Alice and Bob.
g.V(v1).addE('knows').to(v2).next()
gremlin> g.V(v1).addE('knows').to(v2).next() ==>e[4][0-knows->2]
This edge indicates that Alice “knows” Bob.
4.3 Running Basic Traversals
Once the graph is populated, simple traversals can be executed to explore it.
Retrieve all vertices:
g.V()
gremlin> g.V() ==>v[0] ==>v[2]
Retrieve all person names:
g.V().values('name')
gremlin> g.V().values('name') ==>Alice ==>Bob
Find who Alice knows:
g.V().has('name', 'Alice').out('knows').values('name')
gremlin> g.V().has('name', 'Alice').out('knows').values('name') ==>Bob
These examples illustrate a fundamental idea in Gremlin, traversals describe how to walk the graph, step by step, rather than declaring what data to fetch in a purely declarative manner.
Understanding this flow early makes it significantly easier to reason about more advanced topics such as schema design, traversal optimization, graph computer usage, and production deployments.
Previous Next Home
No comments:
Post a Comment