【问题标题】:Cassandra filter based on secondary index基于二级索引的 Cassandra 过滤器
【发布时间】:2015-11-29 14:13:47
【问题描述】:

我们已经使用 Cassandra 有一段时间了,我们正在尝试获得一个真正优化的表,该表将能够快速查询和过滤大约 100k 行。

我们的模型看起来像这样:

class FailedCDR(Model):  
    uuid = columns.UUID(partition_key=True, primary_key=True)
    num_attempts = columns.Integer(index=True)
    datetime = columns.Integer()

如果我描述该表,它清楚地表明num_attempts 是索引。

CREATE TABLE cdrs.failed_cdrs (
    uuid uuid PRIMARY KEY,
    datetime int,
    num_attempts int
) WITH bloom_filter_fp_chance = 0.01
    AND caching = '{"keys":"ALL", "rows_per_partition":"NONE"}'
    AND comment = ''
    AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy'}
    AND compression = {'sstable_compression': 'org.apache.cassandra.io.compress.LZ4Compressor'}
    AND dclocal_read_repair_chance = 0.1
    AND default_time_to_live = 0
    AND gc_grace_seconds = 864000
    AND max_index_interval = 2048
    AND memtable_flush_period_in_ms = 0
    AND min_index_interval = 128
    AND read_repair_chance = 0.0
    AND speculative_retry = '99.0PERCENTILE';
CREATE INDEX index_failed_cdrs_num_attempts ON cdrs.failed_cdrs (num_attempts);

我们希望能够运行类似这样的过滤器:

failed = FailedCDR.filter(num_attempts__lte=9)

但是会发生这种情况:

QueryException: Where clauses require either a "=" or "IN" comparison with either a primary key or indexed field

我们怎样才能完成类似的任务?

【问题讨论】:

    标签: python django python-2.7 cassandra


    【解决方案1】:

    如果要在 CQL 中进行范围查询,则需要该字段为聚类列。

    因此,您会希望 num_attempts 字段成为集群列。

    此外,如果您想执行单个查询,则需要在同一个分区(或可以使用 IN 子句访问的少量分区)中查询所有行。由于您只有 100K 行,因此小到可以放入一个分区。

    所以你可以这样定义你的表:

    CREATE TABLE test.failed_cdrs (
        partition int,
        num_attempts int,
        uuid uuid,
        datetime int,
        PRIMARY KEY (partition, num_attempts, uuid));
    

    您可以使用分区键的常量插入数据,例如 1。

    INSERT INTO failed_cdrs (uuid, datetime, num_attempts, partition)
        VALUES ( now(), 123, 5, 1);
    

    然后你可以像这样进行范围查询:

    SELECT * from failed_cdrs where partition=1 and num_attempts >=8;
    

    此方法的缺点是要更改 num_attempts 的值,您需要删除旧行并插入新行,因为您不允许更新关键字段。您可以在批处理语句中执行删除和插入操作。

    Cassandra 3.0 中将提供的一个更好的选择是创建一个具有 num_attempts 作为集群列的物化视图,在这种情况下,Cassandra 会在您更新基表中的 num_attempts 时为您处理删除和插入操作。 3.0 版本目前处于 beta 测试阶段。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-10
      • 2013-09-02
      • 2016-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多