Gremlin: how can I return vertex and their associated vertex?

故事扮演 提交于 2019-12-13 03:09:50

问题


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]]


来源:https://stackoverflow.com/questions/46681625/gremlin-how-can-i-return-vertex-and-their-associated-vertex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!