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

No comments:

Post a Comment