Gremlin Query Language¶
Gremlin is a graph traversal language developed by Apache TinkerPop. Grafeo supports Gremlin as an optional query language via feature flag.
Overview¶
Gremlin uses a functional, step-based approach to traverse and manipulate graph data. It's designed around the concept of traversing vertices and edges through a series of chained steps.
Enabling Gremlin¶
Gremlin support is optional and requires a feature flag:
Quick Reference¶
| Operation | Syntax |
|---|---|
| All vertices | g.V() |
| Vertex by ID | g.V(id) |
| Filter by label | g.V().hasLabel('Person') |
| Filter by property | g.V().has('name', 'Alice') |
| Outgoing edges | g.V().out('KNOWS') |
| Incoming edges | g.V().in('KNOWS') |
| Both directions | g.V().both('KNOWS') |
| Get properties | g.V().values('name') |
| Count results | g.V().count() |
| Limit results | g.V().limit(10) |
Basic Examples¶
Finding Vertices¶
// All vertices
g.V()
// Vertices with a specific label
g.V().hasLabel('Person')
// Vertex with specific property
g.V().has('name', 'Alice')
// Multiple conditions
g.V().hasLabel('Person').has('age', gt(25))
Traversing Edges¶
// Friends of Alice
g.V().has('name', 'Alice').out('KNOWS')
// People who know Bob
g.V().has('name', 'Bob').in('KNOWS')
// Two-hop traversal
g.V().has('name', 'Alice').out('KNOWS').out('KNOWS')
Getting Properties¶
// Get names of all people
g.V().hasLabel('Person').values('name')
// Get multiple properties
g.V().hasLabel('Person').valueMap('name', 'age')
Aggregations¶
// Count all people
g.V().hasLabel('Person').count()
// Count friends
g.V().has('name', 'Alice').out('KNOWS').count()
Python Usage¶
import grafeo
db = grafeo.GrafeoDB()
# Create some data
db.execute("INSERT (:Person {name: 'Alice', age: 30})")
db.execute("INSERT (:Person {name: 'Bob', age: 25})")
db.execute("""
MATCH (a:Person {name: 'Alice'}), (b:Person {name: 'Bob'})
INSERT (a)-[:KNOWS {since: 2020}]->(b)
""")
# Query with Gremlin
result = db.execute_gremlin("g.V().hasLabel('Person').values('name')")
for row in result:
print(row)
# Traverse relationships
friends = db.execute_gremlin("g.V().has('name', 'Alice').out('KNOWS').values('name')")
Rust Usage¶
use grafeo_engine::GrafeoDB;
let db = GrafeoDB::new_in_memory();
// Create data with GQL
db.execute("INSERT (:Person {name: 'Alice'})").unwrap();
// Query with Gremlin
let result = db.execute_gremlin("g.V().hasLabel('Person')").unwrap();
Supported Steps¶
Source Steps¶
g.V()- Start traversal from verticesg.V(id)- Start from specific vertex
Filter Steps¶
hasLabel(label)- Filter by vertex labelhas(key, value)- Filter by property equalityhas(key, predicate)- Filter by property predicate
Traversal Steps¶
out(label?)- Traverse outgoing edgesin(label?)- Traverse incoming edgesboth(label?)- Traverse both directionsoutE(label?)- Get outgoing edgesinE(label?)- Get incoming edges
Property Steps¶
values(key)- Get property valuesvalueMap(keys...)- Get multiple properties as map
Terminal Steps¶
count()- Count elementslimit(n)- Limit results
Learn More¶
-
g.V(), hasLabel, has, and filtering.
-
out, in, both, and relationship patterns.
-
values, valueMap, and property access.
-
count, sum, and grouping.