0% found this document useful (0 votes)
7 views43 pages

CSL and Mongo

The document compares Relational databases and NoSQL databases, highlighting key differences such as data handling capabilities, scalability, and transaction support. It also provides an overview of CRUD operations in MongoDB and details on managing keyspaces in Apache Cassandra, including creating, altering, and dropping keyspaces. Additionally, it covers Cassandra Query Language (CQL) for data manipulation, emphasizing its ease of use and schema-less nature.

Uploaded by

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

CSL and Mongo

The document compares Relational databases and NoSQL databases, highlighting key differences such as data handling capabilities, scalability, and transaction support. It also provides an overview of CRUD operations in MongoDB and details on managing keyspaces in Apache Cassandra, including creating, altering, and dropping keyspaces. Additionally, it covers Cassandra Query Language (CQL) for data manipulation, emphasizing its ease of use and schema-less nature.

Uploaded by

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

Difference between Relational database and

NoSQL :
Relational Database NoSQL
It is used to handle data coming in low
It is used to handle data coming in high velocity.
velocity.
It gives only read scalability. It gives both read and write scalability.
It manages structured data. It manages all type of data.
Data arrives from one or few locations. Data arrives from many locations.
It supports complex transactions. It supports simple transactions.
It has single point of failure. No single point of failure.
It handles data in less volume. It handles data in high volume.
Transactions written in one location. Transactions written in many locations.

support ACID properties compliance doesn’t support ACID properties


Its difficult to make changes in database once
Enables easy and frequent changes to database
it is defined
schema is mandatory to store the data schema design is not required
Deployed in vertical fashion. Deployed in Horizontal fashion.
• CRUD Operations (Create, Read, Update, and
Delete) are the basic set of operations that allow
users to interact with the MongoDB server.
• As we know, to use MongoDB we need to
interact with the MongoDB server to perform
certain operations like entering new data into the
application, updating data into the application,
deleting data from the application, and reading
the application data.
• CREATE, READ, UPDATE, and DELETE that form
the CRUD operations in MongoDB.
• Perform CRUD Operations in MongoDB
• Now that we know the components of the
CRUD operation, let’s learn about each
individual operation in MongoDB. We will
know what each operation does, and the
methods to perform these operations in
MongoDB.
• We will create, read, update and delete
documents from MongoDB server.
• . Create Operations
• The create or insert operations are used to
insert or add new documents in the collection.
If a collection does not exist, then it will create
a new collection in the database.
• You can perform, create operations using the
following methods provided by the MongoDB:
Method Description

db.collection.insertOne()
It is used to insert a single document
in the collection.

db.collection.insertMany()
It is used to insert multiple
documents in the collection.

db.createCollection()
It is used to create an empty
collection.
• Create Operations Example
• Let’s look at some examples of the Create
operation from CRUD in MongoDB.

• Example 1: In this example, we are inserting


details of a single student in the form of
document in the student collection using
db.collection.insertOne() method.create
operation example
• Example 2: In this example, we are inserting
details of the multiple students in the form of
documents in the student collection using
db.collection.insertMany() method.
• 2. Read Operations
• The Read operations are used to retrieve
documents from the collection, or in other
words, read operations are used to query a
collection for a document.
• You can perform read operation using the
following method provided by the MongoDB:
Method Description

db.collection.find() It is used to retrieve documents from the


collection.
Method Description

It is used to update a single document in the collection that satisfy the given
db.collection.updateOne() criteria.

db.collection.updateMany It is used to update multiple documents in the collection that satisfy the given
() criteria.

db.collection.replaceOne() It is used to replace single document in the collection that satisfy the given criteria.
• Update Operations Example
• Let’s look at some examples of the update
operation from CRUD in MongoDB.
• Example 1: In this example, we are updating
the age of Sumit in the student collection
using db.collection.updateOne() method.
• In this example, we are updating the year of
course in all the documents in the student
collection using db.collection.updateMany()
method.
Apache Cassandra
• Apache Cassandra is a highly scalable,
distributed NoSQL database designed for
handling large amounts of data across many
commodity servers without a single point of
failure.
• It uses a column-oriented data model, which
is optimized for read and write performance at
scale.
A brief overview of how to create, alter, and
drop keyspaces in Cassandra.
• Keyspaces in Cassandra
• A keyspace is a namespace in Cassandra that
defines how data is replicated across the
nodes in the cluster.
• Each keyspace can contain multiple tables.
When you create a keyspace, you specify the
replication strategy and the replication factor,
which determine how data is distributed and
replicated across the cluster.
Creating a Keyspace- Syntax
• use the CREATE KEYSPACE command

CREATE KEYSPACE keyspace_name


WITH REPLICATION = {
'class' : 'replication_strategy',
'replication_factor' : number_of_replicas
};

• keyspace_name: The name of the keyspace


