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

Cassandra deletes the data


May 17, 2021 Cassandra


Table of contents


Use Cqlsh to delete data

You can use the command DELETE to remove data from the table. The syntax is as follows:

DELETE FROM <identifier> WHERE <condition>;

Example

Let's assume that 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 Hyderabad 9848022339 40000
3 rahman Chennai 9848022330 45000

The following statement removes the column from emp_sal row:

cqlsh:tutorialspoint> DELETE emp_sal FROM emp WHERE emp_id=3;

Verify

Use the SELECT statement to verify that the data has been deleted. If you use SELECT to validate the emp table, it produces the following output:

cqlsh:tutorialspoint> select * from emp;

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

Since we removed Rahman's salary, you will see an empty value instead of the salary.

Delete the entire line

The following command removes the entire row from the table.

cqlsh:tutorialspoint> DELETE FROM emp WHERE emp_id=3;

Verify

Use the SELECT statement to verify that the data has been deleted. If you use SELECT to validate the emp table, it produces the following output:

cqlsh:tutorialspoint> select * from emp;

 emp_id |  emp_city | emp_name |  emp_phone | emp_sal
--------+-----------+----------+------------+---------
      1 | Hyderabad |      ram | 9848022338 | 50000
      2 |     Delhi |    robin | 9848022339 | 50000
 
(2 rows)

Since we deleted the last row, there are only two rows left in the table.

Use the Java API to delete data

You can use the Session class's execute() method to delete the data in the table. Follow these steps to remove data from the table using 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. Use the following code to create cluster objects.

//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();

Create a cluster object with the new builder object. T o do this, there is a method called build () in the Cluster.Builder class. U se the following code to create a cluster object.

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

Here we use KeySpace called 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 the following example, we remove the data from a table called emp. You must store the query in a string variable and pass it to the execute() method, as shown below.

String query1 = ”DELETE FROM emp WHERE emp_id=3; ”;
session.execute(query);

The complete program for removing data from the table in Cassandra using the Java API is shown below.

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

public class Delete_Data {

   public static void main(String args[]){
   
      //query
      String query = "DELETE FROM emp WHERE emp_id=3;";

      //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("Data deleted");
   }
}

Use the class name and .java save the above program and browse to the save location. Compile and execute the program as shown in the following image.

$javac Delete_Data.java
$java Delete_Data

Under normal conditions, it should produce the following output:

Data deleted