Gremlin
Updated: September 10, 2025Categories: Query
Printed from:
Gremlin Cheatsheet: Master Graph Traversals with Apache TinkerPop
1. Connection and Basic Setup
Gremlin Console
groovy
1234567// Launch Gremlin Console bin/gremlin.sh // Create a graph instance graph = TinkerGraph.open() g = graph.traversal()
Driver Connections
Python
1234567891011# Python (Gremlin-Python)
from gremlin_python.driver import client
gremlin_client = client.Client('ws://localhost:8182/gremlin', 'g')
# Java
GraphTraversalSource g = AnonymousTraversalSource.traversal().withRemote(DriverRemoteConnection.using("localhost", 8182));
# JavaScript (Node.js)
const gremlin = require('gremlin');
const client = new gremlin.driver.Client('ws://localhost:8182/gremlin');
2. Graph Structure Basics
Vertices and Edges
groovy
123456789// Create vertices g.addV('Person').property('name', 'Alice') g.addV('Company').property('name', 'TinkerCorp') // Create edges g.V().has('name', 'Alice') .addE('works_at') .to(g.V().has('name', 'TinkerCorp'))
Properties
groovy
12345678// Add/Modify properties g.V().has('name', 'Alice') .property('age', 30) .property('city', 'San Francisco') // Retrieve properties g.V().has('name', 'Alice').valueMap()
3. Gremlin Traversal Language Fundamentals
Basic Traversal Pattern
groovy
123456// Pipeline-based traversals g.V() // Start with all vertices .has('type', 'User') // Filter vertices .out('friends') // Traverse outgoing edges .values('name') // Extract name property
4. Basic Traversal Steps
Vertex and Edge Traversals
groovy
123456g.V() // All vertices g.E() // All edges g.V().out() // Outgoing neighbors g.V().in() // Incoming neighbors g.V().both() // All connected vertices
5. Filtering and Predicates
Filtering
groovy
1234567// Simple filtering g.V().has('age', gt(25)) g.V().has('name', containing('John')) // Complex predicates g.V().where(__.out('knows').count().is(gt(2)))
Predicate Examples
groovy
12345678// Comparison predicates eq() // Equal to neq() // Not equal gt() // Greater than lt() // Less than inside() outside()
6. Transformation Steps
Mapping and Selection
groovy
1234567891011// Map transformation g.V().map(values('name')) // Select specific properties g.V().select('name', 'age') // Project multiple values g.V().project('name', 'friends') .by('name') .by(out('knows').count())
7. Aggregation and Grouping
Counting and Grouping
groovy
12345678// Count elements g.V().count() g.V().group().by('type').by(count()) // Fold and reduce g.V().fold() g.V().group().by('type').by(fold())
8. Path Operations
Tracking Traversal Paths
groovy
123456// Path tracking g.V().out().path() // Complex path filtering g.V().repeat(out()).times(2).path()
9. Mutations
Graph Modifications
groovy
123456789101112// Add vertex g.addV('Person') .property('name', 'Bob') // Add edge g.V().has('name', 'Alice') .addE('knows') .to(g.V().has('name', 'Bob')) // Drop elements g.V().has('name', 'Bob').drop()
10. Subgraph and Branching
Conditional Traversals
groovy
123456789// Branch traversal g.V().branch( select('type').option('User', __.out('friends')) .option('Company', __.in('works_at')) ) // Subgraph extraction g.V().has('age', gt(25)).subgraph('adult_network')
11. Performance Optimization
Efficient Traversals
groovy
123456// Use local steps g.V().local(values('name').limit(5)) // Avoid full graph scans g.V('user_id') // Direct vertex lookup
12. Graph Database Integration
TinkerPop-Enabled Databases
- Neo4j
- Amazon Neptune
- JanusGraph
- OrientDB
- ArangoDB
13. Common Graph Algorithms
Path and Connectivity
groovy
123456// Shortest path g.V('start').repeat(both()).until(hasId('end')).path() // Centrality calculation g.V().pageRank()
14. Best Practices
- Always use vertex/edge IDs for direct lookups
- Minimize full graph scans
- Use local steps for performance
- Leverage side-effects for complex computations
- Chain traversals efficiently
- Use lazy evaluation
Anti-Patterns
- Avoid multiple full graph traversals
- Don't create unnecessary intermediate collections
- Minimize memory-intensive operations
Additional Resources
- Apache TinkerPop Documentation
- Gremlin Language Reference
- Graph Databases: Principles and Algorithmic Approaches
Continue Learning
Discover more cheatsheets to boost your productivity