【问题标题】:Cassandra filter with ordering query modeling带有排序查询建模的 Cassandra 过滤器
【发布时间】:2018-08-09 12:36:24
【问题描述】:

我是 Cassandra 的新手,我正在尝试在 Cassandra 中为表格建模。我的查询如下所示

Query #1: select * from TableA where Id = "123"
Query #2: select * from TableA where name="test" orderby startTime DESC
Query #3: select * from TableA where state="running" orderby startTime DESC

我已经能够为查询 #1 构建表,看起来像

    val tableAStatement = SchemaBuilder.createTable("tableA").ifNotExists.
    addPartitionKey(Id, DataType.uuid).
    addColumn(Name, DataType.text).
    addColumn(StartTime, DataType.timestamp).
    addColumn(EndTime, DataType.timestamp).
    addColumn(State, DataType.text)

    session.execute(tableAStatement)

但是对于 Query#2 和 3,我尝试了很多不同的方法,但都失败了。每次,我都会陷入与 cassandra 不同的错误。

考虑到上述查询,正确的表模型应该是什么?对此类查询建模的正确方法是什么。

【问题讨论】:

    标签: scala cassandra


    【解决方案1】:

    查询 #2:select * from TableB where name="test"

    CREATE TABLE TableB (
        name text,
        start_time timestamp,
        PRIMARY KEY (text, start_time)
    ) WITH CLUSTERING ORDER BY (start_time DESC)
    

    查询 #3:select * from TableC where state="running"

    CREATE TABLE TableC (
        state text,
        start_time timestamp,
        PRIMARY KEY (state, start_time)
    ) WITH CLUSTERING ORDER BY (start_time DESC)
    

    在 cassandra 中,您可以围绕查询对表进行建模。需要数据非规范化和复制。注意聚类顺序 - 这样您可以在查询中省略“排序依据”

    【讨论】:

    • 谢谢。这就是我要找的。​​span>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-17
    • 1970-01-01
    • 2016-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多