Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Cassandra batch


May 17, 2021 Cassandra


Table of contents


Use Cqlsh to execute batch statements

With BATCH, you can execute multiple modification statements (insert, update, delete) at the same time. The syntax is as follows:

BEGIN BATCH
<insert-stmt>/ <update-stmt>/ <delete-stmt>
APPLY BATCH

Example

Suppose Cassandra has a table called emp with the following data:

emp_id emp_name emp_city emp_phone emp_sal
1 Ram Hyderabad 9848022338 50000
2 robin Delhi 9848022339 50000
3 rahman Chennai 9848022330 45000

In this example, we'll do the following:

  • Insert a new line (4, rajeev, pune, 9848022331,30000) with the following details.
  • Update the salary of an employee with line number 3 to 50,000.
  • Delete the city of the employee with the line ID of 2.

To do this at once, use the following PATCH command:

cqlsh:tutorialspoint> BEGIN BATCH
... INSERT INTO emp (emp_id, emp_city, emp_name, emp_phone, emp_sal) values(  4,'Pune','rajeev',9848022331, 30000);
... UPDATE emp SET emp_sal = 50000 WHERE emp_id =3;
... DELETE emp_city FROM emp WHERE emp_id = 2;
... APPLY BATCH;

Verify

After the change, the table is validated with the SELECT statement. It should produce the following output:

cqlsh:tutorialspoint> select * from emp;

 emp_id |  emp_city | emp_name |  emp_phone | emp_sal
--------+-----------+----------+------------+---------
      1 | Hyderabad | ram      | 9848022338 | 50000
      2 | null      | robin    | 9848022339 | 50000
      3 | Chennai   | rahman   | 9848022330 | 50000
      4 | Pune      | rajeev   | 9848022331 | 30000
    
(4 rows)

Here you can observe tables with modified data.

Batch statements that use the Java API

You can programmatically write batch statements in a table using the Execute() method of the Session class. Follow the steps given below to execute multiple statements with the help of the Java API.

Step 1: Create a cluster object

Create an instance of the Cluster.builder class called com.datastax.driver.core, as shown below.

//Creating Cluster.Builder object
Cluster.Builder builder1 = Cluster.builder();

Use the addContactPoint() method of the Cluster.Builder object to add a contact point (the IP address of the node). This method returns Cluster.Builder.

//Adding contact point to the Cluster.Builder object
Cluster.Builder builder2 = build.addContactPoint( "127.0.0.1" );

Create a cluster object with the new builder object. T o do this, there is a method called build() in the Cluster.Builder class. Create cluster objects using the following code:

//Building a cluster
Cluster cluster = builder.build();

You can use a single line of code to build cluster objects, as shown below.

Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1").build();

Step 2: Create a session object

Create an instance of a Session object using the Connect() method of the Cluster class, as shown below.

Session session = cluster.connect( );

This method creates a new session and initializes it. If you already have a key space, you can do so by formatting the KeySpace name as an existing key space in string format, as shown below.

Session session = cluster.connect(“ Your keyspace name ”);

Here we use KeySpace to name tp. Therefore, the session object is created as follows.

Session session = cluster.connect(“tp”);

Step 3: Execute the query

You can use the Execute() method of the Session class to perform CQL queries. P ass the query to the execute() method in string format or state class objects. Whether you pass this method in string format will execute on cqlsh.

In this example, we'll do the following:

  • Insert a new line (4, rajeev, pune, 9848022331,30000) with the following details.
  • Update the salary of an employee with line number 3 to 50,000.
  • Delete the city where the line ID is 2 for the employee.

You must store the query in a string variable and pass it to the execute() method, as shown below.

String query1 = ” BEGIN BATCH INSERT INTO emp (emp_id, emp_city, emp_name,   emp_phone, emp_sal) values( 4,'Pune','rajeev',9848022331, 30000);
UPDATE emp SET emp_sal = 50000 WHERE emp_id =3;
DELETE emp_city FROM emp WHERE emp_id = 2;
APPLY BATCH;”;

Here's the full program that uses the Java API to execute multiple statements at the same time on a table in Cassandra.

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session;

public class Batch {

   public static void main(String args[]){
    
      //query
      String query =" BEGIN BATCH INSERT INTO emp (emp_id, emp_city,
         emp_name, emp_phone, emp_sal) values( 4,'Pune','rajeev',9848022331, 30000);"
    
         + "UPDATE emp SET emp_sal = 50000 WHERE emp_id =3;"
         + "DELETE emp_city FROM emp WHERE emp_id = 2;"
         + "APPLY BATCH;";

      //Creating Cluster object
      Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1").build();
 
      //Creating Session object
      Session session = cluster.connect("tp");
 
      //Executing the query
      session.execute(query);

      System.out.println("Changes done");
   }
}

Use the class name and .java save the above program and browse to the save location. The program is compiled and executed as follows.

$javac Batch.java
$java Batch

Under normal conditions, it should produce the following output:

Changes done