【问题标题】:Querying in cassandra cql在 cassandra cql 中查询
【发布时间】:2019-05-18 19:13:48
【问题描述】:

我是 cassandra 的新手。假设我有一个名为 customer 的表,其中字段 id 作为主键、年龄、名字和姓氏。也采取它,因为该表中有很多数据。如何在该表中查询以获取我的 senario,例如“搜索查询。即在 senarios 下获取的 select 语句。”

  1. 同时具有 firstname="something" 和 last name="something" 的数据,即满足两者的查询
  2. 如果 firstname=null 和 lastname='something we given'
  3. 如果 lasttname=null 和 firstname'something we given'

我们如何使用一条同时满足三个条件的 cql 查询语句进行查询。

有什么办法吗,.....

【问题讨论】:

    标签: cassandra-3.0 cql3 cqlsh


    【解决方案1】:

    您无法通过一次查询获得结果。它与 MySQL 等 RDBMS 有很大不同。有一些主要规则。

    1. 第一条基本规则是,如果未提及字段,则不能查询 在主键中。为了能够使用非主字段进行查询,您必须提到 ALLOW FILTERING ,这是不推荐的,并且首先违背了使用 C* 进行更快读取的目的。您可以在非主列上使用二级索引,但有一些权衡(您必须知道在哪里使用它)。

    2. 第二条规则如果有多个主键,你必须提到 WHERE 子句中的字段顺序。不能通过聚类查询 没有提及分区键的键或无法通过 2nd 查询 没有提及分区和第一个集群键的集群键 在 WHERE 子句中等等。 (我假设你已经学习了基本的东西并且知道术语 partitionclustering key)。

    3. 主键不能为 NULL

    我在下面给你一个例子。

    create table users(
        id text,
        firstname text,
        lastename text,
        primary key(id)
    );
    

    如果您的架构如上所示,那么您只能通过 'id' 进行查询。

    select * from users where id = ?;
    

    您不能按名字或姓氏进行查询(它们没有作为键被提及)。

    create table users(
        id text,
        firstname text,
        lastename text,
        primary key(firstname, id)
    );
    

    这里可以通过firstnamefirstnameid查询。

    select * from users where firstname = ? ;
    select * from users where firstname = ? and id = ?;
    

    第一个查询满足您使用名字搜索的第二个要求。 (与此相同,您需要另一个表,其中姓氏作为您的第三个要求的分区键。) 您不能仅通过 id 转义 firstname 进行查询。 通过使用ALLOW FILTERING,您可以通过id进行查询。

    select * from users where id = ?允许过滤;
    create table users(
        id text,
        firstname text,
        lastename text,
        primary key(firstname, lastename, id)
    );
    

    这里可以通过firstnamelastnameid查询。

    select * from users where firstname = ?;
    select * from users where firstname = ? and lastname = ?;
    select * from users where firstname = ? and lastname = ? and id = ?;
    

    第二个查询满足您的第一个要求。

    总结:您需要至少 3 个表并将所有数据非规范化/复制到 3 个表中以提供查询。您只能使用一个表并索引适合它的其他字段,但性能会降低。物化视图(每个查询的表)比二级索引快得多。最重要的是,在这里您只能搜索完全匹配。部分匹配(like 或 MySQL 中的 % 关键字)在这里不起作用。了解分布式特性及其支持上述数据模型的内部架构非常重要。

    【讨论】:

      猜你喜欢
      • 2015-03-10
      • 1970-01-01
      • 2014-11-15
      • 1970-01-01
      • 2019-05-04
      • 1970-01-01
      • 2013-08-14
      • 2013-10-14
      • 2013-01-02
      相关资源
      最近更新 更多