在设计通知数据模型方面需要一些帮助。所以,我想使用 Cassandra,并有一个网页,我想在其中存储用户 fe 的通知:
用户必须获取最后通知的范围并删除单个通知。所以我需要一个最佳模式(50% 的读取 vs. 50% 的写入??)。
我的想法如下......(你怎么看?按时间戳排序的键怎么样):
notifications {
john : {
111-1123-3242-9202 : {type: 'newmail'; ...; timestamp: 321948293849}
555-1123-aaac-ccc3 : {type: 'voted'; ...; timestamp: 321948293433}
}
anna : {...}
...
}
Run Code Online (Sandbox Code Playgroud)
真的提前致谢!
汤姆
我假设“通知”是您的 CF,而“约翰”、“安娜”等是行键。
假设每个通知的数据相对较小或从不需要更新,我建议您使用时间戳作为列名,并将整个序列化通知(可能是 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])
Run Code Online (Sandbox Code Playgroud)
我在这里假设您已将比较器设置为LongType(reversed=true),这意味着如果您对列名称使用时间戳,则您的通知将按时间倒序存储。