PR 5 - No SQL
PR 5 - No SQL
Objective
Students will explore data modeling principles in Apache Cassandra, emphasizing the creation
of efficient schemas. The goal is to strengthen understanding of Cassandra's query language
(CQL) through hands-on experience with simple queries. Additionally, students will perform
monitoring, troubleshooting, and learn performance tuning and optimization techniques.
Furthermore, they will implement compaction strategies, ensuring a comprehensive skill set for
leveraging Cassandra in practical scenarios.
Prerequisite Theory
Create Keyspace:
To create a keyspace, use the `CREATE KEYSPACE` statement. Replace 'your_keyspace' with the
desired keyspace name and set the replication strategy and factor based on your requirements.
Alter Keyspace:
To alter an existing keyspace, use the `ALTER KEYSPACE` statement. This can include modifying
the replication strategy, replication factor, or other configuration options.
Drop Keyspace:
To drop (delete) a keyspace and all its data, use the `DROP KEYSPACE` statement. Be cautious,
as this operation is irreversible.
These statements provide the basic syntax for creating, altering, and dropping keyspaces in
Cassandra. Customize the keyspace names, replication strategies, and other parameters based
on your specific use case and requirements.
In Cassandra, you can manage tables using the Cassandra Query Language (CQL). Below are
examples of creating, altering, and dropping tables:
Create Table:
To create a table, use the `CREATE TABLE` statement. Replace 'your_table' with the desired
table name and specify the columns along with their data types.
name TEXT,
Alter Table:
To alter an existing table, you can use the `ALTER TABLE` statement. This
allows you to add or drop columns, change the data type of a column, or
modify other table properties.
-- Drop a column
To drop (delete) a table, use the `DROP TABLE` statement. Be cautious, as this
operation permanently removes the table and its data.
DROP TABLE your_table;
These statements provide the basic syntax for creating, altering, and dropping tables in
Cassandra. Customize the table names, column definitions, and other parameters based
on your specific use case and requirements.
In Cassandra, the `TRUNCATE` statement is used to remove all data from a table
while keeping the table structure intact. This operation is similar to deleting all rows
from the table but more efficient, as it does not involve the same overhead.
Truncate Table:
In Cassandra, you can create and drop indexes using the Cassandra Query Language
(CQL). Below are examples of creating and dropping an index on a table:
Create Index:
To create an index on a column, use the `CREATE INDEX` statement. This allows
you to create an index to improve the performance of queries based on that column.
CREATE INDEX your_index_name ON your_table (your_column);
Replace 'your_index_name' with the desired name for the index, 'your_table' with the table
name, and 'your_column' with the column on which you want to create the index.
Drop Index:
To drop (delete) an index, use the `DROP INDEX` statement. This removes the
specified index from the table.
DROP INDEX your_index_name;
Replace 'your_index_name' with the name of the index you want to drop. This statement
removes the index from the table but does not affect the table structure or data.
Ensure that you carefully consider the implications of adding and removing indexes, as
it can impact the performance and storage requirements of your Cassandra database.
BEGIN BATCH
INSERT INTO your_table (id, name, age) VALUES (uuid(), 'John', 25);
UPDATE your_table SET email = '[email protected]' WHERE id = <some_id>;
DELETE FROM another_table WHERE id = <another_id>;
APPLY BATCH;
- `BEGIN BATCH`: Indicates the beginning of the batch.
Insert:
Select:
Replace 'your_table' with the table name and `<some_id>` with the specific identifier.
Update:
Delete:
Cassandra Collections
Cassandra collections are used to handle tasks. You can store multiple elements in
collection.
In Cassandra, `SET`, `LIST`, and `MAP` are collection types that allow you to store
multiple values within a single column. These collections can be useful when you need
to handle scenarios where a column contains multiple items. Here's a brief overview of
each:
SET:
A `SET` is an unordered collection of unique elements. Each element in the set must
be of the same data type. Duplicate values are not allowed.
Example:
CREATE TABLE example_set (
id UUID PRIMARY KEY,
tags SET<TEXT>
);
INSERT INTO example_set (id, tags) VALUES (uuid(), {'tag1', 'tag2', 'tag3'});
LIST:
Ans:
Ans:
Ans:
(4)What is the key feature of Cassandra that ensures system resilience and reliability?
(a) Single Point of Failure (b) Decentralized Design
(c) Low Availability (d) Centralized Architecture
Ans:
Ans:
Ans:
(7)Which use case is NOT mentioned as a strength of Cassandra?
(a) Real-time Applications (b) E-commerce
(c) IoT (Internet of Things) (d) Big Data Analytics
Ans:
Ans:
(9) What is the purpose of the Bloom filter in Cassandra's read operations?
(a) To store data in memory (b) To provide fault tolerance
(c) To filter out unnecessary data (d) To replicate data across nodes
Ans:
(10) What is the primary role of the commit log in Cassandra's architecture?
(a) Data storage (b) Crash recovery mechanism
(c) Query execution (d) Data replication
Ans:
Ans:
True/False Statements:
1. Cassandra supports JOINS, GROUP BY, and aggregation operations.
2. Cassandra's data model is similar to traditional RDBMS.
3. Cassandra's write operations involve capturing data in the commit log, followed by
storing it in SSTables.
4. Cassandra is not optimized for high write performance.
ASSESSMENT RUBRICS
Needs
Criteria Excellent Good Satisfactory Improvement Marks
(10) (7) (5) (3)
Demonstrates a Shows a good
Understanding of comprehensive Displays a basic Lacks
grasp of
Cassandra understanding understanding of
understanding of Cassandra
of Cassandra Cassandra
Cassandra concepts
------------
Signature with date