Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CQL--get records by id and latest timestamp

I am very new to cassandra so it might sound like a newbie question. I am running cqlsh 5.0.1 | Cassandra 2.1.4 on local.

I have a table like below:

CREATE TABLE master (
id uuid,
creation timestamp,
event_type text,
name text,
PRIMARY KEY(id,creation)
);

...and the records are:

id                                   | creation                 | event_type | name
--------------------------------------+--------------------------+------------+------------------
 305abd6d-34b8-4f36-96c6-9ea0c11be952 | 2015-04-15 14:01:54-0400 |     create |            test2
 305abd6d-34b8-4f36-96c6-9ea0c11be952 | 2015-04-15 14:03:03-0400 |     update |     test2 update
 7440c51c-6441-44fb-833b-6140fbe822eb | 2015-04-15 14:01:54-0400 |     create |            test3
 7440c51c-6441-44fb-833b-6140fbe822eb | 2015-04-15 14:03:44-0400 |     update |     test3 update
 7440c51c-6441-44fb-833b-6140fbe822eb | 2015-04-15 14:04:34-0400 |     update | test3 2nd update

 bf42a120-dec1-47d8-bde2-c0d76f1c93a5 | 2015-04-15 14:01:54-0400 |     create |            test1

How can i select all the records with distinct ids and last modified timestamp. the result should be like:

305abd6d-34b8-4f36-96c6-9ea0c11be952 | 2015-04-15 14:03:03-0400 |     update |     test2 update
 7440c51c-6441-44fb-833b-6140fbe822eb | 2015-04-15 14:04:34-0400 |     update | test3 2nd update
 bf42a120-dec1-47d8-bde2-c0d76f1c93a5 | 2015-04-15 14:01:54-0400 |     create |            test1
like image 335
dgupta Avatar asked Sep 28 '22 07:09

dgupta


1 Answers

Given your current structure, you won't be able to select any other columns aside from id with a DISTINCT query. You can create another query table with just id as the PK, then run a basic SELECT on that (it should always keep the last modified date)

CREATE TABLE querytable (
    id uuid,
    creation timestamp,
    event_type text,
    name text,
    PRIMARY KEY(id)
);

SELECT * from querytable --should only contain unique ID's and the last updated creation date.

You'll have to update this table as you update the master as well.

like image 80
tymeJV Avatar answered Oct 30 '22 14:10

tymeJV