Gremlin:根据一行代码中的两个计数计算除法

问题描述 投票:0回答:2

我有两个计数,计算如下:

1)g.V()。hasLabel( '品牌')。其中,(__。INE( 'client_brand')。COUNT()。被(GT(0)))。COUNT()

2)g.V()。hasLabel( '品牌')。COUNT()

我希望得到一行代码,导致第一个计数除以第二个。

orientdb gremlin
2个回答
1
投票

这是一种方法:

g.V().hasLabel('brand').
  fold().as('a','b').
  math('a/b').
    by(unfold().where(inE('client_brand')).count())
    by(unfold().count())

请注意,我将第一次遍历简化为.where(inE('client_brand')).count(),因为您只需要计算至少有一条边,不需要对它们进行全部计算并进行比较。

你也可以像union()一样:

g.V().hasLabel('brand').
  union(where(inE('client_brand')).count(),
        count())
  fold().as('a','b').
  math('a/b').
    by(limit(local,1))
    by(tail(local))

虽然第一个更容易阅读/遵循,我猜第二个更好,因为它只存储两个计数的列表,而第一个存储所有“品牌”顶点的列表,这将是更多的内存密集我猜测。

另一种方式,由Daniel Kuppitz提供,以有趣的方式使用groupCount()

g.V().hasLabel('brand').
  groupCount().
    by(choose(inE('client_brand'),
                constant('a'),
                constant('b'))).
  math('a/(a+b)')

使用sack()步骤的以下解决方案显示了为什么我们有math()步骤:

g.V().hasLabel('brand').
  groupCount().
    by(choose(inE('client_brand'),
                constant('a'),
                constant('b'))).
  sack(assign).
    by(coalesce(select('a'), constant(0))).
  sack(mult).
    by(constant(1.0)). /* we need a double */
  sack(div).
    by(select(values).sum(local)).
  sack()

如果你可以使用lambdas:

g.V().hasLabel('brand').
  union(where(inE('client_brand')).count(),
        count())
  fold().
  map{ it.get()[0]/it.get()[1]} 

-2
投票

这对我有用:

g.V().limit(1).project('client_brand_count','total_brands')
.by(g.V().hasLabel('brand')
.where(__.inE('client_brand').count().is(gt(0))).count())
.by(g.V().hasLabel('brand').count())
.map{it.get().values()[0] / it.get().values()[1]}
.project('brand_client_pct')
© www.soinside.com 2019 - 2024. All rights reserved.