简体   繁体   English

遍历Gremlin Cosmos DB的价值汇总

[英]Value Aggregation along Traversal Gremlin Cosmos DB

I need to perform a query of the following form: I have a tree structure with costs at the leaf node. 我需要执行以下形式的查询:我有一个树结构,叶子节点处有开销。 I need a single query to give me all the aggregated costs under the root node. 我只需要一个查询即可向我提供根节点下的所有汇总成本。 在此处输入图片说明

For example in the above graph, I would expect an output from my query like 例如,在上图中,我希望查询输出如下

{ 1: 6, 2: 4, 3: 2, 4: 1, 5: 1, 6: 2, 7: 1, 8: 1}

I was looking into using the 'sack' step from the Gremlin API for this, but cosmosDB doesn't seem to support sacks currently. 我当时正在考虑使用Gremlin API中的“麻袋”步骤,但是cosmosDB目前似乎不支持麻袋。 I also tried storing a pseudo-property of "aggregated-cost" and working my way up from the leaf nodes, but I was unable to figure out how to store a dynamic value at each node as a property that is local to only that node. 我还尝试存储“聚合成本”的伪属性,并从叶节点向上移动,但是我无法弄清楚如何在每个节点上将动态值存储为仅对该节点本地的属性。 Is this kind of query possible given these constraints? 考虑到这些限制,这种查询是否可能?

When asking questions about Gremlin, it is always best to include a short Gremlin script of sample data: 在询问有关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()

With the steps available in CosmosDB I think that the closest you might be able to get is this: 通过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]]

The group() helps produce the Map structure you wanted. group()有助于生成所需的Map结构。 Then for each vertex you group on you use repeat() to traverse out until you reach the leaf vertices. 然后,对您分组的每个顶点使用repeat()进行遍历,直到到达叶顶点。 Note that emit() is ensuring that only those vertices that are leaves with the "cost" property are being returned for purpose of the result. 请注意, emit()确保仅出于结果目的返回那些具有“ cost”属性的叶子的顶点。

The reason I say that this is about as close as you can get with CosmosDB is because I don't see that CosmosDB supports the sum() step here . 我之所以说这与CosmosDB差不多,是因为我没有看到CosmosDB在这里支持sum()步骤。 If it did then: 如果这样做,则:

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]

I guess you will have to do that final computation on the returned result yourself. 我猜您将不得不自己对返回结果进行最终计算。

For others (or when CosmosDB supports sack() in the future) you can do: 对于其他用户(或当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]

Courtesy of the Gremlin Guru . 图片由Gremlin Guru提供

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

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