需要在gremlin中使用sum using group by

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

我正在使用 aws neptune,需要按属性“月”对结果集进行分组,并对 2 个属性的计算执行求和函数。

以下查询用于使用 math step 和 monthName 投影计算数据

g.V().hasLabel('Customer').out().hasLabel('Workspace')
.has('lastUpdatedOn').has('createdOn')
.project('gp1','var1','var2')
    .by(values('lastUpdatedOn_MN'))
    .by(values('createdOn'))
    .by(values('lastUpdatedOn'))
.project('mathResult','monthName')
    .by(math('var2-var1'))
    .by('gp1').fold()

结果如下:

{
    'mathResult': 92111.0,
    'monthName': 'MARCH'
},
{
    'mathResult': 302648.0,
    'monthName': 'MARCH'
},
{
    'mathResult': 23820301.0,
    'monthName': 'FEBRUARY'
}
]

之后,我按投影中的“monthName”属性对结果进行分组,如下所示


g.V().hasLabel('Customer').out().hasLabel('Workspace')
.has('lastUpdatedOn').has('createdOn')
.project('gp1','var1','var2')
    .by(values('lastUpdatedOn_MN'))
    .by(values('createdOn'))
    .by(values('lastUpdatedOn'))
.project('mathResult','monthName')
    .by(math('var2-var1'))
    .by('gp1')
.group().by('monthName').by('mathResult')

这导致以下输出

{
    'MARCH': [
        92111.0,
        302648.0
    ],
    'FEBRUARY': [
        23820301.0
    ]
}

请帮助我在上面的输出中得到一个结果,而不是得到数字列表,我可以获得列表中数字的总和。我使用了求和函数,但总是出错。

我使用求和函数的方式如下:

g.V().hasLabel('Customer').out().hasLabel('Workspace')
.has('lastUpdatedOn').has('createdOn')
.project('gp1','var1','var2')
    .by(values('lastUpdatedOn_MN'))
    .by(values('createdOn'))
    .by(values('lastUpdatedOn'))
.project('mathResult','monthName')
    .by(math('var2-var1'))
    .by('gp1')
.group().by('monthName').by(values('mathResult').sum())

但是我收到以下错误:

错误:根: 收到错误消息'{'requestId':'41e740ab-a499-4200-b476-3e195e026607','状态':{'消息':'{“requestId”:“41e740ab-a499-4200-b476-3e195e026607”,“代码":"UnsupportedOperationException"}', 'code': 498, 'attributes': {}}, 'result': {'data': None, 'meta': {}}}'

结果字典'{'41e740ab-a499-4200-b476-3e195e026607':}

gremlin amazon-neptune neptune
1个回答
0
投票

我使用以下方法创建了数据集:

g.addV('Customer').
  property(T.id, '1').
  addV('Workspace').
  property(T.id, '2').
  property('lastUpdatedOn', 2000).
  property('createdOn', 1000).
  property('lastUpdatedOn_MN', 'March').
  addV('Workspace').
  property(T.id, '3').
  property('lastUpdatedOn', 4000).
  property('createdOn', 2000).
  property('lastUpdatedOn_MN', 'March').
  addV('Workspace').
  property(T.id, '4').
  property('lastUpdatedOn', 20000).
  property('createdOn', 10000).
  property('lastUpdatedOn_MN', 'April').
  addV('Workspace').
  property(T.id, '5').
  property('lastUpdatedOn', 40000).
  property('createdOn', 20000).
  property('lastUpdatedOn_MN', 'April').
  V().
  hasLabel('Workspace').
  addE('connected').from(__.V('1'))

然后我运行下面的查询以获得所需的总和。

gremlin> g.V().
......1>   hasLabel('Customer').
......2>   out().
......3>   hasLabel('Workspace').
......4>   has('lastUpdatedOn').
......5>   has('createdOn').
......6>   project('gp1', 'var1', 'var2').
......7>     by(values('lastUpdatedOn_MN')).
......8>     by(values('createdOn')).
......9>     by(values('lastUpdatedOn')).
.....10>   project('mathResult', 'monthName').by(math('var2-var1')).by('gp1').
.....11>   group().by('monthName').by('mathResult').
.....12>   unfold().as('a').
.....13>   select(values).
.....14>   sum(local).
.....15>   group().by(select('a').select(keys)).by(unfold())
==>[March:3000.0,April:30000.0]
© www.soinside.com 2019 - 2024. All rights reserved.