Cassandra Tutorial
Cassandra is a distributed NoSQL database management system that provides high scalability and fault tolerance. RDBMS (Relational Database Management System) is a traditional database management system that uses tabular data representation and has well-defined data structures.
Syntax
The syntax for creating and querying a table in Cassandra is as follows:
CREATE TABLE <keyspace_name>.<table_name>(
<column_name_1> <data_type>,
<column_name_2> <data_type>,
.
.
.
PRIMARY KEY(<column_name>)
);
SELECT <column_name_1>, <column_name_2>, ... FROM <keyspace_name>.<table_name> WHERE <column_name> = <value>;
Example
Suppose we want to create a table employees
in a keyspace company
and insert some data into it. We can create the table using the following command:
CREATE TABLE company.employees(
emp_id uuid,
name text,
department text,
salary double,
PRIMARY KEY(emp_id)
);
We can insert data into the table using the following command:
INSERT INTO company.employees(emp_id, name, department, salary) VALUES (uuid(), 'John Doe', 'Finance', 50000);
We can query the data from the table using the following command:
SELECT name, department, salary FROM company.employees WHERE emp_id = <emp_id>;
Output
The output of the above example would be the data of the specified employee, retrieved from the employees
table.
Name: John Doe
Department: Finance
Salary: 50000
Explanation
The example above demonstrates the creation and querying of a table in Cassandra. Firstly, we create a table employees
in the company
keyspace with four columns, emp_id
, name
, department
, and salary
. The uuid
data type is used to generate a unique identifier for each row in the table.
Next, we insert a row in the table with some sample data using the INSERT INTO
command. Finally, we query the data from the table using the SELECT
command and filter the result based on the condition specified in the WHERE
clause.
Use
Cassandra is useful in situations where high scalability and fault tolerance are required. It is suited for applications where data is frequently updated and queries need to be performed on large datasets. RDBMS is useful in situations where transactional data integrity is important and the data is structured.
Important Points
- Cassandra is a NoSQL database management system that provides high scalability and fault tolerance.
- RDBMS is a traditional database management system that uses tabular data representation and has well-defined data structures.
- Cassandra uses partitioning and replication to distribute data among nodes for high scalability and fault tolerance.
- RDBMS uses normalization to avoid data duplication and maintain data integrity.
- Cassandra is suited for applications where data is frequently updated and queries need to be performed on large datasets.
- RDBMS is suited for applications where transactional data integrity is important and the data is structured.
Summary
In this tutorial, we learned about Cassandra and RDBMS and the similarities and differences between them. We saw an example of creating and querying a table in Cassandra and discussed the advantages and disadvantages of using Cassandra and RDBMS for different applications. Cassandra is a powerful NoSQL database management system that provides high scalability and fault tolerance. RDBMS is a traditional database management system that is well-suited for applications where transactional data integrity is important.