Cassandra数据建模分区键

时间:2018-09-26 05:38:57

标签: cassandra data-modeling

我的项目在cassandra中有两个表。

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);

现在,我想基于view_count为顶部查看的帖子创建一个表格。 该表的分区键应该是什么,因为可能有多个主题和用户?有可能出现实物视图吗?

还有一种方法可以使分区键保持恒定值。

请建议...

1 个答案:

答案 0 :(得分:1)

在上面的一张表上创建物化视图不能帮助您计算总体上顶视图的帖子。作为说明,如果我们按如下方式在post_by_user上创建实例化视图,则只能为每个用户计算最高职位:

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分成单独的列,例如yearmonthday,则还可以计算每日最高-帖子,每月热门帖子等。

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);

参考:Materialized views in Cassandra