【问题标题】:How can I without the 'allow filtering' to have the conditional query in CQL?如果没有“允许过滤”,如何在 CQL 中进行条件查询?
【发布时间】:2019-04-25 00:17:43
【问题描述】:

我是 CQL 的新手。 我有(餐厅)表。 通过实施“描述餐厅”,它将信息显示在 restaurants table information

如何在不使用“允许过滤”的情况下运行以下查询, select name from restaurants where borough = 'brooklyn';

为了能够在没有“允许过滤”的情况下运行查询,有哪些必要步骤?

非常感谢。

【问题讨论】:

    标签: cassandra cql


    【解决方案1】:

    使用 Cassandra (CQL),您需要采用基于查询的建模方法。这意味着您需要一个专门为borough 提供查询服务的表。

    首先要问一个好问题,为什么(在您当前的表设计中)id 是您唯一的主键?您是否必须支持id 的查询?如果没有,那么你不应该需要那张桌子。但是,由于 borough 不是唯一的,因此将其他列聚集到主键定义中是有意义的。

    我会选择这样的:

    CREATE TABLE stackoverflow.restaurants_by_borough (
        borough text,
        name text,
        id int,
        buildingnum text,
        cuisinetype text,
        phone text,
        street text,
        zipcode int,
        PRIMARY KEY (borough, name, id)
    ) WITH CLUSTERING ORDER BY (name ASC, id ASC);
    

    现在这个查询可以工作了:

    select name from restaurants_by_borough
    where borough = 'brooklyn';
    

    它将返回每个行政区的数据,按name 排序。另外,如果任意两家餐厅有相同的name(布鲁克林可能不止一家麦当劳),则在键的末尾添加id 以确保唯一性。

    【讨论】:

      猜你喜欢
      • 2013-09-12
      • 1970-01-01
      • 1970-01-01
      • 2019-10-30
      • 1970-01-01
      • 2016-08-14
      • 2021-09-10
      • 1970-01-01
      • 2014-01-27
      相关资源
      最近更新 更多