Gremlin: how can I return vertex and their associa

2019-08-19 03:30发布

问题:

I Need to return some groups and people in that group, like this:

Group A
-----Person A
-----Person B
-----Person C

Group B
-----Person D
-----Person E
-----Person F

How can I do that with gremlin. They are connected to group with a edge.

回答1:

It is always helpful to include a sample graph with your questions on Gremlin preferably as a something easily pasted to the Gremlin Console as follows:

g.addV('group').property('name','Group A').as('ga').
  addV('group').property('name','Group B').as('gb').
  addV('person').property('name','Person A').as('pa').
  addV('person').property('name','Person B').as('pb').
  addV('person').property('name','Person C').as('pc').
  addV('person').property('name','Person D').as('pd').
  addV('person').property('name','Person E').as('pe').
  addV('person').property('name','Person F').as('pf').
  addE('contains').from('ga').to('pa').
  addE('contains').from('ga').to('pb').
  addE('contains').from('ga').to('pc').
  addE('contains').from('gb').to('pd').
  addE('contains').from('gb').to('pe').
  addE('contains').from('gb').to('pf').iterate()

A solution to your problem is to use group() step:

gremlin> g.V().has('group', 'name', within('Group A','Group B')).
......1>   group().
......2>     by('name').
......3>     by(out('contains').values('name').fold())
==>[Group B:[Person D,Person E,Person F],Group A:[Person A,Person B,Person C]]