Tuesday, 24 July 2018

Cassandra Curd Operation – Create, Update, Read & Delete

Cassandra Curd Operation – Create, Update, Read & Delete


1.Objective

In this Cassandra Tutorial, we will learn about the Cassandra CURD Operation: Create, Update, Read & Delete. Moreover, we will cover syntax and example of each CURD operation in Cassandra.
So, let’s start with Cassandra CURD Operation.
Cassandra Curd Operation
Cassandra Curd Operation – Create, Update, Read & Delete

2. What is Cassandra CURD Operation?

Cassandra CURD Operation stands for Create, Update, Read and Delete or Drop. These operations are used to manipulate data in Cassandra. Apart from these CURD operations in Cassandra, a user can also verify the command or the data.

a. Create Operation

A user can insert data into the table using Cassandra CURD operation. The data is stored in the columns of a row in the table. Using INSERT command with proper what, a user can perform this operation.
A Syntax of Create Operation-
  1. INSERT INTO <table name>
  2. (<column1>,<column2>....)
  3. VALUES (<value1>,<value2>...)
  4. USING<option>
Let’s create a table data to illustrate the operation. Example consist of a table with information about students in college. The following table will give the details about the students.
Table.1 Cassandra Curd Operation – Create Operation
ENNAMEBRANCHPHONECITY
001AyushElectrical Engineering9999999999Boston
002AaravComputer Engineering8888888888New York City
003KabirApplied Physics7777777777Philadelphia
EXAMPLE 1: Creating a table and inserting the data into a table:
INPUT:
cqlsh:keyspace1> INSERT INTO student(en, name, branch, phone, city)
VALUES(001, ‘Ayush’, ‘Electrical Engineering’, 9999999999, ‘Boston’);
cqlsh:keyspace1> INSERT INTO student(en, name, branch, phone, city)
VALUES(002, ‘Aarav’, ‘Computer Engineering’, 8888888888, ‘New York City’);
cqlsh:keyspace1> INSERT INTO student(en, name, branch, phone, city)

VALUES(003, ‘Kabir’, ‘Applied Physics’, 7777777777, ‘Philadelphia’);
Table.2 Cassandra Curd Operation – OUTPUT After Verification (READ operation)
ENNAMEBRANCHPHONECITY
001AyushElectrical Engineering9999999999Boston
002AaravComputer Engineering8888888888New York City
003KabirApplied Physics7777777777Philadelphia

b.Update Operation

The second operation in the Cassandra CURD operation is the UPDATE operation. A user can use UPDATE command for the operation. This operation uses three keywords while updating the table.
  • Where: This keyword will specify the location where data is to be updated.
  • Set: This keyword will specify the updated value.
  • Must: This keyword includes the columns composing the primary key.
Furthermore, at the time of updating the rows, if a row is unavailable, then Cassandra has a feature to create a fresh row for the same.
A Syntax of Update Operation-
  1. UPDATE <table name>
  2. SET <column name>=<new value>
  3. <column name>=<value>...
  4. WHERE <condition>
EXAMPLE 2: Let’s change few details in the table ‘student’. In this example, we will update Aarav’s city from ‘New York City’ to ‘San Fransisco’.
INPUT:
cqlsh:keyspace1> UPDATE student SET city=’San Fransisco’
WHERE en=002;
Table.3 Cassandra Curd Operation – OUTPUT After Verification 
ENNAMEBRANCHPHONECITY
001AyushElectrical Engineering9999999999Boston
002AaravComputer Engineering8888888888San Fransisco
003KabirApplied Physics7777777777Philadelphia

c. Read Operation

This is the third Cassandra CURD Operation – Read Operation. A user has a choice to read either the whole table or a single column. To read data from a table, a user can use SELECT clause. This command is also used for verifying the table after every operation.
SYNTAX to read the whole table-
  1. SELECT * FROM <table name>;
EXAMPLE 3: To read the whole table ‘student’.
INPUT:
cqlsh:keyspace1> SELECT * FROM student;
Table.4 Cassandra Curd Operation – OUTPUT After Verification 
ENNAMEBRANCHPHONECITY
001AyushElectrical Engineering9999999999Boston
002AaravComputer Engineering8888888888San Fransisco
003KabirApplied Physics7777777777Philadelphia
SYNTAX to read selected columns-
  1. SELECT <column name1>,<column name2>.... FROM <table name>;
EXAMPLE 4: To read columns of name and city from table ‘student’.
INPUT:
cqlsh:keyspace1> SELECT name, city FROM student;
Table.5 Cassandra Curd Operation – OUTPUT After Verification 
NAMECITY
AyushBoston
AaravSan Fransisco
KabirPhiladelphia

d. Delete Operation

Delete operation is the last Cassandra CURD Operation, allows a user to delete data from a table. The user can use DELETE command for this operation.
A Syntax of Delete Operation-
  1. DELETE <identifier> FROM <table name> WHERE <condition>;
EXAMPLE 5: In the ‘student’ table let us delete the ‘phone’ or phone number from 003 row.
cqlsh:keyspace1> DELETE phone FROM student WHERE en=003;
Table.6 Cassandra Curd Operation – OUTPUT After Verification 
ENNAMEBRANCHPHONECITY
001AyushElectrical Engineering9999999999Boston
002AaravComputer Engineering8888888888San Fransisco
003KabirApplied PhysicsnullPhiladelphia
SYNTAX for deleting the entire row-
  1. DELETE FROM <identifier> WHERE <condition>;
EXAMPLE 6: In the ‘student’ table, let us delete the entire third row.
cqlsh:keyspace1> DELETE FROM student WHERE en=003;
Table.7 Cassandra Curd Operation – OUTPUT After Verification 
ENNAMEBRANCHPHONECITY
001AyushElectrical Engineering9999999999Boston
002AaravComputer Engineering8888888888San Fransisco
So, this was all about Cassandra CURD operations Tutorial. Hope you like our explanation.

3. Conclusion

Hence, in this article, we studied the CURD operations in Cassandra. By using these operations, a user can make many applications or program in Cassandra. Furthermore, if you have any query, feel free to ask in the comment section.

0 comments:

Post a Comment