Static type using batch in Cassandra

Prerequisite – Cassandra
In this article, we will discuss the static column using the BATCH statement and how it works with the BATCH statement. Static column can be used for conditional update using a batch statement. Let’s have a look.

Let’s consider bank_emp_details is a table name and Id, Name, Bank_name, E_start_date and E_end_date are the fields where Id is a primary key and Name is a clustering key.

CREATE TABLE bank_emp_details(
Id int,
Name text,
Bank_name text static,
E_start_date date static,
E_end_date date,
primary key(Id, Name)
); 

Now, here we are using the Batch statement to update the static column. let’s have a look.

BEGIN BATCH 
INSERT INTO bank_emp_details(Id, Name, E_start_date, E_end_date) 
values (201, 'Ashish', '2019-12-06', '2020-12-06');

UPDATE bank_emp_details SET Bank_name = 'ABC'  
where Id = 201 IF Bank_name = NULL;
APPLY BATCH; 

Let’s see the output of inserted data and updated static column.

SELECT * 
from bank_emp_details; 

Output:



Now, here we are trying to update a static column with conditional batch but it will give an error message “Batch with conditions can not span multiple partitions”.let’s have a look.

BEGIN BATCH 
INSERT INTO bank_emp_details(Id, Name, E_start_date, E_end_date) 
values (202, 'Ashish', '2019-12-06', '2022-12-06');

UPDATE bank_emp_details SET Bank_name = 'XYZ' 
where Id = 201 IF Bank_name = 'ABC';
APPLY BATCH; 

The above query will return the following error message given below.

Output:

Now, here we will update the static column value with conditional batch which will work fine. let’s have a look.

BEGIN BATCH 
UPDATE bank_emp_details SET Bank_name = 'XYZ' 
where Id = 201 IF Bank_name = 'ABC';
APPLY BATCH; 

The above query will return the following output given below.

Output:

Let’s see the final Output of the conditional batch using static column. Let’s have a look.

SELECT * 
from bank_emp_details; 

Output:

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.