【问题标题】:How to apply limit for each unique composite column value in cassandra?如何对 cassandra 中的每个唯一复合列值应用限制?
【发布时间】:2016-01-01 22:46:21
【问题描述】:

我有一个这样的示例表结构:

CREATE TABLE testcomposite ( day text, name text, lpt varint, details text, PRIMARY KEY (day, name, lpt) )

我有这样的数据:

cqlsh:KS> select * from testcomposite;

day | name | lpt | details ------+---------+---------+-------- day1 | name1 | 10 | abcdef day1 | name1 | 11 | abcdef day1 | name1 | 21 | abcdef day1 | name2 | 10 | abcdef day1 | name2 | 11 | abcdef

是否可以查询得到这样的结果,其中每一行都包含唯一的 name 字段,并且具有最高的 lpt 值?

day | name | lpt | details ------+---------+---------+-------- day1 | name1 | 21 | abcdef day1 | name2 | 11 | abcdef

【问题讨论】:

标签: cassandra cql composite-key


【解决方案1】:
    CREATE FUNCTION state_group_and_max( state map<text, int>, type text, amount int )
    CALLED ON NULL INPUT
    RETURNS map<text, int>
    LANGUAGE java AS '
    Integer count = (Integer) state.get(type);  if (count == null) count = amount; else count = Math.max(count, amount); state.put(type, count); return state; ' ;


    CREATE OR REPLACE AGGREGATE group_and_max(text, int) 
    SFUNC state_group_and_max 
    STYPE map<text, int> 
    INITCOND {};

cqlsh:test> select group_and_max(name,lpt) from testcomposite where day = 'day1';

 test.group_and_max(name, lpt)
-------------------------------
    {'name1': 21, 'name2': 11}

【讨论】:

  • 感谢您的回答,但我的 cassandra 版本不支持 UDF。我打算重新设计数据模型。看起来我们只能从左到右一一在复合列上应用条件。
猜你喜欢
  • 1970-01-01
  • 2019-01-21
  • 2016-12-13
  • 2015-06-04
  • 2020-11-17
  • 2016-11-01
  • 2021-10-17
  • 2018-08-05
  • 1970-01-01
相关资源
最近更新 更多