卡桑德拉时间序列数据建模

时间:2017-05-22 14:31:55

标签: cassandra time-series data-modeling

我的表格定义如下

create table events (offset int,key varchar, user uuid,name varchar, 
emitted int, day int, month int, year int,PRIMARY KEY((offset), year, month, 
day) ) 
WITH CLUSTERING  ORDER BY (year DESC, month DESC, day DESC);

其中user是提交活动的用户的uuid, offset是唯一的,但用户不是因为用户有很多事件。 我想选择给定用户和日期范围的事件,我该如何完成此

1 个答案:

答案 0 :(得分:1)

您需要创建实体化视图或其他表格

如果您使用cassandra> = 3.x,则可以创建物化视图

CREATE MATERIALIZED VIEW events_by_user AS
    SELECT user, year, month, day, offset
    FROM events
    WHERE user IS NOT NULL AND year IS NOT NULL AND month IS NOT NULL AND day IS NOT NULL AND offset IS NOT NULL
    PRIMARY KEY (user, year, month, day, offset)
    WITH CLUSTERING ORDER BY (year DESC, month DESC, day DESC, offset ASC);

或者如果您使用较低版本,则创建另一个表:

CREATE TABLE events_by_user (
   user uuid, 
   offset int, 
   day int, 
   month int, 
   year int, 
   primary key(user,  year, month, day, offset)
);

现在您可以按用户查询日期范围,即:

SELECT * FROM events_by_user WHERE user = 6d5c6400-3f04-11e7-b92e-371a840aa4bb AND (year, month, day) >= (2017,05,20) AND (year, month, day) <= (2017,05,25);