0% found this document useful (0 votes)
31 views9 pages

PR 5 - No SQL

This document provides an overview of basic operations for managing keyspaces and tables, along with examples of creating, altering and dropping keyspaces and tables in Cassandra. It also discusses truncating tables, creating and dropping indexes, using batches, and performing CRUD operations. Common collection types and practical questions related to Cassandra are also covered.

Uploaded by

dkchauhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views9 pages

PR 5 - No SQL

This document provides an overview of basic operations for managing keyspaces and tables, along with examples of creating, altering and dropping keyspaces and tables in Cassandra. It also discusses truncating tables, creating and dropping indexes, using batches, and performing CRUD operations. Common collection types and practical questions related to Cassandra are also covered.

Uploaded by

dkchauhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

PRACTICAL NO: 05

Aim: Data Modeling and Simple Queries with Cassandra

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

Basic operations and maintenance


In Cassandra, the management of keyspaces involves creating, altering, and dropping. Here are
examples of how you can perform these actions using the Cassandra Query Language (CQL):

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.

CREATE KEYSPACE your_keyspace

WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 3};

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.

ALTER KEYSPACE your_keyspace

WITH replication = {'class': 'NetworkTopologyStrategy', 'DC1': 3, 'DC2': 2};

Drop Keyspace:

To drop (delete) a keyspace and all its data, use the `DROP KEYSPACE` statement. Be cautious,
as this operation is irreversible.

DROP KEYSPACE your_keyspace;

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.

CREATE TABLE your_table ( id UUID PRIMARY KEY,

name TEXT,

age INT, email 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.

-- Add a new column

ALTER TABLE your_table ADD phone_number TEXT;

-- Drop a column

ALTER TABLE your_table DROP age;

-- Modify column data type

ALTER TABLE your_table ALTER email TYPE VARCHAR;


Drop Table:

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:

To truncate a table, use the following `TRUNCATE` statement:


TRUNCATE your_table;
Replace 'your_table' with the name of the table you want to truncate. This statement will
remove all rows from the specified table, but the table structure, including column
definitions and indexes, will remain unchanged.

It's important to note that truncating a table is a non-reversible operation, and it


permanently removes all data from the table. Ensure you have a backup or are certain
about this action before executing it, especially in a production environment.

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.

In Cassandra, the `BATCH` statement is used to group multiple CQL statements


(queries, updates, or deletes) into a single atomic operation. This ensures that either all
the statements in the batch are executed successfully or none of them are. Batches are
useful when you need to maintain consistency across multiple write operations.

Basic Batch Syntax:


BEGIN BATCH
// CQL statements to be executed
APPLY BATCH;
Here is a more detailed example:

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.

- `APPLY BATCH`: Indicates the end of

the batch. CRUD Operations


In Cassandra, you can perform basic data manipulation operations such as insert,
select, update, and delete using the Cassandra Query Language (CQL). Here are
examples of each operation:

Insert:

To insert data into a table, use the `INSERT INTO` statement:


INSERT INTO your_table (id, name, age, email) VALUES (uuid(), 'John Doe', 25,
'[email protected]');
Replace 'your_table' with the name of your table and adjust the values accordingly.

Select:

To retrieve data from a table, use the `SELECT` statement:


SELECT * FROM your_table WHERE id = <some_id>;

Replace 'your_table' with the table name and `<some_id>` with the specific identifier.

Update:

To update existing data in a table, use the `UPDATE` statement:


UPDATE your_table SET email = '[email protected]' WHERE id = <some_id>;
Replace 'your_table' with the table name, `<some_id>` with the specific
identifier, and adjust the values accordingly.

Delete:

To delete data from a table, use the `DELETE` statement:


DELETE FROM your_table WHERE id = <some_id>;
Replace 'your_table' with the table name and `<some_id>` with the specific identifier.

Remember to replace placeholders such as 'your_table' and '<some_id>' with your


actual table name and identifier. Additionally, ensure that your data model and
application requirements guide the structure of these queries for optimal performance
and scalability.

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:

A `LIST` is an ordered collection of elements where duplicates are allowed. Elements in


the list can be of different data types.
Practical related Questions
Multiple-Choice Questions (MCQ):
(1) What is Cassandra primarily designed for?
(a) Small-scale databases
(b) Handling large amounts of data across multiple servers
(c) Single-node applications
(d) Only read-intensive workloads

Ans:

(2) When was Cassandra open-sourced by Facebook?


(a) July 2009 (b) March 2010
(c) July 2008 (d) February 2010

Ans:

(3)Which organization manages Apache Cassandra?


(a) Facebook (b) Datastax (c) Apache Software Foundation (d) Microsoft

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:

(5)Which factor allows Cassandra to accommodate growing data needs easily?


(a) Data Consistency (b) Decentralized Architecture
(c) Low Availability (d) Centralized Design

Ans:

(6)What is CQL in Cassandra?


(a) Centralized Query Language (b) Cassandra Question Language
(c) Cluster Query Language (d) Cassandra Query Language

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:

8. What is one of the challenges mentioned in using Cassandra?


(a) Linear Scalability (b) High Availability -
(c) Learning Curve (d) Tunable Consistency

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:

(11)Match the Following:


1. Keyspace (a) Decentralized design
2. Replication Factor (b) Managing large volumes of IoT data
3. Commit Log (c) Outermost container for data in Cassandra
4. Bloom Filter (d) Crash recovery mechanism

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

Exhibits Demonstrates Struggles to


Proficiency in exceptional skills in Has a basic
good grasp data
Data Modeling data modeling in understanding
proficiency in modeling
of data
Cassandra data modeling concepts
modeling

Successfully Installs and Attempts to


Installation and Unable to
installs and configures install and
Configuration install and
configures Cassandra with configure
configure
Cassandra minor issues Cassandra but
Cassandra
with significant
issues
Average Marks

------------
Signature with date

You might also like