How can I get the last value of primary key in Cassandra-php? I meant, we have a function to get the ID generated in last query mysql_insert_id
in PHP for MYSQL. Likewise, is there anything for Cassandra? Can you please help me for this problem? Suppose this is my sample table, how can i get the last value of primary key?
userId | BookId (primary key) | Genrecode
--------------------------------------------------------------
22 | 9a9fa429c3494137 | ART4
56 | 9a9fa429b3496137 | 45RT
89 | 9a9ga429a3496132 | ER68
20 | 249ga429a9096542 | QW3Y
29 | 249kg429a2393652 | QWE5
12 | i55oa429a9093462 | 9ER4
08 | e4235k594ik9654r | WRUO
As Alar mentioned, there are no auto-increment/last_insert_id() and the linked article goes into why.
That being said, problems like these turn into a data model problem.
How can I get the last value of primary key in Cassandra(-php)
I suggest creating a separate table in Cassandra to keep track of keys created. Something like:
PRIMARY KEY((day),timestamp) and sort timestamp in DESC
This will allow you to:
SELECT key FROM key_log WHERE day=today limit 1
To get the most recent key created for that specific day. Depending on the frequency of keys generated, you may have to change the partition key to distribute the data more evenly.
I find it's usually a good idea to keep track of generated keys like this either way because you never know what application logic you'll eventually need to cover and this saves you from an app heavy data migration.