【问题标题】:Cassandra data modeling suggestionCassandra 数据建模建议
【发布时间】:2018-04-04 12:33:18
【问题描述】:
CREATE TABLE IF NOT EXISTS myns.mytable (
"id" text, -- unique id,
"inserted" timestamp,
"score" int, -- score
PRIMARY KEY (("id","score"), "inserted"))
WITH CLUSTERING ORDER BY (inserted asc);

您认为有更好的方法来对此进行建模吗?我要查询 我的表在:

1. find by id - #use ALLOW FILTERING error
2. find by score - #use ALLOW FILTERING error
3. find by id and score - works

我只想擅长查询表以通过 id 或分数范围或两者来获取对象。你认为我应该在分数上创建一个二级索引吗?

【问题讨论】:

    标签: cassandra cassandra-3.0


    【解决方案1】:

    当您将 "id" 和 "score" 都放在 分区键 中时,您始终需要同时提供 (id, score) 价值观。

    在 Cassandra 上进行数据建模时,我们始终优先考虑要从该表中进行的查询。并根据查询对表进行建模。在您的情况下,您需要通过 (id)(score)(id, score) 进行查询。因此,如果您通过保持id 作为主键score 作为集群键 来更改表定义会更好。这样您就可以通过 idscore 以及两者的 id-score 组合进行查询。

    CREATE TABLE IF NOT EXISTS myns.mytable (
    "id" text, -- unique id,
    "inserted" timestamp,
    "score" int, -- score
    PRIMARY KEY ("id","score", "inserted"))
    WITH CLUSTERING ORDER BY (inserted asc); 
    

    现在您可以查询以下所有内容:

    1. 按 id 查找 - #use ALLOW FILTERING
    2. 按分数查找 - #use ALLOW FILTERING
    3. 按 id 和分数查找

    【讨论】:

    • 如何只按分数查询?例如:从 mytable 中选择 *,其中 score > 10
    • SELECT * from mytable WHERE score > 10 ALLOW FILTERING 应该可以工作。 @user1908559
    猜你喜欢
    • 2011-01-29
    • 2015-11-16
    • 2015-12-25
    • 2015-04-09
    • 2019-09-02
    • 2018-06-16
    • 2018-07-10
    • 1970-01-01
    相关资源
    最近更新 更多