Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Insert with timestamp in Cassandra

Tags:

cassandra

I am trying to INSERT (also UPDATE and DELETE) data in Cassandra using timestamp, but no change occur to the table. Any help please?

BEGIN BATCH
  INSERT INTO transaction_test.users(email,age,firstname,lastname) VALUES ('1',null,null,null) USING TIMESTAMP 0;
  INSERT INTO transaction_test.users(email,age,firstname,lastname) VALUES ('2',null,null,null) USING TIMESTAMP 1;
  INSERT INTO transaction_test.users(email,age,firstname,lastname) VALUES ('3',null,null,null) USING TIMESTAMP 2;         
APPLY BATCH;
like image 910
Haninbt9 Avatar asked Jan 08 '23 21:01

Haninbt9


1 Answers

I think you're falling into Cassandra's "control of timestamps". Operations in C* are (in effect1) executed only if the timestamp of the new operation is "higher" than previous one.

Let's see an example. Given the following insert

INSERT INTO test (key, value ) VALUES ( 'mykey', 'somevalue') USING TIMESTAMP 1000;

You expect this as output:

select key,value,writetime(value) from test where key='mykey';

 key   | value     | writetime(value)
-------+-----------+------------------
 mykey | somevalue |             1000

And it should be like this unless someone before you didn't perform an operation on this information with a higher timestamp. For instance, if you now write

INSERT INTO test (key, value ) VALUES ( 'mykey', '999value') USING TIMESTAMP 999;

Here's the output

select key,value,writetime(value) from test where key='mykey';

 key   | value     | writetime(value)
-------+-----------+------------------
 mykey | somevalue |             1000

As you can see neither the value nor the timestamp have been updated.


1 That's a slight simplification. Unless you are doing a specialised 'compare-and-set' write, Cassandra doesn't read anything from the table before it writes and it doesn't know if there is existing data or what its timestamp is. So you end up with two versions of the row, with different timestamps. But when you read the row back you always get the one with the latest timestamp. Normally Cassandra will compact such duplicate rows after a while, which is when the older timestamp row gets discarded.

like image 191
Carlo Bertuccini Avatar answered Mar 06 '23 08:03

Carlo Bertuccini