【问题标题】:Cassandra Time-Series data modellingCassandra 时间序列数据建模
【发布时间】:2014-11-18 12:54:40
【问题描述】:

我为分析系统研究数据模型已经有一段时间了,但我似乎无法为我的主键设置正确的设置。我观看了很多视频 (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'

我怎样才能完成这种查询?我可能需要引入哪些其他列族来分解它?

【问题讨论】:

  • 在我们确切了解您需要多少表之前,我们应该先讨论一下查询。你想在应用程序方面支持什么?您按天和按周计算。这些是您要支持的典型查询还是唯一查询?
  • @PatrickMcFadin 嘿,伙计!我已经为我的答案准备了一个快速的谷歌文档。 docs.google.com/document/d/…你的邮箱是什么?

标签: database-design cassandra data-modeling


【解决方案1】:

试试这个:

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);

我将 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

【讨论】:

    猜你喜欢
    • 2016-08-29
    • 2017-06-23
    • 2013-08-02
    • 1970-01-01
    • 2015-09-21
    • 2013-04-17
    • 2013-07-13
    相关资源
    最近更新 更多