[英]Value Aggregation along Traversal Gremlin Cosmos DB
在询问有关Gremlin的问题时,最好总是包括一个简短的示例数据的Gremlin脚本:
g.addV().property('id',1).as('1').
addV().property('id',2).as('2').
addV().property('id',3).as('3').
addV().property('id',4).property('cost',1).as('4').
addV().property('id',5).property('cost',1).as('5').
addV().property('id',6).property('cost',2).as('6').
addV().property('id',7).property('cost',1).as('7').
addV().property('id',8).property('cost',1).as('8').
addE('link').from('1').to('2').
addE('link').from('1').to('3').
addE('link').from('2').to('4').
addE('link').from('2').to('5').
addE('link').from('2').to('6').
addE('link').from('3').to('7').
addE('link').from('3').to('8').iterate()
通过CosmosDB中可用的步骤,我认为您可能能够获得的最接近的结果是:
gremlin> g.V().
......1> group().
......2> by('id').
......3> by(emit(has('cost')).
......4> repeat(out()).
......5> values('cost').
......6> fold())
==>[1:[1,1,2,1,1],2:[1,1,2],3:[1,1],4:[1],5:[1],6:[2],7:[1],8:[1]]
group()
有助于生成所需的Map
结构。 然后,对您分组的每个顶点使用repeat()
进行遍历,直到到达叶顶点。 请注意, emit()
确保仅出于结果目的返回那些具有“ cost”属性的叶子的顶点。
我之所以说这与CosmosDB差不多,是因为我没有看到CosmosDB在这里支持sum()
步骤。 如果这样做,则:
gremlin> g.V().
......1> group().
......2> by('id').
......3> by(emit(has('cost')).
......4> repeat(out()).
......5> values('cost').
......6> sum())
==>[1:6,2:4,3:2,4:1,5:1,6:2,7:1,8:1]
我猜您将不得不自己对返回结果进行最终计算。
对于其他用户(或当CosmosDB将来支持sack()
时),您可以执行以下操作:
gremlin> g.V().has('cost').
......1> sack(assign).
......2> by('cost').
......3> emit().
......4> repeat(__.in('link')).
......5> group().
......6> by('id').
......7> by(sack().sum())
==>[1:6,2:4,3:2,4:1,5:1,6:2,7:1,8:1]
图片由Gremlin Guru提供 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.