Cassandra的计数器类型的总和 [英] aggregate sum on Counter type of Cassandra

查看:683
本文介绍了Cassandra的计数器类型的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Cassandra 2.2.0,我已阅读使用计数器类型,但它不提供太多的细节。

I'm using Cassandra 2.2.0, and I've read using Counter type but it doesn't provide much detail.

有一种方法聚集在计数器类型列为Cassandra,类似于以下?

Is there a way to aggregate on Counter type column for Cassandra, similar to the following?

SELECT sum(my_counter_column) FROM my_table ;

上述查询会导致此错误:

The above query results in this error:

InvalidRequest: code=2200 [Invalid query] message="Invalid call to
function sum, none of its type signatures match (known type
signatures: system.sum : (tinyint) -> tinyint, system.sum : (smallint)
-> smallint, system.sum : (int) -> int, system.sum : (bigint) -> bigint, 
system.sum : (float) -> float, system.sum : (double) ->
double, system.sum : (decimal) -> decimal, system.sum : (varint) ->
varint)"



我知道我可以获取所有数据聚合在客户端,但我只是想知道是否可以在Cassandra中完成。非常感谢。

I know I can fetch all data and then do aggregation in the client, but I'm just wondering if it can be done within Cassandra. Thanks a lot.

推荐答案

当系统 sum()函数。您应该可以为它输入一个jira票这里为2.2.x和3.x,以便它可以修复。

Looks like an oversight when the system sum() function was implemented. You should probably enter a jira ticket for it here for 2.2.x and 3.x so that it can be fixed.

同时,你可以解决问题定义您自己的用户定义的聚合函数(在Cassandra 2.2和更高版本中),如下所示:

In the meantime you can work around the problem by defining your own user defined aggregate function (in Cassandra 2.2 and later) like this:

CREATE FUNCTION agg_counter ( state bigint, val counter )
  CALLED ON NULL INPUT
  RETURNS bigint
  LANGUAGE java
  AS '
      if (val != null)
          state = state + val;
      return state;
  ';

CREATE AGGREGATE sum_counter ( counter )
  SFUNC agg_counter
  STYPE bigint
  INITCOND 0;

然后你会这样使用:

SELECT sum_counter(countercol) FROM table WHERE partition=1;

我试过在3.0.0-beta2和它的工作原理。它也应该在2.2中工作。

I tried that in 3.0.0-beta2 and it works. It should also work in 2.2.

在你尝试之前,请记住在cassandra.yaml中设置 enable_user_defined_functions:true 创建函数。

Just remember to set enable_user_defined_functions: true in cassandra.yaml before you try to create the function.

这篇关于Cassandra的计数器类型的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