In many graph queries, the vertices themselves are only part of the story. Often, the real insight lies in the relationship between two vertices, the edge that connects them and the properties stored on that edge. In Apache TinkerPop, edges are first class elements and can carry rich metadata such as distances, timestamps, weights, roles, or permissions.
This post focuses on how to locate, traverse, and inspect edges between specific vertices using Gremlin. You will learn how to move from vertices to edges using steps like outE(), inE(), outV(), and inV(), and how to capture and revisit edges within a traversal using as() and select(). These techniques allow you to precisely target the relationship of interest while filtering vertices along the way.
By the end of this post, you will be comfortable treating edges as primary query targets and extracting meaningful relationship data, an essential skill for building expressive, accurate, and efficient Gremlin traversals.
1. Building the Sample Graph
Let’s start by creating a small in-memory graph using TinkerGraph.
Creating the Graph and Traversal Source
graph = TinkerGraph.open() g = graph.traversal()
Adding Airport Vertices
Each airport is modeled as a vertex with the label airport and a code property.
g.addV('airport').property('code','DEL') // Delhi g.addV('airport').property('code','BOM') // Mumbai g.addV('airport').property('code','BLR') // Bengaluru g.addV('airport').property('code','HYD') // Hyderabad
At this point, we have four airport vertices with no relationships between them.
Adding Route Edges with Distance
Now we add directed route edges from each city to Delhi, storing the flight distance in kilometers using the dist property.
g.V().has('code','BOM'). addE('route').property('dist',1148). to(__.V().has('code','DEL')) g.V().has('code','BLR'). addE('route').property('dist',1740). to(__.V().has('code','DEL')) g.V().has('code','HYD'). addE('route').property('dist',1260). to(__.V().has('code','DEL'))
gremlin> g.V().valueMap(true) ==>[id:0,label:airport,code:[DEL]] ==>[id:2,label:airport,code:[BOM]] ==>[id:4,label:airport,code:[BLR]] ==>[id:6,label:airport,code:[HYD]] gremlin> gremlin> gremlin> g.E().valueMap(true) ==>[id:8,label:route,dist:1148] ==>[id:9,label:route,dist:1740] ==>[id:10,label:route,dist:1260]
2. Examples
Example1: Find the Route distance from Mumbai to Delhi
g.V(). hasLabel('airport'). has('code', 'BOM'). outE('route').as('e'). inV().has('code', 'DEL'). select('e'). valueMap(true)
gremlin> g.V(). ......1> hasLabel('airport'). ......2> has('code', 'BOM'). ......3> outE('route').as('e'). ......4> inV().has('code', 'DEL'). ......5> select('e'). ......6> valueMap(true) ==>[id:8,label:route,dist:1148]
This confirms that the route edge from BOM → DEL has a distance of 1148 km.
We can even achieve above result using inE
g.V(). hasLabel('airport'). has('code', 'DEL'). inE('route').as('e'). outV().has('code', 'BOM'). select('e'). valueMap(true)
gremlin> g.V(). ......1> hasLabel('airport'). ......2> has('code', 'DEL'). ......3> inE('route').as('e'). ......4> outV().has('code', 'BOM'). ......5> select('e'). ......6> valueMap(true) ==>[id:8,label:route,dist:1148]
Example 2: Inspecting All Incoming Routes to Delhi
If you’re interested in all routes leading to Delhi, you don’t need to traverse back to the source vertex.
g.V(). hasLabel('airport'). has('code', 'DEL'). inE('route'). valueMap(true)
gremlin> g.V(). ......1> hasLabel('airport'). ......2> has('code', 'DEL'). ......3> inE('route'). ......4> valueMap(true) ==>[id:8,label:route,dist:1148] ==>[id:9,label:route,dist:1740] ==>[id:10,label:route,dist:1260]
Previous Next Home
No comments:
Post a Comment