Cassandra格式值失败

时间:2019-02-24 09:20:56

标签: cassandra

我正在关注使用分组依据的文章:http://www.batey.info/cassandra-aggregates-min-max-avg-group.html

我具有以下功能并汇总

CREATE FUNCTION state_group_and_total( 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 = count + amount;
  state.put(type, count);
  return state; ' ;


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

但是当我运行以下命令select group_and_total(name,count) from ascore;时,出现错误Failed to format value OrderedMapSerializedKey([(u'gleydson', 4)]) : 'NoneType' object has no attribute 'sub_types'

我的模式是

CREATE TABLE ascore (
  name text,
  count int,
  id text,
  PRIMARY KEY(id)
)

1 个答案:

答案 0 :(得分:0)

博客文章中的So there's no group by keyword in Cassandra不再准确。您可以按分组进行分组并将聚合应用于该分组,例如,这使此操作变得容易得多:

CREATE TABLE scores (
  competition text,
  name text,
  run_date timestamp,
  score int,
  PRIMARY KEY ((competition), name, run_date));

INSERT INTO scores (competition, name, run_date , score ) VALUES ( 'week-12', 'user1', dateOf(now()), 2);
INSERT INTO scores (competition, name, run_date , score ) VALUES ( 'week-12', 'user1', dateOf(now()), 2);
INSERT INTO scores (competition, name, run_date , score ) VALUES ( 'week-12', 'user1', dateOf(now()), 4);
INSERT INTO scores (competition, name, run_date , score ) VALUES ( 'week-12', 'user2', dateOf(now()), 4);

SELECT name, sum(score) AS user_total FROM scores WHERE competition = 'week-12' GROUP BY competition, name;

 name  | user_total
-------+------------
 user1 |          8
 user2 |          4

请注意,您拥有的汇总函数确实适用于以上示例:

select group_and_total(name,score) from scores where competition = 'week-12';

 test.group_and_total(name, score)
----------------------------------------
               {'user1': 8, 'user2': 4}

更新与您的架构:

> INSERT INTO ascore (id, name, count) VALUES ('id1', 'bob', 2);
> INSERT INTO ascore (id, name, count) VALUES ('id2', 'alice', 1);
> INSERT INTO ascore (id, name, count) VALUES ('id3', 'alice', 1);
# even with a null
> INSERT INTO ascore (id, name) VALUES ('id4', 'alice');
> select group_and_total(name,count) from ascore;                                                                                                                                                            
 test.group_and_total(name, count)
----------------------------------------
                 {'alice': 2, 'bob': 2}

您使用的旧版本可能存在一些错误?