Apache Cassandra (NOSQL database) - GeeksforGeeks

21 Apr, 2022
In this article, we will learn the basics of Apache Cassandra and the basics of CQL (Cassandra Query Language) operations like Create, insert, delete, select, etc. 
Apache Cassandra: Apache Cassandra is an open-source no SQL database that is used for handling big data. Apache Cassandra has the capability to handle structure, semi-structured, and unstructured data. Apache Cassandra was originally developed at Facebook after that it was open-sourced in 2008 and after that, it become one of the top-level Apache projects in 2010.
 
 Figure-1: Masterless ring architecture of Cassandra 
Apache Cassandra is a highly scalable, distributed database that strictly follows the principle of the 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 copies of data at the time of keyspace creation. We can simply define replication strategy and RF (Replication Factor) to create multiple copies of data. Example:
CREATE KEYSPACE Example
WITH replication = {'class': 'NetworkTopologyStrategy', 
                             'replication_factor': '3'}; 
In this example, we define RF (Replication Factor) as 3 which simply means that we are creating here 3 copies of data across multiple nodes in a 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 use 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 a table use 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 use 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 use the following CQl query.
SELECT * FROM Emp_table;
Ashish_rana
saurabh kundu
Apache
DBMS
GBlog
DBMS
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Introduction of B-Tree
Difference between Clustered and Non-clustered index
Introduction of ER Model
CTE in SQL
SQL | Views
Roadmap to Become a Web Developer in 2022
Must Do Coding Questions for Companies like Amazon, Microsoft, Adobe, ...
DSA Sheet by Love Babbar
Socket Programming in C/C++
Must Do Coding Questions for Product Based Companies