简体   繁体   English

Cassandra CQL-按时间排序唯一ID

[英]Cassandra cql - sorting unique IDs by time

I am writting messaging chat system, similar to FB messaging. 我正在编写类似于FB消息传递的消息传递聊天系统。 I did not find the way, how to effectively store conversation list (each row different user with last sent message most recent on top). 我没有找到有效存储对话列表的方法(每行不同的用户,最近发送的消息排在最前面)。 If I list conversations from this table: 如果我从该表中列出对话:

CREATE TABLE "conversation_list" (
    "user_id" int,
    "partner_user_id" int,
    "last_message_time" time,
    "last_message_text" text,
    PRIMARY KEY ("user_id", "partner_user_id")
)

I can select from this table conversations for any user_id . 我可以从此表中为任何user_id选择对话。 When new message is sent, we can simply update the row: 发送新消息时,我们可以简单地更新行:

UPDATE conversation_list SET last_message_time = '...', last_message_text='...' WHERE user_id = '...' AND partner_user_id = '...'

But it is sorted by clustering key of course. 但它当然是按聚类键排序的。 My question: How to create list of conversations, which is sorted by last_message_time, but partner_user_id will be unique for given user_id? 我的问题:如何创建对话列表(按last_message_time排序),但是partner_user_id对于给定的user_id将是唯一的?

If last_message_time is clustering key and we delete the row and insert new (to keep partner_user_id unique), I will have many so many thumbstones in the table. 如果last_message_time是集群键,并且我们删除该行并插入new(以保持partner_user_id唯一),则表中将有很多经验教训。

Thank you. 谢谢。

A slight change to your original model should do what you want: 对原始模型稍作更改即可满足您的要求:

CREATE TABLE conversation_list (
    user_id int,
    partner_user_id int,
    last_message_time timestamp,
    last_message_text text,
    PRIMARY KEY ((user_id, partner_user_id), last_message_time)
) WITH CLUSTERING ORDER BY (last_message_time DESC);

I combined " user_id " and " partner_user_id " into one partition key. 我将“ user_id ”和“ partner_user_id ”组合到一个分区键中。 " last_message_time " can be the single clustering column and provide sorting. last_message_time ”可以是单个群集列并提供排序。 I reversed the default sort order with the CLUSTERING ORDER BY to make the timestamps descending. 我使用CLUSTERING ORDER BY反转了默认的排序顺序,以使时间戳降序。 Now you should be able to just insert any time there is a message from a user to a partner id. 现在,您应该可以在任何时候从用户到合作伙伴ID的消息中插入内容。

The select now will give you the ability to look for the last message sent. 现在,选择将使您能够查找最后发送的消息。 Like this: 像这样:

SELECT last_message_time, last_message_text
FROM conversation_list
WHERE user_id= ? AND partner_user_id = ?
LIMIT 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM