我正在Apache Cassandra中设计聊天应用程序的数据库架构,我无法解决这个问题。
到目前为止,这是我的架构:
CREATE TABLE users(
user_id bigint,
nickname text,
email text,
chat_rooms set<timeuuid>,
PRIMARY KEY(user_id)
);
CREATE TABLE rooms(
room_id timeuuid,
creation_date timestamp,
creator_id bigint,
participants set<bigint>,
PRIMARY KEY(room_id)
);
CREATE TABLE messages(
message_id timeuuid,
room_id timeuuid,
author_id bigint,
time_bucket int,
content text,
PRIMARY KEY((room_id, time_bucket), message_id)
) WITH CLUSTERING ORDER BY (message_id DESC);
我希望按照用户上次回复时间排序的ID来获取房间列表,类似于Facebook Messenger和Telegram。
我在考虑向last_reply_time
添加新列rooms
,将其用作群集密钥,并在房间中有新消息时更新。但是,无法在Cassandra中更新聚类键值。我应该如何对此进行建模?
我查看了他们的Cassandra实施中的KillrChat example和Discord's wonderful piece,但他们没有提到与我的问题相关的任何内容。
提前致谢!
答案 0 :(得分:2)
你可以有一个这样的表
create table test (
creator_id bigint ,
room_id timeuuid ,
last_reply_time timestamp,
primary KEY ((creator_id), room_id))
with CLUSTERING ORDER BY
(room_id ASC );
您可以将所有last_reply_time插入其中并使用
选择您的数据select * from test where creator_id = ? and room_id = ?
您将只执行将更新数据库中相同条目的插入内容,因为您将拥有相同的creator_id和room_id。