Ebad
Ebad

Reputation: 346

Retrieve values from multiple vertices in Map with Gremlin Tinkerpop

In a graph of several vertices, how can I traverse the graph and collect select properties from multiple vertices?

Let's say I have 4 vertices (Person1, Person2, Person3, Person4), and I want to get the value of a field "name" in Person1, "name" and "age" from Person2, "birthday" from Person3, and "hasPets" in Person4. How can I traverse the graph and collect only the values for each of these fields that I want and store them in a map/hashmap?

From other examples:

How do I retrieve multiple multi-properties in Gremlin?

How can I collect property values while traversing a graph with gremlin in Java?

It shows that I can get a map with valueOf(), but I don't see how I can use that on different vertices in one traversal. The other example shows how to get multiple values from different vertices, but they are just stored in a list. I need to be able to know which value is which.

Upvotes: 0

Views: 723

Answers (1)

Kelvin Lawrence
Kelvin Lawrence

Reputation: 14391

As I don't have your data, here is a simple example using air routes that shows how a choose and an option step can be used if you know at least some common property of each vertex you can test on.

gremlin> g.V(1,2,3,4).choose(values('code')).
......1>              option('AUS',valueMap('code','city')).
......2>              option('ATL',valueMap('code','region')).
......3>              option('ANC',valueMap('code','desc')) 
  
==>[code:[ATL],region:[US-GA]]
==>[code:[ANC],desc:[Anchorage Ted Stevens]]
==>[code:[AUS],city:[Austin]]  

If you do not have such a property using the pattern suggested in the other comment along the lines of several as and select steps will work but might become a bit unwieldy (not that the choose case will not as well), at any size.

Upvotes: 0

Related Questions