How to get the last inserted row in Kassandra?

I want to get the last inserted row in the Cassandra table. How to get it? Any ideas?

I am developing a project for this, I am replacing mysql with cassandra. I want to get rid of all sql queries and write them all in cassandra.

+6
source share
2 answers

Just to understand a little ...

As with all Cassandra query problems, the request should be served by a model specially developed for it. This is called query-based modeling. Querying the last row inserted is not an integral function built into each table. You will need to design your model to support this in advance.

For example, let's say I have a table that stores data for users.

CREATE TABLE users ( username TEXT, email TEXT, firstname TEXT, lastname TEXT, PRIMARY KEY (username)); 

If I were to run SELECT * FROM users LIMIT 1 in this table, my result set would contain one row. This line will be the one that contains the least hashed username value (my section key), because this is what Cassandra stores data in the cluster. I would not know if it was the last one added or not, so that would not be very useful for you.

On the other hand, let's say I had a table designed to track the updates that users made to their account information.

 CREATE TABLE userUpdates ( username TEXT, lastUpdated TIMEUUID, email TEXT, firstname TEXT, lastname TEXT, PRIMARY KEY (username,lastUpdated)) WITH CLUSTERING ORDER BY (lastUpdated DESC); 

Next I will raise 3 lines:

 > INSERT INTO userUpdates (username,lastUpdated,email,firstname,lastname) VALUES ('bkerman',now(),' bkerman@ksp.com ','Bob','Kerman'); > INSERT INTO userUpdates (username,lastUpdated,email,firstname,lastname) VALUES ('jkerman',now(),' jkerman@ksp.com ','Jebediah','Kerman'); > INSERT INTO userUpdates (username,lastUpdated,email,firstname,lastname) VALUES ('bkerman',now(),' bobkerman@ksp.com ','Bob','Kerman'); > SELECT username, email, dateof(lastUpdated) FROM userupdates; username | email | system.dateof(lastupdated) ----------+-------------------+---------------------------- jkerman | jkerman@ksp.com | 2016-02-17 15:31:39+0000 bkerman | bobkerman@ksp.com | 2016-02-17 15:32:22+0000 bkerman | bkerman@ksp.com | 2016-02-17 15:31:38+0000 (3 rows) 

If I just SELECT username, email, dateof(lastUpdated) FROM userupdates LIMIT 1 , I will get data from Jedediah Kerman, which is not the last updated. However, if I restrict my section to username='bkerman' , with LIMIT 1 , I will get the very last line for Bob Kerman.

 > SELECT username, email, dateof(lastUpdated) FROM userupdates WHERE username='bkerman' LIMIT 1; username | email | system.dateof(lastupdated) ----------+-------------------+---------------------------- bkerman | bobkerman@ksp.com | 2016-02-17 15:32:22+0000 (1 rows) 

This works because I indicated the order of clustering descending on lastUpdated :

 WITH CLUSTERING ORDER BY (lastUpdated DESC); 

Thus, the results in each section will be returned with the most recent updated row at the top, so LIMIT 1 will become a way to query the very last row.

Therefore, it is important to understand that:

  • Cassandra orders data in a cluster with a hashed partition key value. This helps to ensure a more even distribution of data.
  • Cassandra CLUSTERING ORDER sets the sort order of data on a disk in a section section.
  • Until you can get the latest updated row for each table, you can create models to return this row for each section.

tl; dr; A query in Cassandra is much different than a MySQL query or any DBMS. If querying for the last updated row (for a section) is what you need to do, there are probably ways you can model your table to support it.

+8
source

I want to get the last inserted row in the Cassandra table. How to get it? Any ideas?

It is not possible that you are requesting a queue pattern (give me the last message), and the queue is a well - known anti-pattern for Cassandra

+2
source

All Articles