Open In App

Static type using batch in Cassandra

Improve
Improve
Like Article
Like
Save
Share
Report

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:


Last Updated : 09 Dec, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads