Monday, 11 May 2026

Retrieving Property Values from a Vertex

  

Properties are a fundamental aspect of the Apache TinkerPop property graph model. Vertices and edges are not only the identifiers connected by relationships; they are rich entities that carry descriptive information in the form of key–value pairs. Apache TinkerPop provides a flexible and expressive set of steps to add, remove, and query these properties during graph traversals.

 

This section focuses on retrieving property values from vertices, starting with simple value extraction and progressing toward more structured representations that include both property keys and their associated values.

 

Understanding Vertex Properties in TinkerPop

In TinkerPop, a vertex property consists of:

·      A key (for example, name, age, or course)

·      One or more values

 

Properties can exist on both vertices and edges, depending on the traversal step used, results may include:

·      Only property values

·      Property keys and values together

·      Metadata such as vertex identifiers and labels

 

Choosing the correct step is important, as it directly affects how much information is returned and how it can be processed downstream.

 

Creating Sample student Vertices for Demonstration

graph = TinkerGraph.open()
g = graph.traversal()

g.addV('student').
  property('name', 'Rahul').
  property('city', 'Hyderabad').
  property('course', 'Computer Science').
  property('year', 3).
  property('gpa', 8.6)

g.addV('student').
  property('name', 'Sneha').
  property('city', 'Pune').
  property('course', 'Mechanical Engineering').
  property('year', 2).
  property('gpa', 7.9)

g.addV('student').
  property('name', 'Amit').
  property('city', 'Bengaluru').
  property('course', 'Data Science').
  property('year', 4).
  property('gpa', 9.1)

gremlin> graph = TinkerGraph.open()
==>tinkergraph[vertices:0 edges:0]
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> 
gremlin> g.addV('student').
......1>   property('name', 'Rahul').
......2>   property('city', 'Hyderabad').
......3>   property('course', 'Computer Science').
......4>   property('year', 3).
......5>   property('gpa', 8.6)
==>v[0]
gremlin> 
gremlin> g.addV('student').
......1>   property('name', 'Sneha').
......2>   property('city', 'Pune').
......3>   property('course', 'Mechanical Engineering').
......4>   property('year', 2).
......5>   property('gpa', 7.9)
==>v[6]
gremlin> 
gremlin> g.addV('student').
......1>   property('name', 'Amit').
......2>   property('city', 'Bengaluru').
......3>   property('course', 'Data Science').
......4>   property('year', 4).
......5>   property('gpa', 9.1)
==>v[12]

Verifying the Inserted Vertices

g.V().hasLabel('student').valueMap(true)

gremlin> g.V().hasLabel('student').valueMap(true)
==>[id:0,label:student,city:[Hyderabad],year:[3],name:[Rahul],course:[Computer Science],gpa:[8.6]]
==>[id:6,label:student,city:[Pune],year:[2],name:[Sneha],course:[Mechanical Engineering],gpa:[7.9]]
==>[id:12,label:student,city:[Bengaluru],year:[4],name:[Amit],course:[Data Science],gpa:[9.1]]

Retrieving the property values of a specific student

g.V().
  hasLabel('student').
  has('name', 'Sneha').
  values()

gremlin> g.V().
......1>   hasLabel('student').
......2>   has('name', 'Sneha').
......3>   values()
==>Pune
==>2
==>Sneha
==>Mechanical Engineering
==>7.9

   

Retrieving Values for Specific Properties

The values() step also accepts one or more property keys as parameters. This allows you to retrieve values selectively instead of returning everything.

 

g.V().
  hasLabel('student').
  has('name', 'Sneha').
  values('name', 'course')

This traversal returns only the value associated with the name and course properties. It is precise, readable, and efficient when only some specific attributes are required.

gremlin> g.V().
......1>   hasLabel('student').
......2>   has('name', 'Sneha').
......3>   values('name', 'course')
==>Sneha
==>Mechanical Engineering

   

Retrieving Keys and Values Together with valueMap()

The valueMap() step returns a structured representation of vertex properties, preserving the association between property keys and their values.

 

g.V().
  hasLabel('student').
  has('name', 'Sneha').
  valueMap()

gremlin> g.V().
......1>   hasLabel('student').
......2>   has('name', 'Sneha').
......3>   valueMap()
==>[city:[Pune],year:[2],name:[Sneha],course:[Mechanical Engineering],gpa:[7.9]]

