如何按cassandra中的上次更新日期对数据进行排序?

c8ib6hqw  于 2021-06-09  发布在  Cassandra
关注(0)|答案(1)|浏览(281)

我需要一些建议来正确设计Cassandra的table。我需要一份所有书的分类清单。排序按上次更新的日期执行。每次购买一本书时 number_of_buyers 列已更新。另外,我需要更新 updated_at 列。问题在于 updated_at 列是 clustering key 哪一部分是 primary key . 无法更新属于主键的列中的值。

create table books (
   book_id uuid,
   created_at timestamp,
   updated_at timestamp,
   book_name varchar,
   book_author varchar,
   number_of_buyers int,
   primary key (book_id, updated_at)
) with clustering order by (updated_at desc);

另一个例子:

create table chat_rooms (
   chat_room_id uuid,
   created_at timestamp,
   updated_at timestamp,
   last_message_content varchar,
   last_message_author varchar,
   unread_messages_number int,
   primary key (chat_room_id, updated_at)
) with clustering order by (updated_at desc);

每个聊天室都有最新消息。这些信息总是在变化。如果有变化,我想把聊天室放在首位。许多信使的典型行为。

rpppsulh

rpppsulh1#

当然是这样;你需要在不同的地方划分。诀窍是在避免未绑定分区增长的同时,找到查询灵活性的适当平衡(您在这里的明显需求)。
对于 books 表,有没有可能在 category ? 你知道,像恐怖,幻想,图画小说,非小说,教学,等等。。?

CREATE TABLE book_events (
   book_id uuid,
   created_at timestamp,
   updated_at timestamp,
   book_name varchar,
   book_author varchar,
   number_of_buyers int,
   category text,
   PRIMARY KEY (category, book_name, updated_at, book_id)
) WITH CLUSTERING ORDER BY (book_name ASC,updated_at DESC,book_id ASC);

对于主键定义,我们可以在 category ,然后群集 book_name 以及 updated_at ,与 book_id 最后(为了独特性)。那么, INSERT 为每个销售活动创建一个新行。在查询中(插入几行之后),使用 MAX 上的聚合 updated_at 在使用 GROUP BY 条款 book_name .

SELECT book_name,book_author,number_of_buyers,MAX(updated_at) FROm book_events 
 WHERE category='Computers & Technology' GROUP BY book_name;

 book_name                       | book_author                                                | number_of_buyers | system.max(updated_at)
---------------------------------+------------------------------------------------------------+------------------+---------------------------------
  Mastering Apache Cassandra 3.x |                                Aaron Ploetz, Teja Malepati |               52 | 2020-10-05 14:29:33.134000+0000
 Seven NoSQL Databases in a Week | Aaron Ploetz, Devram Kandhare, Brian Wu, Sudarshan Kadambi |              163 | 2020-10-05 14:29:33.142000+0000

(2 rows)

唯一需要考虑的是如何处理过时的销售行。当然,根据写入频率的不同,您可以随时删除它们。最理想的解决方案是考虑销售节奏,并应用ttl。
这个解决方案肯定不完整,但我希望它能把你引向正确的方向。

相关问题