Search code examples
cassandracqlcql3

Is it possible to query the most recent additions to a table/column family in cassandra?


Assuming the following table, is it possible to easily query the most recent items added to the table?

create table messages(
    person_uuid uuid,
    uuid timeuuid,
    message text);

The primary purpose of this table is to hold a list of messages sent to a particular user, but there is also a need to show an RSS feed of all most recent users, ie, something like:

select person_uuid, message from messages
order by uuid
limit 30;

Solution

  • You need to use Compound Primary Key to be able to sort and order by date.

    CREATE TABLE  messages(
        person_uuid uuid,
        date timeuuid,
        message text,
        PRIMARY KEY(person_uuid,date)
    );
    

    Then you can do

    SELECT * FROM messages WHERE person_uuid=xxx ORDER BY date DESC LIMIT 20;