在Cassandra / wide行中混合列类型

时间:2014-07-04 12:22:55

标签: cassandra normalization cql cql3 denormalization

我正在尝试学习如何在cassandra中实现一个feed(想想推特)。我想使用宽行来存储用户发布的所有帖子。 我正在考虑在同一行添加用户信息或统计信息(帖子数量,上次发布日期,用户名等)。

我的问题是:姓名,年龄等等。"字段名称"列存储?或者那些宽行只存储指定的列名和值?我在浪费磁盘空间吗?我是否以某种方式妥协了表现?

谢谢!

- 表创建

CREATE TABLE user_feed (
    owner_id int,
    name text,
    age int,
    posted_at timestamp,
    post_text text,
    PRIMARY KEY (owner_id, posted_at)
);

- 插入用户

insert into user_feed (owner_id, name, age, posted_at) values (1, 'marc', 36, 0);

- 插入用户位置

insert into user_feed (owner_id, posted_at, post_text) values (1, dateof(now()), 'first post!');
insert into user_feed (owner_id, posted_at, post_text) values (1, dateof(now()), 'hello there');
insert into user_feed (owner_id, posted_at, post_text) values (1, dateof(now()), 'i am kind of happy');

- 吃饭

select * from user_feed where owner_id=1 and posted_at>0;

- 结果

 owner_id | posted_at                | age  | name | post_text
----------+--------------------------+------+------+--------------------
        1 | 2014-07-04 12:01:23+0000 | null | null |        first post!
        1 | 2014-07-04 12:01:23+0000 | null | null |        hello there
        1 | 2014-07-04 12:01:23+0000 | null | null | i am kind of happy

- 获取用户信息 - 仅用户信息POSTED_AT = 0

select * from user_feed where owner_id=1 and posted_at=0;

- 结果

 owner_id | posted_at                | age  | name | post_text
----------+--------------------------+------+------+--------------------
        1 | 1970-01-01 00:00:00+0000 |   36 | marc |               null

1 个答案:

答案 0 :(得分:3)

如何让它们保持静止?

静态列在所有分区键中都是相同的,因为您的分区键是所有者的ID,所以可以避免浪费空间并在任何查询中检索用户信息。

CREATE TABLE user_feed (
    owner_id int,
    name text static,
    age int static,
    posted_at timestamp,
    post_text text,
    PRIMARY KEY (owner_id, posted_at)
);

干杯, 卡罗