简体   繁体   English

如何在cassandra(php)中获取主键的最后一个值?

[英]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? 如何在Cassandra-php中获取主键的最后一个值? I meant, we have a function to get the ID generated in last query mysql_insert_id in PHP for MYSQL . 我的意思是,我们有一个函数来获取在PHP中用于MYSQL的最后一个查询mysql_insert_id中生成的ID。 Likewise, is there anything for Cassandra ? 同样, 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(). 您可以看到这个答案: Cassandra列键自动增量 ,它解释了为什么Cassandra中没有auto_increment,因此没有last_insert_id()。 You need to generate meaningful keys or use a UUID 您需要生成有意义的密钥或使用UUID

As Alar mentioned, there are no auto-increment/last_insert_id() and the linked article goes into why. 正如Alar所提到的,没有自动增量/ last_insert_id(),链接的文章就是为什么。

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) 如何在Cassandra(-php)中获取主键的最后一个值

I suggest creating a separate table in Cassandra to keep track of keys created. 我建议在Cassandra中创建一个单独的表来跟踪创建的密钥。 Something like: PRIMARY KEY((day),timestamp) and sort timestamp in DESC 类似于: 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. 这将允许您: SELECT key FROM key_log WHERE day=today limit 1获取为该特定日期创建的最新密钥。 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. 我发现以这种方式跟踪生成的密钥通常是一个好主意,因为你永远不知道最终需要覆盖哪种应用程序逻辑,这样就可以避免应用程序繁重的数据迁移。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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