简体   繁体   中英

How do I get the last value of primary key in cassandra(php)?

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

You can see this answer: Cassandra column key auto increment which explain why there are no auto_increment in Cassandra and, consequentially, why there is no last_insert_id(). You need to generate meaningful keys or use a UUID

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM