cassandra数据建模分区键

dzhpxtsq  于 2021-06-15  发布在  Cassandra
关注(0)|答案(1)|浏览(414)

我的项目有两张Cassandra的table。

create table IF NOT EXISTS post_by_user
(topic_id varchar, 
post_id UUID,
title varchar, 
post_body varchar, 
user_id varchar,  
view_count int,
date_created date,
primary key (user_id, post_id))
WITH CLUSTERING ORDER BY (post_id DESC);

create table IF NOT EXISTS post_by_topic
(topic_id varchar, 
post_id UUID,
title varchar, 
post_body varchar, 
user_id varchar,  
view_count int,
date_created date,
primary key (topic_id, post_id))
WITH CLUSTERING ORDER BY (post_id DESC);

现在我想创建一个基于视图计数的顶部查看文章的表。因为可能有多个主题和用户,所以表的分区键应该是什么?材料化的观点有可能吗?
还有一种方法可以使分区键保持一个常量值。
请建议。。。

50few1ms

50few1ms1#

在上面的一个表上创建物化视图并不能帮助您计算总体排名靠前的文章。举个例子,如果我们在 post_by_user 如下所示,我们只能为每个用户计算top post:

CREATE MATERIALIZED VIEW toppostforuser AS
   SELECT post_id FROM post_by_user
   WHERE post_id IS NOT NULL AND user_id IS NOT NULL AND view_count IS NOT NULL AND date_created IS NOT NULL
   PRIMARY KEY (user_id, post_id, view_count, date_created)
   WITH CLUSTERING ORDER BY (view_count desc);

您需要一个分区键正好等于 post_id 在此基础上,可以创建一个物化视图来计算总体顶部查看的文章。
注意:如果你分开 date_created 分为单独的列,例如 year , month , day ,你也可以计算每日的热门帖子,每月的热门帖子等。

CREATE MATERIALIZED VIEW dailytoppostforuser AS
   SELECT post_id FROM post_by_user
   WHERE post_id IS NOT NULL AND user_id IS NOT NULL AND view_count IS NOT NULL AND year IS NOT NULL AND month IS NOT NULL AND day IS NOT NULL
   PRIMARY KEY ((user_id, year, month, day), post_id, view_count)
   WITH CLUSTERING ORDER BY (view_count desc);

参考:cassandra中的物化视图

相关问题