Cassandra cql: how to select the LAST n rows from a table

Ivan

I want to verify that rows are getting added to the table. What cql statement would show the last n rows from the table below?

Table description below:

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>
Adam Holmberg

You didn't specify last n "by what".

To get the last N per id:

SELECT * FROM option_data WHERE ts=1 ORDER BY id DESC LIMIT N;

ORDER BY clause can only be applied to the second column in a compound primary key. If you need to query by time you will need to think about your data model a little more.

If your queries are most often "last N", you might consider writing something like this:

CREATE TABLE time_series (
    id text,
    t timeuuid,
    data text,
    PRIMARY KEY (id, t)
) WITH CLUSTERING ORDER BY (t DESC)

... where 'id' is your time series id. The CLUSTERING ORDER reverses the order of timeuuid 't', causing the cells to be stored in a natural order for your query.

With this, you would get the last five events as follows:

SELECT * FROM time_series WHERE id='stream id' LIMIT 5;

There is a lot of information out there for time series in Cassandra. I suggest reading some of the more recent articles on the matter.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Deleting all rows from Cassandra cql table

From Dev

Select first N rows of Cassandra table

From Dev

How to know affected rows in Cassandra(CQL)?

From Dev

Doctrine - select last 4 rows from table

From Dev

Doctrine - select last 4 rows from table

From Dev

How to populate related table in Cassandra using CQL?

From Dev

Cassandra CQL3 select row keys from table with compound primary key

From Dev

Cassandra query language(CQL): Can we combine SELECT * FROM table_name; and SELECT count(*) FROM table_name;

From Dev

Cassandra query language(CQL): Can we combine SELECT * FROM table_name; and SELECT count(*) FROM table_name;

From Dev

Select last N rows from MySQL - codeigniter

From Dev

Select last N rows from MySQL - codeigniter

From Dev

MS SQL how to select n rows from table even if table has n - x rows

From Dev

how to efficiiently select first or last rows from a KDB table stored on disk

From Dev

How the select the last record from a time series in Cassandra?

From Dev

How the select the last record from a time series in Cassandra?

From Dev

Laravel 4 and Eloquent ORM - How to select the last 5 rows of a table

From Dev

jQuery - How to select last column of all rows in a table?

From Dev

Get average value from the "last" N rows in a table

From Dev

PostgreSQL:How get last rows from a select query

From Dev

How to select rows from last 2 hours in Doctrine

From Dev

How to select last row from a series of linked rows

From Dev

How to select all rows from a table given conditions of rows?

From Dev

How does one get the average of the last N rows in a table?

From Dev

How to use proc mean to average only last n rows of a table?

From Dev

How to get the average and the last data from a group of rows in a table in MySQL

From Dev

Select last N# of columns of a table from MySQL

From Dev

Select last N# of columns of a table from MySQL

From Dev

SQL - How to select discontinuous rows from a table with one select?

From Dev

How to select the last 3 rows but the last one

Related Related

  1. 1

    Deleting all rows from Cassandra cql table

  2. 2

    Select first N rows of Cassandra table

  3. 3

    How to know affected rows in Cassandra(CQL)?

  4. 4

    Doctrine - select last 4 rows from table

  5. 5

    Doctrine - select last 4 rows from table

  6. 6

    How to populate related table in Cassandra using CQL?

  7. 7

    Cassandra CQL3 select row keys from table with compound primary key

  8. 8

    Cassandra query language(CQL): Can we combine SELECT * FROM table_name; and SELECT count(*) FROM table_name;

  9. 9

    Cassandra query language(CQL): Can we combine SELECT * FROM table_name; and SELECT count(*) FROM table_name;

  10. 10

    Select last N rows from MySQL - codeigniter

  11. 11

    Select last N rows from MySQL - codeigniter

  12. 12

    MS SQL how to select n rows from table even if table has n - x rows

  13. 13

    how to efficiiently select first or last rows from a KDB table stored on disk

  14. 14

    How the select the last record from a time series in Cassandra?

  15. 15

    How the select the last record from a time series in Cassandra?

  16. 16

    Laravel 4 and Eloquent ORM - How to select the last 5 rows of a table

  17. 17

    jQuery - How to select last column of all rows in a table?

  18. 18

    Get average value from the "last" N rows in a table

  19. 19

    PostgreSQL:How get last rows from a select query

  20. 20

    How to select rows from last 2 hours in Doctrine

  21. 21

    How to select last row from a series of linked rows

  22. 22

    How to select all rows from a table given conditions of rows?

  23. 23

    How does one get the average of the last N rows in a table?

  24. 24

    How to use proc mean to average only last n rows of a table?

  25. 25

    How to get the average and the last data from a group of rows in a table in MySQL

  26. 26

    Select last N# of columns of a table from MySQL

  27. 27

    Select last N# of columns of a table from MySQL

  28. 28

    SQL - How to select discontinuous rows from a table with one select?

  29. 29

    How to select the last 3 rows but the last one

HotTag

Archive