Cassandra cql:如何从表中选择最后n行

Iva*_*van 11 cassandra cql3

我想验证行是否已添加到表中.什么cql语句会显示下表中的最后 n行?

表格说明如下:

cqlsh:timeseries> describe table option_data;

CREATE TABLE option_data (
  ts bigint,
  id text,
  strike decimal,
  callask decimal,
  callbid decimal,
  maturity timestamp,
  putask decimal,
  putbid decimal,
  PRIMARY KEY ((ts), id, strike)
) WITH
  bloom_filter_fp_chance=0.010000 AND
  caching='KEYS_ONLY' AND
  comment='' AND
  dclocal_read_repair_chance=0.100000 AND
  gc_grace_seconds=864000 AND
  index_interval=128 AND
  read_repair_chance=0.000000 AND
  replicate_on_write='true' AND
  populate_io_cache_on_flush='false' AND
  default_time_to_live=0 AND
  speculative_retry='99.0PERCENTILE' AND
  memtable_flush_period_in_ms=0 AND
  compaction={'class': 'SizeTieredCompactionStrategy'} AND
  compression={'sstable_compression': 'LZ4Compressor'};

cqlsh:timeseries>
Run Code Online (Sandbox Code Playgroud)

Ada*_*erg 15

你没有指定最后一个"由什么".

要获得每个ID的最后N个:

SELECT * FROM option_data WHERE ts=1 ORDER BY id DESC LIMIT N;
Run Code Online (Sandbox Code Playgroud)

ORDER BY子句只能应用于复合主键中的第二列.如果您需要按时间查询,则需要更多地考虑您的数据模型.

如果您的查询通常是"最后N",您可以考虑编写如下内容:

CREATE TABLE time_series (
    id text,
    t timeuuid,
    data text,
    PRIMARY KEY (id, t)
) WITH CLUSTERING ORDER BY (t DESC)
Run Code Online (Sandbox Code Playgroud)

...'id'是你的时间序列id.CLUSTERING ORDER反转timeuuid't的顺序,使得单元格以查询的自然顺序存储.

有了这个,您将获得最后五个事件,如下所示:

SELECT * FROM time_series WHERE id='stream id' LIMIT 5;
Run Code Online (Sandbox Code Playgroud)

Cassandra有很多关于时间序列的信息.我建议阅读一些关于此事的最新文章.这是简洁且相对较新的:http: //www.datastax.com/documentation/tutorials/Time_Series.pdf