Cassandra时间序列数据建模

时间:2014-09-24 06:26:01

标签: database-design cassandra data-modeling

我现在一直在研究分析系统的数据模型,但我似乎无法为我的主键获得正确的设置。我已经观看了一系列视频(https://www.youtube.com/watch?v=UP74jC1kM3w&list=PLqcm6qE9lgKJoSWKYWHWhrVupRbS8mmDA&index=9),以获得有关最佳实践的一些知识,特别是有关时间序列数据的知识。

关于PRIMARY KEYS,我似乎无法获得正确的平衡,以便我可以查询我需要的方式。

到目前为止,这是我的数据模型:

CREATE TABLE eventPropertyCountsByDay (
    user_id int,
    event_type varchar,
    property varchar,
    value varchar,
    date_to_day varchar,
    count counter,
    PRIMARY KEY ((event_type, user_id), date_to_day, property, value)
) WITH CLUSTERING ORDER BY (date_to_day DESC, property DESC, value DESC);

我将事件存储在此表(列族)中的另一个表和事件属性中。

我需要能够根据用户ID进行查询,使用IN查询一次获取多个用户的记录,但我还需要查询属性和值字段,以及指定日期范围。

以下是我正在尝试实现的查询示例:

SELECT * FROM eventPropertyCountsByWeek 
WHERE event_type = 'some_event' 
AND date_to_day > '2014-09-24'
AND user_id IN (123, 456) 
AND property = 'property_name' 
AND value = 'property_value'

如何完成此类查询?我需要介绍什么样的其他列系列来分解它?

1 个答案:

答案 0 :(得分:2)

试试这个:

CREATE TABLE eventPropertyCountsByDay (
  user_id int,
  event_type varchar,
  property varchar,
  value varchar,
  date_to_day int, // day number
  count counter,
  PRIMARY KEY ((event_type, user_id), property, value, date_to_day)
) WITH CLUSTERING ORDER BY (property DESC, value DESC, date_to_day DESC);

我在clustering键的末尾移动了date_to_day,使其可用于具有固定属性和值的范围查询。

数据更新查询:

update eventPropertyCountsByDay set count = count + 1 where 
  user_id=1 and 
  event_type='log' and 
  property='prop1' and 
  value='val1' and 
  date_to_day=54321;

选择查询:

select * from eventPropertyCountsByDay 
  where event_type='log' and 
    user_id=1 and 
    property='prop1' and
    value='val1' and 
    date_to_day > 54300;

 event_type | user_id | property | value | date_to_day | count
------------+---------+----------+-------+-------------+-------
        log |       1 |    prop1 |  val1 |       54323 |     2
        log |       1 |    prop1 |  val1 |       54321 |     1
相关问题