Cassandra数据模型设计用于通知功能

时间:2012-03-06 13:02:31

标签: model cassandra

需要一些帮助来设计通知的数据模型。所以,我想使用Cassandra,并有一个网页,我想为用户存储通知f.e. :

  • 收到
  • 的消息
  • 用户xyz投票了你的照片
  • 等...

用户必须获得最后通知的范围并删除单个通知。所以我需要一个最优的模式(50%的读取与50%的写入?)。

我的想法如下......(您怎么看?按时间戳排序的键怎么样):

notifications {
   john : {
       111-1123-3242-9202 : {type: 'newmail'; ...; timestamp: 321948293849}
       555-1123-aaac-ccc3 : {type: 'voted'; ...; timestamp: 321948293433}
   }

   anna : {...}

   ...
}

提前非常感谢!

汤姆

1 个答案:

答案 0 :(得分:4)

我假设“通知”是你的CF,“john”,“anna”等是行键。

假设每个通知的数据相对较小或永远不需要更新,我建议您使用时间戳作为列名,并将整个序列化通知(可能是json)放在列值中。这样可以非常有效地获取最后N个通知,并允许轻松删除单个通知。

使用pycassa,获取查询和删除可能如下所示:

def get_notifications_for(user):
    cols = notifications_cf.get(user, column_count=10)
    return map(json.loads, cols.values())

def delete_notification(user, notification_timestamp):
    notifications_cf.remove(user, columns=[notification_timestamp])

我假设您已将比较器设置为LongType(reversed=true),这意味着如果您使用列名称的时间戳,您的通知将按逆时间顺序存储。