【问题标题】:Efficient query to count and calculate average in Cassandra在 Cassandra 中计算和计算平均值的高效查询
【发布时间】:2017-03-25 23:50:35
【问题描述】:

我有一个表datavalue,其中每个client 的数据在eventtime 15 分钟的间隔内几乎一年。我想创建另一个表datavalue_by_hour,我将在其中以 1 小时的间隔存储来自datavalue 的数据。为此,我需要:

  1. 获取clienteventhour 的不同值;
  2. 为上述结果的每一行计算clienteventhouravg(activepower)
  3. 计算上述平均函数中使用的activepower 值的数量。

问题是,在 mysql 或 postgres 中,几乎只需一条指令即可轻松完成。使用 Cassandra,我想唯一的方法是遍历每个结果并应用另一个查询,直到获得最终结果..?这似乎效率很低,有没有其他方法可以更有效地实现这样的结构?

AFH0AEE00A0BHC  2016-05-24 18:00:00+0000    0.067   4
AFH0AEE00AGCEC  2016-05-24 19:00:00+0000    0.081   4
AFH0ADE0ACDAAE  2016-05-24 20:00:00+0000    0.068   3
AFH0AEE00AGFEC  2016-05-24 21:00:00+0000    0.032   4

我的初始数据值表有这样的结构:

CREATE TABLE datavalue (
client text,
eventhour bigint,
eventtime timestamp,
activepower double,
activepowerclassification double,
dstoffset double,
PRIMARY KEY (( pt, eventhour ), eventtime));

以及datavalue 表中的数据集示例(+10000000 行):

AFH0AEE00BFEFB  1473847200  2016-09-14 10:00:00+0000    0.042   0.0 1.0
AFH0AEE00BFEFB  1473847200  2016-09-14 10:15:00+0000    0.056   0.0 1.0
AFH0AEE00BFEFB  1473847200  2016-09-14 10:30:00+0000    0.075   0.0 1.0
AFH0AEE00BFEFB  1473847200  2016-09-14 10:45:00+0000    0.102   0.0 1.0
A0C0AEAFIFEAHE  1472882400  2016-09-03 06:00:00+0000    0.0 0.0 1.0
A0C0AEAFIFEAHE  1472882400  2016-09-03 06:15:00+0000    0.0 0.0 1.0
A0C0AEAFIFEAHE  1472882400  2016-09-03 06:30:00+0000    0.0 0.0 1.0
A0C0AEAFIFEAHE  1472882400  2016-09-03 06:45:00+0000    0.0 0.0 1.0
AFH0ACFB00CD0F  1452693600  2016-01-13 14:00:00+0000    0.244   0.0 0.0
AFH0ACFB00CD0F  1452693600  2016-01-13 14:15:00+0000    0.244   0.0 0.0
AFH0ACFB00CD0F  1452693600  2016-01-13 14:30:00+0000    0.242   0.0 0.0
AFH0ACFB00CD0F  1452693600  2016-01-13 14:45:00+0000    0.244   0.0 0.0
A0C0AEAFIEE0DC  1466319600  2016-06-19 07:00:00+0000    0.036   0.0 1.0

【问题讨论】:

    标签: php cassandra


    【解决方案1】:

    更新您的架构添加 dstCount、dstSum 和 dstAvg:

    CREATE TABLE datavalue (
        client text,
        eventhour bigint,
        eventtime timestamp,
        activepower double,
        activepowerclassification double,
        dstoffset double,
        powerCount bigint static,
        powerSum double static,
        powerAvg double static,
    PRIMARY KEY (( client, eventhour ), eventtime));
    

    我用的是静态列,这里是文档

    静态列是由同一分区的所有行共享的特殊列。让我们举个例子:假设我们想要存储每个用户需要支付的账单,并保留每个用户需要支付的余额。我们要保持的不变式是余额始终是所有未付账单的总和:

    在插入新记录之前,使用此查询选择特定客户端和事件小时的当前 powerCount 和 powerSum

    让你在插入记录

    client          eventhour   eventtime                  activepower                  
    AFH0AEE00BFEFB  1473847200  2016-09-14 10:00:00+0000   0.040
    

    查询将是

     SELECT powerCount,powerSum 
       FROM datavalue 
     WHERE client = 'AFH0AEE00BFEFB' AND eventhour = 1473847200 LIMIT 1;
    

    现在你得到了当前的 powerCount 和 powerSum 让powerCount = 4 and powerSum = 0.275
    所以更新数据

    powerSum = powerSum + 0.040 = 0.315
    powerCount = powerCount + 1  = 5  
    powerAvg = powerSum/powerCount =  0.063
    

    现在插入记录以及新的 powerSum、powerCount 和 powerAvg

    【讨论】:

    • 看起来不错!我会尝试应用它并让你知道结果!非常感谢
    • 试试这个让我知道
    猜你喜欢
    • 1970-01-01
    • 2020-02-15
    • 1970-01-01
    • 1970-01-01
    • 2019-07-06
    • 1970-01-01
    • 1970-01-01
    • 2012-02-03
    • 1970-01-01
    相关资源
    最近更新 更多