【问题标题】:cassandra cql query with in condition in rowkey and in clustering columnscassandra cql 查询在行键和集群列中具有条件
【发布时间】:2013-10-17 19:24:10
【问题描述】:

我是 cassandra 的新手。我有一个带有复合主键的表。表的描述是

CREATE TABLE testtable (
  foid bigint,
  id bigint,
  severity int,
  category int,
  ack boolean,
  PRIMARY KEY (foid, id, severity, category)
) WITH
  bloom_filter_fp_chance=0.010000 AND
  caching='KEYS_ONLY' AND
  comment='' AND
  dclocal_read_repair_chance=0.000000 AND
  gc_grace_seconds=864000 AND
  index_interval=128 AND
  read_repair_chance=0.100000 AND
  replicate_on_write='true' AND
  populate_io_cache_on_flush='false' AND
  default_time_to_live=0 AND
  speculative_retry='NONE' AND
  memtable_flush_period_in_ms=0 AND
  compaction={'class': 'SizeTieredCompactionStrategy'} AND
  compression={'sstable_compression': 'LZ4Compressor'};

我的要求是我需要用foidid 用范围条件和severity 用in 条件查询表

所以当我尝试以下查询时

select * from testtable where foid in (5,6) and id>10 and severity in (5);

我收到了错误消息

select * from testtable where foid in (5,6) and id>10 and severity in (5);

甚至severity 列上的相等条件对我来说就足够了,但这也行不通。

有没有什么方法可以做到这一点

我也尝试使用 severitycategory 的二级索引,但这并没有给我带来任何积极的影响。

【问题讨论】:

    标签: cassandra cql


    【解决方案1】:

    您需要依次限制主键,因此以下将起作用:

    select * from testtable where foid in (1) and id=2 and severity<20 ;
    

    但这不会:

    select * from testtable where foid in (1) and id>10 and severity=3;
    

    如何减少查询的限制(如您在问题中建议的那样),如下所示

    select * from testtable where foid in (5,6) and id>10
    

    然后在客户端对结果进行排序?

    另一种(可能更有吸引力)的解决方案是根据您将如何执行查询来排序您的密钥,例如,

    CREATE TABLE testtable2 (
      foid bigint,
      severity int,
      id bigint,
      category int,
      ack boolean,
      PRIMARY KEY (foid, severity, id, category)
    )
    

    允许您进行这样的查询(注意severity 上的相等操作,severity 上的 IN 操作将不起作用):

    select * from testtable2 where foid in (5,6) and severity=5 and id>10;
    

    (使用 cql 测试 [cqlsh 4.0.1 | Cassandra 2.0.1 | CQL 规范 3.1.1 | Thrift 协议 19.37.0])

    【讨论】:

    • 我也想过和你描述的一样的场景。而且最后一个解决方案也适用于某些场景。由于包含过滤的严重性列是按需进行的,并且是动态的。在某些情况下,我不会使用严重性列。无论如何,正如你所说,我正在尝试执行限制 foid 和 id 的查询,并在客户端进行排序以进行剩余过滤
    猜你喜欢
    • 2015-08-09
    • 2016-07-21
    • 2019-05-18
    • 2016-08-02
    • 2017-07-15
    • 1970-01-01
    • 2019-07-05
    • 2017-03-11
    • 2020-10-03
    相关资源
    最近更新 更多