Cassandra Release Statement - Order of Execution

I have a Cassandra batch statement containing a delete and insert statement of the same section key, where delete is the first statement and insert is the second. How does the batch operator follow these instructions? In the same order in which we added the statements?

+3


source to share


1 answer


No, it does not execute them in the order shown. To enforce a specific order of execution, you can add a clause USING TIMESTAMP

. Check the docs for more information: http://docs.datastax.com/en/cql/3.1/cql/cql_reference/batch_r.html

Using a timestamp as it can maintain the order of execution. For example, if the above example is (delete and insert for the same section key), the end result is the inserted record. Is this possible by adding a timestamp?

Yes. I'll give examples from the above link and DELETE documentation for demonstration and start by creating a simple table named purchases

with two fields:

CREATE TABLE purchases (user text PRIMARY KEY, balance bigint);

      

Then I will execute the batch with INSERT and DELETE. I'll make the DELETE last but with an earlier timestamp than the INSERT:



BEGIN BATCH
  INSERT INTO purchases (user, balance) VALUES ('user1', -8) USING TIMESTAMP 1432043350384;
  DELETE FROM purchases USING TIMESTAMP 1432043345243 WHERE user='user1';
APPLY BATCH;

      

When I ask userid

:

aploetz@cqlsh:stackoverflow2> SELECT user, balance, writetime(balance) FROM purchases WHERE user='user1';

 user  | balance | writetime(balance)
-------+---------+--------------------
 user1 |      -8 |      1432043350384

(1 rows)

      

As you can see, the INSERT was saved because it had the last timestamp. If I just ran INSERT and DELETE (in that order) from the cqlsh prompt, the query would return nothing.

+6


source







All Articles