简体   繁体   English

Gremlin 选择性遍历

[英]Gremlin selective traversal

This might be a rookie Gremlin question :这可能是一个新手 Gremlin 问题:

Let's say I have this graph假设我有这张图

A [knows---> ] B A [知道---> ] B

A [knows---> ] C A [知道---> ] C

D [knows---> ] C D [知道---> ] C

I want to traverse this graph and find nodes whom only A knows , in this case B and not C because both A and D know C. Is there a way to do this with Gremlin ?我想遍历这个图并找到只有 A 知道的节点,在这种情况下是 B 而不是 C,因为 A 和 D 都知道 C。有没有办法用 Gremlin 做到这一点?

Edit : Sorry I should have been more explicit in the question initially The number of incoming edges can actually be variable.编辑:对不起,我最初应该在问题中更明确 传入边的数量实际上可以是可变的。

g.addV('A').as('a')
  .addV('B').as('b')
  .addV('C').as('c')
  .addV('D').as('d')
  .addV('E').as('e')
  .addV('F')as('f')
  .addE('knows').from('a').to('c')
  .addE('knows').from('b').to('c')
  .addE('knows').from('a').to('f')
  .addE('knows').from('b').to('f')
  .addE('knows').from('d').to('f')

In this case I want only C and not F because A & B know C and F both but D also knows F so I don't want F.在这种情况下,我只想要 C 而不是 F,因为 A & B 知道 C 和 F,但 D 也知道 F,所以我不想要 F。

It is helpful to have a small sample graph.有一个小样本图很有帮助。 Here is one that matches your question.这是一个与您的问题相匹配的。

g.addV('A').as('a').
  addV('B').as('b').
  addV('C').as('c').
  addV('D').as('d').
  addE('knows').from('a').to('b').
  addE('knows').from('a').to('c').
  addE('knows').from('d').to('c')    

Using that graph the query to find friends unique to A could be written as使用该图查找 A 独有的朋友的查询可以写为

gremlin> g.V().hasLabel('A').
               out('knows').
               filter(__.in('knows').count().is(1)).
               path().
               by(label)  

==>[A,B] 

Edited based on the updated question.根据更新的问题进行编辑。

OK so given the additional criteria, I believe this gives you what you need好的,鉴于附加标准,我相信这可以满足您的需求

gremlin>  g.V().hasLabel('A','B').
......1>        out().
......2>        groupCount().
......3>        unfold().
......4>        filter(select(values).is(2)).
......5>        select(keys).
......6>        where(__.in('knows').count().is(2)).
......7>        label()  

==>C   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM