Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
290 views
in Technique[技术] by (71.8m points)

cql3 - Cassandra batch statement - Execution order

I have a batch statement of Cassandra that contains a delete and an insert statement of same partition key, where delete is the first statement and insert is the second. How the batch statement executes these statements ? Is in the same order in which,we added the statements?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

No, it does not execute them in the order specified. To force a particular execution order, you can add the USING TIMESTAMP clause. Check the docs for more information: http://docs.datastax.com/en/cql/3.1/cql/cql_reference/batch_r.html

Using time stamp how it can maintain the order of execution . For Example if the above example (delete and insert for same partition key), the final result should be the inserted record. Is that possible by adding time-stamp ??

Yes. I'll combine examples from the link above and the DELETE documentation to demonstrate, and start by creating a simple table called purchases with two fields:

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

Next, I'll execute a batch with an INSERT and a DELETE. I'll do 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 query for 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 persisted because it had the latest timestamp. Whereas if I had simply run the INSERT and DELETE (in that order) from the cqlsh prompt, the query would have returned nothing.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...