• replication_strategy: The replication strategy (e.g., 'SimpleStrategy'
or 'NetworkTopologyStrategy')
• number_of_replicas: The number of replicas for the keyspace.
Example:Creating a Keyspace
CREATE KEYSPACE my_keyspace
WITH REPLICATION = {
'class': 'SimpleStrategy',
'replication_factor': 3
};

This creates a keyspace named my_keyspace using


the SimpleStrategy with a replication factor of 3.
Altering a Keyspace
ALTER KEYSPACE my_keyspace
WITH REPLICATION = {
'class': 'NetworkTopologyStrategy',
'datacenter1': 3,
'datacenter2': 2
};

In this example, the keyspace my_keyspace is altered to


use NetworkTopologyStrategy with different replication
factors for different data centers.
Dropping a Keyspace
• To drop a keyspace, use the DROP KEYSPACE
command.

DROP KEYSPACE my_keyspace;

Be cautious with this command, as it will remove the keyspace


and all its associated data.
Summary

1. Create a Keyspace: Use CREATE KEYSPACE with


appropriate replication settings.
2. Alter a Keyspace: Use ALTER KEYSPACE to modify
replication strategies or other settings.
3. Drop a Keyspace: Use DROP KEYSPACE to
permanently remove a keyspace and all its data.

These commands can be executed using cqlsh, the Cassandra Query


Language shell, or through a Cassandra client library in your
preferred programming language.
Cassandra Query Language (CQL)
• Cassandra is an open-source database
application from Apache, which doesn’t
require SQL to fetch, view, update, or delete
the database or the data. Hence it is known as
a NoSQL type of database application.
• This database can be accessed and
administered remotely and directly from
various nodes.
It is famous for its exclusive features like
• higher performance,
• flexibility for expansion and scalability,
• lower user latency,
• peer-to-peer architecture,
• no schema for structuring,
• significantly less effort and cost required for
maintenance,
• faster processing, easy to learn & work on it,
uncomplicated process for data copying, etc.
Advantages

• Ease of data replication: It could easily replicate data deployed on any particular
node if it has gone down when available
• Distribution of data: This could be used to store and locate data at various nodes,
which has its great advantage
• Peer-to-peer architecture: It does not follow a typical master-slave concept but
follows peer-to-peer architecture where every node participates equally with
equal responsibilities. As a consequence of this, there is no single point of failure.
• High availability: Since there is no single point of failure, hence it is highly
available around
• High Performance: It has to be very reliable for a big organization that generally
deals with large-scale data
• Schema-less concept: Schema-less concept means that schema could be made
internally as per our requirement
Cassandra Query Language (CQL)
Cassandra Query Language (CQL) is used for interacting with
Cassandra databases. It provides a syntax similar to SQL,
which makes it easier for users familiar with relational
databases to work with Cassandra:

• Inserts: Can add new rows or overwrite existing rows with


the same primary key.
• Updates: Modify rows identified by the primary key; if the
row doesn’t exist, it will be created.
• Deletes: Remove rows based on the primary key; specifying
no primary key can lead to unintended deletions.
• o insert data into a table in Cassandra, use the
INSERT INTO statement. This operation is used
to add new rows or update existing rows if
they have the same primary key.
• Syntax:
• INSERT INTO keyspace_name.table_name
(column1, column2, column3, ...) VALUES
(value1, value2, value3, ...);
• Example: Assume we have a keyspace
my_keyspace and a table users with columns
user_id, name, and email.
• sql
• Copy code
• INSERT INTO my_keyspace.users (user_id,
name, email) VALUES (123, 'Alice',
'[email protected]');
Insert into
In this example, if a row with user_id 123 does not exist, it will be created. If it
does exist, the row will be updated with the new values.
• 2. Update Data
• To update existing data, use the UPDATE statement. This operation
modifies the data in rows that match the specified primary key. You can
update one or more columns in a row.
• Example: Continuing with the users table:
• sql
• Copy code
• UPDATE my_keyspace.users SET email =
'[email protected]' WHERE
user_id = 123;
• This command updates the email for the user
with user_id 123. The WHERE clause is
essential as it specifies which rows should be
updated.
Update
Syntax:

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE primary_key_column = primary_key_value;

Example:
To update the email address for the user with user_id:

UPDATE users
SET email = '[email protected]'
WHERE user_id = some_user_id;
DELETE)
• Delete Data
• To delete data from a table, use the DELETE
statement. This operation removes rows that
match the specified primary key.
• sql
• Copy code
• DELETE FROM keyspace_name.table_name
WHERE primary_key_column =
primary_key_value;
• sql
• Copy code
• DELETE FROM keyspace_name.table_name
WHERE primary_key_column =
primary_key_value;
• o delete the user with user_id 123 from the
users table:
• sql
• Copy code
• DELETE FROM my_keyspace.users WHERE
user_id = 123;

You might also like