From the output, you can deduce:

·      Each property key maps to a list of values

·      This design supports multi-valued properties in TinkerPop

 

We can even retrieve id, label properties of a vertex by passing the argument 'true' to the valueMap step.

g.V().
  hasLabel('student').
  has('name', 'Sneha').
  valueMap(true)

gremlin> g.V().
......1>   hasLabel('student').
......2>   has('name', 'Sneha').
......3>   valueMap(true)
==>[id:6,label:student,city:[Pune],year:[2],name:[Sneha],course:[Mechanical Engineering],gpa:[7.9]]

   

Why Values Are Returned as Lists?

Even if a property has only one value, TinkerPop represents it as a list. This ensures a consistent model that supports:

 

·      Multi-cardinality properties

·      Schema flexibility across different graph implementations

 

Understanding this behavior is essential when processing traversal results programmatically.

 

In summary,

·      values() extracts only property values, discarding keys and context

·      It can be limited to specific properties by passing key names

·      valueMap() preserves the relationship between keys and values

·      valueMap(true) return id, label properties as well

 

 

Previous                                                    Next                                                    Home

Gremlin Traversals: The Foundation of Graph Queries

  

In Apache TinkerPop, a graph query is expressed as a traversal. The term is deliberate and meaningful, rather than issuing a declarative query that describes what data is required, a Gremlin traversal describes how to move through the graph to reach the desired result.

 

At a conceptual level, a traversal starts at one or more elements in the graph vertices or edges and progresses step by step until it reaches a final outcome. Each step represents a well-defined operation, and traversals are built by chaining these steps together in a fluent, readable manner.

 

A traversal may consist of a single step, but in practice it is almost always composed of multiple steps that progressively narrow, transform, or project the result set.

 

The Traversal Source

All Gremlin traversals begin with a traversal source, commonly named g. The traversal source acts as the entry point into the graph and provides the initial steps used to start a traversal.

graph = TinkerGraph.open()
g = graph.traversal()

gremlin> graph = TinkerGraph.open()
==>tinkergraph[vertices:0 edges:0]
gremlin> 
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]

This statement creates a GraphTraversalSource instance backed by the underlying graph. From this point onward, all traversals are expressed by invoking methods on g.

 

Starting Points: V() and E()

Most traversals begin with either:

 

·      g.V(): to start from vertices

·      g.E(): to start from edges

 

The V() step returns a traversal over vertices, while the E() step returns a traversal over edges. These steps can be used both at the beginning of a traversal and, in certain cases, in the middle of a traversal to switch context. Their advanced usage will be explored later in this tutorial series.

 

At this introductory stage, it is helpful to think of:

 

·      g.V() as "looking at all vertices in the graph"

·      g.E() as "looking at all edges in the graph"

 

Additional steps are then applied to filter, navigate, or reshape these elements. Both V() and E() also accept parameters that restrict the traversal to a specific subset of vertices or edges, such as those with known identifiers. For now, we will focus on their parameterless forms.

 

Filtering Traversals with has and hasLabel

To refine the set of vertices or edges under consideration, Gremlin provides filtering steps. Two of the most frequently used filters are:

 

·      hasLabel(label): filters elements by label

·      has(key, value): filters elements by property key and value

 

These steps act as predicates, allowing only elements that satisfy the specified condition to pass through the traversal. While Gremlin offers many variations of the has step, including predicates and composite conditions, these two forms are sufficient to begin writing meaningful traversals.

 

Creating Sample Data: Employee Vertices

Consider the following example graph containing employee data. Each employee is represented as a vertex with the label employee and a set of properties.

 

g.addV('employee').
  property('name', 'Asha').
  property('department', 'Engineering').
  property('location', 'India')

g.addV('employee').
  property('name', 'Ravi').
  property('department', 'Engineering').
  property('location', 'India')

g.addV('employee').
  property('name', 'Kiran').
  property('department', 'Engineering').
  property('location', 'USA')

g.addV('employee').
  property('name', 'Meera').
  property('department', 'HR').
  property('location', 'India')

g.addV('employee').
  property('name', 'Anita').
  property('department', 'HR').
  property('location', 'USA')

gremlin> g.addV('employee').
......1>   property('name', 'Asha').
......2>   property('department', 'Engineering').
......3>   property('location', 'India')
==>v[0]
gremlin> 
gremlin> g.addV('employee').
......1>   property('name', 'Ravi').
......2>   property('department', 'Engineering').
......3>   property('location', 'India')
==>v[4]
gremlin> 
gremlin> g.addV('employee').
......1>   property('name', 'Kiran').
......2>   property('department', 'Engineering').
......3>   property('location', 'USA')
==>v[8]
gremlin> 
gremlin> g.addV('employee').
......1>   property('name', 'Meera').
......2>   property('department', 'HR').
......3>   property('location', 'India')
==>v[12]
gremlin> 
gremlin> g.addV('employee').
......1>   property('name', 'Anita').
......2>   property('department', 'HR').
......3>   property('location', 'USA')
==>v[16]
gremlin>

Querying by Label

The following traversal returns all vertices that have the label employee:

g.V().hasLabel('employee')

This traversal:

·      Starts with all vertices in the graph

·      Filters them to retain only those whose label is employee

 

At this stage, the traversal returns a stream of vertices rather than concrete values.

gremlin> g.V().
......1>   hasLabel('employee').
......2>   valueMap(true)
==>[id:0,label:employee,name:[Asha],location:[India],department:[Engineering]]
==>[id:16,label:employee,name:[Anita],location:[USA],department:[HR]]
==>[id:4,label:employee,name:[Ravi],location:[India],department:[Engineering]]
==>[id:8,label:employee,name:[Kiran],location:[USA],department:[Engineering]]
==>[id:12,label:employee,name:[Meera],location:[India],department:[HR]]

Querying by Property

To locate a specific Vertex by name, the has step can be used:

g.V().has('name', 'Asha')

This traversal returns the vertex representing the employee named Asha. Note that this query does not explicitly check the label. If multiple vertex types share a name property, the result set may include unexpected elements.

gremlin> g.V().
......1>   has('name', 'Asha').
......2>   valueMap(true)
==>[id:0,label:employee,name:[Asha],location:[India],department:[Engineering]]

   

Combining Label and Property Filters

In practice, it is common to combine label and property filters to make queries more precise. The following two traversals are equivalent.

 

·      g.V().hasLabel('employee').has('name', 'Asha')

·      g.V().has('employee', 'name', 'Asha')

 

The second form uses an overloaded version of the has step that accepts the label as its first argument. Both traversals ensure that:

 

·      The element is an employee

·      The name property has the value Asha

 

Choosing between these forms is largely a matter of style and readability.

 

 

gremlin> g.V().
......1>   hasLabel('employee').
......2>   has('name', 'Asha').
......3>   valueMap(true)
==>[id:0,label:employee,name:[Asha],location:[India],department:[Engineering]]
gremlin> 
gremlin> g.V().
......1>   has('employee', 'name', 'Asha').
......2>   valueMap(true)
==>[id:0,label:employee,name:[Asha],location:[India],department:[Engineering]]

Understanding the Traversal Result

When executed in the Gremlin Console, the result of the traversal appears as:

v[4]

gremlin> g.V().has('employee', 'name', 'Ravi')
==>v[4]

   

This output indicates that the traversal returned a vertex with an internal identifier of 4. What is returned is not a simple value, but an instance of the TinkerPop Vertex data structure.

 

At this stage, the traversal itself has not yet produced a concrete object suitable for further manipulation. To do so, a terminal step must be applied.

 

Terminal Steps and Materializing Results

Terminal steps conclude a traversal and convert its result into a concrete object. One commonly used terminal step is next(), which retrieves the next element from the traversal.

 

v = g.V().hasLabel('employee').has('name', 'Asha').next()

   

Here, the variable v now holds a Vertex instance. Although the Gremlin Console uses Groovy as its execution environment, the underlying objects are Java based. This allows standard Java introspection methods to be applied.

 

For example, the following expression reveals the runtime type of the returned object:

 

gremlin> v.getClass()
==>class org.apache.tinkerpop.gremlin.groovy.loaders.SugarLoader$VertexCategory

   

This confirms that the result is a TinkerPop Vertex implementation.

 

Terminal steps play an important role when Gremlin is embedded in applications, as they mark the boundary between traversal construction and application-level logic. Additional terminal steps, such as toList, hasNext, and iterate, will be introduced in later post as we move from console based exploration to production usage.

 

Previous                                                    Next                                                    Home