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

Cassandra updates the data


May 17, 2021 Cassandra


Table of contents


Use Cqlsh to update the data

UPDATE is a command that updates the data in a table. Use the following keywords when updating data in a table:

  • Where - This clause is used to select the rows to update.

  • Set - Use this keyword to set the value.

  • Must - Includes all the columns that make up the primary key.

When a row is updated, UPDATE creates a new row if a given row is not available. Here's the syntax for the UPDATE command:

UPDATE <tablename>
SET <column name> = <new value>
<column name> = <value>....
WHERE <condition>

Example

Suppose you have a table called emp. This table stores the details of a company employee with the following details:

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

Now let's emp_city Robin's salary to Delhi and his salary to 50,000.

cqlsh:tutorialspoint> UPDATE emp SET emp_city='Delhi',emp_sal=50000
   WHERE emp_id=2;

Verify

Use the SELECT statement to verify that the data has been updated. If you validate the emp table with the SELECT statement, 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 | 45000
      
(3 rows)

It is observed here that the table data has been updated.

Update the data using the Java API

You can update the data in the table using the Sex class's execute() method. Follow these steps to update the data in 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( );

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 the following example, we update the emp table. You must store the query in a string variable and pass it to the execute() method, as follows:

String query = “ UPDATE emp SET emp_city='Delhi',emp_sal=50000
WHERE emp_id = 2;” ;

The full program for updating the data in the table using the Java API is given below.

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

public class Update_Data {
  
   public static void main(String args[]){
      
      //query
      String query = " UPDATE emp SET emp_city='Delhi',emp_sal=50000"
          
      //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 updated");
   }
 }

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 Update_Data.java
$java Update_Data

Under normal conditions, it should produce the following output:

Data updated