Skip to content
Related Articles

Related Articles

Apache Cassandra (NOSQL database)
  • Last Updated : 18 Sep, 2019

In this article we will learn basics of Apache Cassandra and basics of CQL (Cassandra Query Language) operations like Create, insert, delete, select etc.

Apache Cassandra:
Apache Cassandra is an open source no SQL database which is used for handling big data. Apache Cassandra has capability to handle structure, semi structure, unstructured data. Apache Cassandra was originally developed at Facebook after that it was open sourced in 2008 and after that it become one of a top level Apache project in 2010.


Figure-1: Master less ring architecture of Cassandra

Apache Cassandra is highly Scalable, distributed database which is strictly follow the principle of CAP (Consistency Availability and Partition tolerance) theorem.




Figure-2: CAP Theorem



In Apache Cassandra there is no master-client architecture. It has a peer to peer architecture. In Apache Cassandra we can create multiple copy of data at the time of keyspace creation. we can simple define replication strategy and RF (Replication Factor) to create multiple copy of data.

Example:

CREATE KEYSPACE Example
WITH replication = {'class': 'NetworkTopologyStrategy', 
                             'replication_factor': '3'}; 

In this Example we define RF (Replication Factor) is 3 which simply means that we are creating here 3 copy of data across multiple node in clockwise direction.


Figure-3: RF = 3

cqlsh: CQL shell
cqlsh is a command line shell for interacting with Cassandra through CQL (Cassandra Query Language).

CQL query for Basic Operation:
Step1: To create keyspace used the following CQL query.

CREATE KEYSPACE Emp
WITH replication = {'class': 'SimpleStrategy', 
                             'replication_factor': '1'}; 

Step2: CQL query for using keyspace

Syntax: 
USE keyspace-name 
USE Emp;

Step-3: To create table used the following CQL query.

Example:
CREATE TABLE Emp_table (
    name text PRIMARY KEY,
    Emp_id int,
    Emp_city text,
    Emp_email text,
    );

Step-4: To insert into Emp_table used the following CQL query.

Insert into Emp_table(name, Emp_id, Emp_city, Emp_email) 
  VALUES ('ashish', 1001, 'Delhi', 'ashish05.rana05@gmail.com');
Insert into Emp_table(name, Emp_id, Emp_city, Emp_email) 
  VALUES ('Ashish Gupta', 1001, 'Bangalore', 'ashish@gmail.com');
Insert into Emp_table(name, Emp_id, Emp_city, Emp_email) 
  VALUES ('amit ', 1002, 'noida', 'abc@gmail.com');
Insert into Emp_table(name, Emp_id, Emp_city, Emp_email) 
  VALUES ('dhruv', 1003, 'pune', 'xyz@gmail.com');
Insert into Emp_table(name, Emp_id, Emp_city, Emp_email) 
  VALUES ('shivang', 1004, 'mumbai', 'test@gmail.com');
Insert into Emp_table(name, Emp_id, Emp_city, Emp_email) 
  VALUES ('aayush', 1005, 'gurugram', 'cass_write@gmail.com');
Insert into Emp_table(name, Emp_id, Emp_city, Emp_email) 
  VALUES ('bhagyesh', 1006, 'chandigar', 'welcome@gmail.com'); 

Step-5: To read data used the following CQl query.

SELECT * FROM Emp_table;

Try out the all-new GeeksforGeeks Premium!

My Personal Notes arrow_drop_up
Recommended Articles
Page :