Until now, traversal steps such as out(), in(), and both() have focused on moving between vertices. These steps answer questions like which vertices are connected and how they are related.
However, there are many situations where the edge itself is the object of interest:
· When did a relationship occur?
· What properties are stored on the relationship?
· How many relationships of a certain type exist?
· Which connections satisfy specific conditions?
To answer such questions, Gremlin allows traversals to move from vertices to edges. The outE() step is the first and most common example of this pattern.
The outE() step performs the following operation:
· Start at the current vertex
· Select all edges for which the current vertex is the out-vertex
· Emit those edges as the traversal result
Formally, outE() returns all outgoing incident edges of the current vertex. This step does not move to adjacent vertices. It stops at the edges themselves.
Filtering by Edge Label
Like vertex traversal steps, outE() supports edge label filtering:
· outE() → all outgoing edges
· outE('placed') → outgoing edges labeled placed
· outE('contains', 'deliveredTo') → multiple edge labels
Filtering is especially important when vertices participate in multiple relationships.
Example: An E-Commerce Graph
To make the discussion concrete, consider an e-commerce domain modeled as a property graph. The graph contains:
Vertex Types
· city: Bengaluru, Hyderabad, Chennai, Pune
· category: Electronics, Books, Clothing
· product: Laptop, Smartphone, Java Programming Book, T-Shirt
· customer: Amit Sharma, Priya Iyer, Rohit Verma
· order: O9001 to O9005
Edge Types
· belongsTo: product → category
· placed: customer → order
· contains: order → product
· deliveredTo: order → city
Each edge has a clear semantic direction that aligns with real-world meaning.
Gremlin Statements to build the Graph
graph = TinkerGraph.open() g = graph.traversal() blr = g.addV('city').property('name', 'Bengaluru').next() hyd = g.addV('city').property('name', 'Hyderabad').next() chn = g.addV('city').property('name', 'Chennai').next() pne = g.addV('city').property('name', 'Pune').next() catElectronics = g.addV('category').property('name', 'Electronics').next() catBooks = g.addV('category').property('name', 'Books').next() catClothing = g.addV('category').property('name', 'Clothing').next() p1 = g.addV('product').property('sku', 'P1001').property('name', 'Laptop').next() p2 = g.addV('product').property('sku', 'P1002').property('name', 'Smartphone').next() p3 = g.addV('product').property('sku', 'P2001').property('name', 'Java Programming Book').next() p4 = g.addV('product').property('sku', 'P3001').property('name', 'T-Shirt').next() g.V(p1).addE('belongsTo').to(catElectronics).next() g.V(p2).addE('belongsTo').to(catElectronics).next() g.V(p3).addE('belongsTo').to(catBooks).next() g.V(p4).addE('belongsTo').to(catClothing).next() c1 = g.addV('customer').property('customerId', 'C101').property('name', 'Amit Sharma').next() c2 = g.addV('customer').property('customerId', 'C102').property('name', 'Priya Iyer').next() c3 = g.addV('customer').property('customerId', 'C103').property('name', 'Rohit Verma').next() o1 = g.addV('order').property('orderId', 'O9001').next() o2 = g.addV('order').property('orderId', 'O9002').next() o3 = g.addV('order').property('orderId', 'O9003').next() o4 = g.addV('order').property('orderId', 'O9004').next() o5 = g.addV('order').property('orderId', 'O9005').next() g.V(c1).addE('placed').to(o1).next() g.V(c1).addE('placed').to(o2).next() g.V(c2).addE('placed').to(o3).next() g.V(c3).addE('placed').to(o4).next() g.V(c3).addE('placed').to(o5).next() g.V(o1).addE('contains').to(p1).next() g.V(o1).addE('contains').to(p3).next() g.V(o2).addE('contains').to(p2).next() g.V(o3).addE('contains').to(p4).next() g.V(o4).addE('contains').to(p1).next() g.V(o4).addE('contains').to(p4).next() g.V(o5).addE('contains').to(p3).next() g.V(o1).addE('deliveredTo').to(blr).next() g.V(o2).addE('deliveredTo').to(blr).next() g.V(o3).addE('deliveredTo').to(hyd).next() g.V(o4).addE('deliveredTo').to(chn).next() g.V(o5).addE('deliveredTo').to(pne).next()
Example 1: Print all vertices and edges
gremlin> g.V().valueMap(true) ==>[id:0,label:city,name:[Bengaluru]] ==>[id:33,label:customer,customerId:[C102],name:[Priya Iyer]] ==>[id:2,label:city,name:[Hyderabad]] ==>[id:4,label:city,name:[Chennai]] ==>[id:36,label:customer,customerId:[C103],name:[Rohit Verma]] ==>[id:6,label:city,name:[Pune]] ==>[id:39,label:order,orderId:[O9001]] ==>[id:8,label:category,name:[Electronics]] ==>[id:41,label:order,orderId:[O9002]] ==>[id:10,label:category,name:[Books]] ==>[id:43,label:order,orderId:[O9003]] ==>[id:12,label:category,name:[Clothing]] ==>[id:45,label:order,orderId:[O9004]] ==>[id:14,label:product,name:[Laptop],sku:[P1001]] ==>[id:47,label:order,orderId:[O9005]] ==>[id:17,label:product,name:[Smartphone],sku:[P1002]] ==>[id:20,label:product,name:[Java Programming Book],sku:[P2001]] ==>[id:23,label:product,name:[T-Shirt],sku:[P3001]] ==>[id:30,label:customer,customerId:[C101],name:[Amit Sharma]] gremlin> gremlin> gremlin> gremlin> g.E().valueMap(true) ==>[id:64,label:deliveredTo] ==>[id:65,label:deliveredTo] ==>[id:49,label:placed] ==>[id:50,label:placed] ==>[id:51,label:placed] ==>[id:52,label:placed] ==>[id:53,label:placed] ==>[id:54,label:contains] ==>[id:55,label:contains] ==>[id:56,label:contains] ==>[id:57,label:contains] ==>[id:26,label:belongsTo] ==>[id:58,label:contains] ==>[id:27,label:belongsTo] ==>[id:59,label:contains] ==>[id:28,label:belongsTo] ==>[id:60,label:contains] ==>[id:29,label:belongsTo] ==>[id:61,label:deliveredTo] ==>[id:62,label:deliveredTo] ==>[id:63,label:deliveredTo]
Example 2: Outgoing Edges from a Customer
To retrieve all relationships initiated by a customer.g.V(). has('customer', 'name', 'Amit Sharma'). outE(). valueMap(true)
gremlin> g.V(). ......1> has('customer', 'name', 'Amit Sharma'). ......2> outE(). ......3> valueMap(true) ==>[id:49,label:placed] ==>[id:50,label:placed]
Example 3: Orders Contained in an Order (Edge-Focused)
To examine how an order connects to its products.
g.V(). has('order', 'orderId', 'O9001'). outE('contains')
gremlin> g.V(). ......1> has('order', 'orderId', 'O9001'). ......2> outE('contains') ==>e[54][39-contains->14] ==>e[55][39-contains->20]
Example 4: From Edges Back to Vertices
g.V().has('order', 'orderId', 'O9001'). outE('contains'). inV(). valueMap(true)
gremlin> g.V().has('order', 'orderId', 'O9001'). ......1> outE('contains'). ......2> inV(). ......3> valueMap(true) ==>[id:14,label:product,name:[Laptop],sku:[P1001]] ==>[id:20,label:product,name:[Java Programming Book],sku:[P2001]]
Mental Model to Remember
out() → jump directly to adjacent vertices
outE() → stop at the relationship
inV() → complete the hop from relationship to vertex
No comments:
Post a Comment