简体   繁体   English

在遍历 B 中迭代遍历 A 的值列表(Gremlin)

[英]Iterate list of values from traversal A in traversal B (Gremlin)

This is my test data:这是我的测试数据:

graph = TinkerGraph.open()
g= graph.traversal()
g.addV('Account').property('id',"0x0").as('a1').
  addV('Account').property('id',"0x1").as('a2').
  addV('Account').property('id',"0x2").as('a3').
  addV('Token').property('address','1').as('tk1').
  addV('Token').property('address','2').as('tk2').
  addV('Token').property('address','3').as('tk3').
  addV('Trx').property('address','1').as('Trx1').
  addV('Trx').property('address','1').as('Trx2').
  addV('Trx').property('address','3').as('Trx3').
  addE('sent').from('a1').to('Trx1').
  addE('sent').from('a2').to('Trx2').
  addE('received_by').from('Trx1').to('a2').
  addE('received_by').from('Trx2').to('a3').
  addE('distributes').from('a1').to('tk1').
  addE('distributes').from('a1').to('tk2').
  addE('distributes').from('a1').to('tk3').
  iterate()

I need to first get all the Token addresses using the distributes relationship and then with those values loop through a traversal.我需要首先使用分布关系获取所有令牌地址,然后使用这些值循环遍历。 This is an example of what I need for one single token这是我需要一个令牌的示例

h = g.V().has('Account','id','0x0').next()
token = '1'
g.V(h).
      out('sent').has('address',token).as('t1').
      out('received_by').as('a2').
      out('sent').has('address',token).as('t2').
      out('received_by').as('a3').
      select('a3','a2'). \
         by('id').toList()

This is the output:这是 output:

[a3:0x2,a2:0x1]

Instead of doing that has('address',token) on each hop I could omit it and just make sure the token address is the same by placing a where('t1',eq('t2')).by('address') at the end of the traversal, but this performs badly given my database design and indexes.我可以省略它并通过放置where('t1',eq('t2')).by('address') has('address',token) where('t1',eq('t2')).by('address')在遍历结束时,但考虑到我的数据库设计和索引,这表现不佳。

So what I do to iterate is:所以我要迭代的是:

tokens = g.V(h).out('distributes').values('address').toList()
finalList = []
for (token in tokens){    
    finalList.add(g.V(h).
      out('sent').has('address',token).
      out('received_by').as('a2').
      out('sent').has('address',token).
      out('received_by').as('a3').
      select('a3','a2'). \
         by('id').toList())  
 }

And this is what's stored in finalList at the end:这是最后存储在 finalList 中的内容:

==>[[a3:0x2,a2:0x1]]
==>[]
==>[]

This works but I was wondering how can I iterate that token list this way without leaving Gremlin and without introducing that for loop.这行得通,但我想知道如何在不离开 Gremlin 且不引入 for 循环的情况下以这种方式迭代该令牌列表。 Also, my results contain empty results which is not optimal.此外,我的结果包含不是最佳的空结果。 The key here for me is to always be able to do that has('address',token) for each hop with the tokens that the Account node has ever sent.对我来说,这里的关键是始终能够使用帐户节点曾经发送过的令牌为每个跃点执行has('address',token) Thank you very much.非常感谢你。

There is still uncertainty about what you are trying to achieve.您要实现的目标仍然存在不确定性。

Nevertheless, I think this query does what you need:尽管如此,我认为这个查询可以满足您的需要:

g.V().has('Account', 'id', '0x0').as('a').
  out('distributes').values('address').as('t').
    select('a').
  repeat(out('sent').where(values('address').
        as('t')).
    out('received_by')).
  emit()

Example: https://gremlify.com/spwya4itlvd示例: https://gremlify.com/spwya4itlvd

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

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