This article is half-done without your Comment! *** Please share your thoughts via Comment ***
In this post, I am sharing basic help to execute multiple statements using one single Cassandra batch.
Many times, it is required to execute multiple statements using single batch.
Cassandra CQL provides the BEGIN BATCH…APPLY BATCH command to execute multiple statements.
Below is a small demonstration:
Create Keyspace if not exists:
1 |
CREATE KEYSPACE IF NOT EXISTS dbrnd WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 3}; |
1 |
USE dbrnd; |
1 2 3 4 5 6 7 |
CREATE TABLE IF NOT EXISTS tbl_Employee ( EmpID INT PRIMARY KEY ,EmpFirstName VARCHAR ,EmpLastName VARCHAR ,EmpSalary INT ); |
1 2 3 4 5 6 7 8 9 |
INSERT INTO tbl_Employee (EmpID,EmpFirstName,EmpLastName,EmpSalary) VALUES (1,'Anvesh','Patel',50000); INSERT INTO tbl_Employee (EmpID,EmpFirstName,EmpLastName,EmpSalary) VALUES (2,'Neevan','Patel',150000); |
1 2 3 4 5 6 7 8 9 10 |
cassandra@cqlsh:dbrnd> begin batch INSERT INTO tbl_Employee (EmpID,EmpFirstName,EmpLastName,EmpSalary) VALUES (3,'Meera','Patel',80000); UPDATE tbl_Employee SET EmpFirstName ='Martin' WHERE EmpID=2; DELETE FROM tbl_Employee WHERE EmpID=3; apply batch; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
cassandra@cqlsh:dbrnd> SELECT *FROM tbl_Employee; @ Row 1 --------------+-------- empid | 1 empfirstname | Anvesh emplastname | Patel empsalary | 50000 @ Row 2 --------------+-------- empid | 2 empfirstname | Martin emplastname | Patel empsalary | 150000 (2 rows) |
Leave a Reply