0% found this document useful (0 votes)
20 views29 pages

06 CassandraDB Comman

Uploaded by

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

06 CassandraDB Comman

Uploaded by

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

CassandraDB Commands

Tushar B. Kute,
http://tusharkute.com
Shell Commands

• help
– The HELP command displays a synopsis and a
brief description of all cqlsh commands.
• capture
– This command captures the output of a
command and adds it to a file.
• CONSISTENCY
– This command shows the current consistency
level, or sets a new consistency level.
Shell Commands

• Copy
– This command copies data to and from Cassandra to a file. Given
below is an example to copy the table named emp to the file myfile.
– COPY emp (emp_id, emp_city, emp_name, emp_phone, emp_sal)
TO ‘myfile’;
• DESCRIBE
– This command describes the current cluster of Cassandra and its
objects. The variants of this command are explained below.
– Describe cluster: This command provides information about the
cluster.
– Describe Keyspaces: This command lists all the keyspaces in a
cluster.
– Describe tables: This command lists all the tables in a keyspace.
Shell Commands

• Expand
– This command is used to expand the output. Before using
this command, you have to turn the expand command on.
• SHOW
– This command displays the details of current cqlsh session
such as Cassandra version, host, or data type assumptions.
– show host;
– show version;
• SOURCE
– Using this command, you can execute the commands in a
file.
KeySpace operations

• A keyspace in Cassandra is a namespace that defines data


replication on nodes.
• A cluster contains one keyspace per node. Given below is the
syntax for creating a keyspace using the statement CREATE
KEYSPACE.
• Syntax:
– CREATE KEYSPACE <identifier> WITH
<properties>
– Example:
– CREATE KEYSPACE “KeySpace Name” WITH
replication = {'class': ‘Strategy name’,
'replication_factor' : ‘No.Of replicas’};
Replication

• The replication option is to specify the Replica Placement strategy


and the number of replicas wanted. The following table lists all the
replica placement strategies.
Alter Keyspace

• ALTER KEYSPACE can be used to alter properties


such as the number of replicas and the
durable_writes of a KeySpace.
• Syntax:
– ALTER KEYSPACE <identifier> WITH
<properties>
– Example:
– ALTER KEYSPACE “KeySpace Name”
WITH replication = {'class': ‘Strategy name’,
'replication_factor' : ‘No.Of replicas’};
Drop Keyspace

• You can drop a KeySpace using the command


DROP KEYSPACE. Given below is the syntax for
dropping a KeySpace.
• Syntax:
– DROP KEYSPACE <identifier>

• Example:
– DROP KEYSPACE tushar;
Create table

CREATE TABLE tablename(


column1 name datatype PRIMARYKEY,
column2 name data type,
column3 name data type)
• Example:
CREATE TABLE emp(
emp_id int PRIMARY KEY,
emp_name text,
emp_city text,
emp_sal varint,
emp_phone varint
);
Alter Table

• You can alter a table using the command ALTER


TABLE. Using ALTER command, you can perform the
following operations:
– Add a column
– Drop a column
– Update the options of a table using with keyword
• Example:
– ALTER TABLE emp
ADD emp_email text;
– ALTER TABLE emp DROP emp_email;
Drop and Truncate Table

• Drop table command:


– You can drop a table using the command
Drop Table.
– Example: DROP TABLE emp;
• Truncating a Table
– You can truncate a table using the TRUNCATE
command. When you truncate a table, all the
rows of the table are deleted permanently.
– Example: TRUNCATE student;
Create index

• You can create an index in Cassandra using the


command CREATE INDEX. Its syntax is as
follows:
• CREATE INDEX <identifier> ON <tablename>
• Given below is an example to create an index to
a column. Here we are creating an index to a
column ‘emp_name’ in a table named emp .
• CREATE INDEX name ON emp1 (emp_name);
Batch statements

• Using BATCH, you can execute multiple modification statements (insert,


update, delete) simultaneously Its syntax is as follows:
BEGIN BATCH
<insert-stmt> / <update-stmt> / <delete-stmt>
APPLY BATCH
• Example:
BEGIN BATCH
... INSERT INTO emp (emp_id, emp_city, emp_name,
emp_phone, emp_sal) values ( 4,'Pune','rajeev',
9848022331, 30000);
... UPDATE emp SET emp_sal = 50000 WHERE emp_id =3;
... DELETE emp_city FROM emp WHERE emp_id = 2;
... APPLY BATCH;
Insert statement
Updating a table

• UPDATE is the command used to update data in a table.


The following keywords are used while updating data
in a table:
– Where: This clause is used to select the row to be
updated.
– Set: Set the value using this keyword.
– Must: Includes all the columns composing the
primary key.
• Example:
– UPDATE emp SET emp_city='Delhi',
emp_sal=50000 WHERE emp_id=2;
Updating a table
Reading a data
Reading a data
Deleting a data
Deleting a data
CQL Data types
CQL Data types
CQL Collection types
CQL Collections

• CQL provides the facility of using Collection


data types.
• Using these Collection types, you can store
multiple values in a single variable.
– List
– Map
– Set
Lists

• List is used in the cases where


– the order of the elements is to be maintained, and
– a value is to be stored multiple times.
• You can get the values of a list data type using the index of
the elements in the list.
Set

• Set is a data type that is used to store a group of elements.


• The elements of a set will be returned in a sorted order.
Set operations
Map

• Map is a data type that is used to store a key-value pair of


elements.
Thank you
This presentation is created using LibreOffice Impress 4.2.8.2, can be used freely as per GNU General Public License

Web Resources Blogs


http://mitu.co.in http://digitallocha.blogspot.in
http://tusharkute.com http://kyamputar.blogspot.in

[email protected]

You might also like