The document provides summaries of various Cassandra shell commands and CQL operations including:
1. Commands like HELP, CAPTURE, CONSISTENCY are used to get help, capture output, and set consistency levels. Keyspace operations like CREATE KEYSPACE, USE, ALTER KEYSPACE, and DROP KEYSPACE are used to create, modify and delete keyspaces.
2. Table operations such as CREATE TABLE, ALTER TABLE, DROP TABLE, TRUNCATE TABLE, CREATE INDEX and DROP INDEX allow creating, modifying and deleting tables as well as indexes.
3. CRUD operations allow inserting, updating, reading and deleting data using CQL statements. Collection, user-defined and type operations provide functionality
1. ********************************************************************************
*****************************************************
CASSANDRA BASICS - SHELL COMMANDS
********************************************************************************
*****************************************************
HELP:
====
Displays help topics for all cqlsh commands.
Ex:
--
HELP
--------------------------------------------------------------------------------
----------------------------------------------------
CAPTURE:
=======
Captures the output of a command and adds it to a file.
Ex:
--
CAPTURE '/home/hadoop/CassandraProgs/Outputfile';
verification:
------------
CAPTURE '/home/hadoop/CassandraProgs/Outputfile';
select * from emp;
CAPTURE OFF;
--------------------------------------------------------------------------------
----------------------------------------------------
CONSISTENCY:
===========
Shows the current consistency level, or sets a new consistency level.
Ex:
--
CONSISTENCY
--------------------------------------------------------------------------------
----------------------------------------------------
COPY:
====
Copies data to and from Cassandra.
Ex:
--
COPY emp (emp_id, emp_city, emp_name, emp_phone, emp_sal) TO ‘myfile‘;
--------------------------------------------------------------------------------
----------------------------------------------------
DESCRIBE:
=========
Describes the current cluster of Cassandra and its objects.
Ex:
--
DESCRIBE CLUSTER;
DESCRIBE KEYSPACES;
DESCRIBE TABLES;
DESCRIBE TABLE emp;
DESCRIBE COLUMNFAMILIES;
DESCRIBE TYPES;
DESCRIBE TYPE card_details;
--------------------------------------------------------------------------------
----------------------------------------------------
EXPAND:
======
2. Expands the output of a query vertically.
Ex
--
EXPAND ON
EXPAND OFF
verificarion:
------------
EXPAND ON
select * from emp;
EXPAND OFF
--------------------------------------------------------------------------------
----------------------------------------------------
EXIT:
====
Using this command, you can terminate cqlsh.
--------------------------------------------------------------------------------
----------------------------------------------------
PAGING:
======
Enables or disables query paging.
--------------------------------------------------------------------------------
----------------------------------------------------
SHOW:
====
Displays the details of current cqlsh session such as Cassandra version, host,
or data type assumptions.
Ex:
--
SHOW HOST;
SHOW VERSION;
--------------------------------------------------------------------------------
----------------------------------------------------
SOURCE:
======
Executes a file that contains CQL statements.
Ex:
--
SOURCE '/home/hadoop/CassandraProgs/inputfile';
--------------------------------------------------------------------------------
----------------------------------------------------
TRACING:
=======
Enables or disables request tracing.
********************************************************************************
*****************************************************
KEYSPACE OPERATIONS
********************************************************************************
*****************************************************
CREATE KEYSPACE:
====== ========
Creates a KeySpace in Cassandra.
USE:
===
Connects to a created KeySpace.
Syntax
------
CREATE KEYSPACE <identifier> WITH <properties>
3. CREATE KEYSPACE "KeySpace Name" WITH replication = {'class': ‘Strategy name‘,
'replication_factor' : ‘No.Of replicas‘};
CREATE KEYSPACE "KeySpace Name" WITH replication = {'class': ‘Strategy name‘,
'replication_factor' : ‘No.Of replicas‘} AND DURABLE_WRITES = ‘Boolean value‘;
USE <identifier>
Ex:
---
USE tutorialspoint;
CREATE KEYSPACE tutorialspoint WITH replication = {'class':'SimpleStrategy',
'replication_factor' : 3};
CREATE KEYSPACE test WITH REPLICATION = { 'class' : 'NetworkTopologyStrategy',
'datacenter1' : 3 } AND DURABLE_WRITES = false;
verification:
------------
DESCRIBE KEYSPACES;
SELECT * FROM system.schema_keyspaces;
--------------------------------------------------------------------------------
----------------------------------------------------
ALTER KEYSPACE:
===== ========
Changes the properties of a KeySpace.
Syntax
------
ALTER KEYSPACE <identifier> WITH <properties>
ALTER KEYSPACE "KeySpace Name" WITH REPLICATION = {'class': ‘Strategy name‘,
'replication_factor' : ‘No.Of replicas‘};
ALTER KEYSPACE "KeySpace Name" WITH REPLICATION = {'class': ‘Strategy name‘,
'replication_factor' : ‘No.Of replicas‘} AND DURABLE_WRITES = ‘Boolean value‘;
Ex:
--
ALTER KEYSPACE tutorialspoint WITH REPLICATION =
{'class':'NetworkTopologyStrategy', 'replication_factor' : 3};
ALTER KEYSPACE test WITH REPLICATION = {'class' : 'NetworkTopologyStrategy',
'datacenter1' : 3} AND DURABLE_WRITES = true;
verification:
------------
SELECT * FROM system.schema_keyspaces;
--------------------------------------------------------------------------------
----------------------------------------------------
DROP KEYSPACE:
==== ========
Removes a KeySpace
Syntax
------
DROP KEYSPACE <identifier>
DROP KEYSPACE "KeySpace name"
Ex:
--
DROP KEYSPACE tutorialspoint;
verification:
------------
DESCRIBE KEYSPACES;
********************************************************************************
*****************************************************
TABLE OPERATIONS
********************************************************************************
4. *****************************************************
CREATE TABLE:
====== =====
Creates a table in a KeySpace.
Syntax
------
CREATE (TABLE | COLUMNFAMILY) <tablename> ('<column-definition>' , '<column-
definition>') (WITH <option> AND <option>)
Ex:
--
CREATE TABLE emp(
emp_id int PRIMARY KEY,
emp_name text,
emp_city text,
emp_sal varint,
emp_phone varint
);
verification:
------------
DESCRIBE COLUMNFAMILIES;
--------------------------------------------------------------------------------
----------------------------------------------------
ALTER TABLE:
===== =====
Modifies the column properties of a table.
Syntax
------
ALTER (TABLE | COLUMNFAMILY) <tablename> <instruction>
ALTER TABLE table name ADD newcolumn datatype;
ALTER table name DROP column name;
Ex:
--
ALTER TABLE emp ADD emp_email text;
ALTER TABLE emp DROP emp_email;
verification:
------------
DESCRIBE TABLE emp;
--------------------------------------------------------------------------------
----------------------------------------------------
DROP TABLE:
==== =====
Removes a table.
Syntax
------
DROP TABLE <tablename>
Ex:
--
DROP TABLE emp;
verification:
------------
DESCRIBE COLUMNFAMILIES;
--------------------------------------------------------------------------------
----------------------------------------------------
TRUNCATE TABLE
======== =====
5. Removes all the data from a table.
Syntax
------
TRUNCATE <tablename>
Ex:
--
TRUNCATE student;
verification:
------------
SELECT * FROM student;
--------------------------------------------------------------------------------
----------------------------------------------------
CREATE INDEX:
====== =====
Defines a new index on a single column of a table.
Syntax
------
CREATE INDEX <identifier> ON <tablename>
Ex:
--
CREATE INDEX name ON emp1 (emp_name);
--------------------------------------------------------------------------------
----------------------------------------------------
DROP INDEX:
==== =====
Deletes a named index.
Syntax
------
DROP INDEX <identifier>
Ex:
--
DROP INDEX name;
--------------------------------------------------------------------------------
----------------------------------------------------
BATCH STATEMENTS:
===== ==========
Executes multiple DML statements at once.
Syntax
------
BEGIN BATCH
<insert-stmt>/ <update-stmt>/ <delete-stmt>
APPLY BATCH
Ex:
--
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;
verification:
------------
SELECT * FROM emp;
********************************************************************************
6. *****************************************************
CRUD OPERATIONS
********************************************************************************
*****************************************************
CREATE DATA:
====== ====
Adds columns for a row in a table.
Syntax
------
INSERT INTO <tablename> (<column1 name>, <column2 name>....) VALUES (<value1>,
<value2>....) USING <option>
Ex:
--
INSERT INTO emp (emp_id, emp_name, emp_city, emp_phone, emp_sal) VALUES(1,'ram',
'Hyderabad', 9848022338, 50000);
INSERT INTO emp (emp_id, emp_name, emp_city, emp_phone, emp_sal)
VALUES(2,'robin', 'Hyderabad', 9848022339, 40000);
INSERT INTO emp (emp_id, emp_name, emp_city, emp_phone, emp_sal)
VALUES(3,'rahman', 'Chennai', 9848022330, 45000);
verification:
------------
SELECT * FROM emp;
--------------------------------------------------------------------------------
----------------------------------------------------
UPDATE DATA
====== ====
Updates a column of a row.
Syntax
------
UPDATE <tablename> SET <column name> = <new value> <column name> = <value>....
WHERE <condition>
Ex:
--
UPDATE emp SET emp_city='Delhi',emp_sal=50000 WHERE emp_id=2;
verification:
------------
SELECT * FROM emp;
--------------------------------------------------------------------------------
----------------------------------------------------
READ DATA:
==== ====
This clause reads data from a table.
Syntax
------
SELECT FROM <tablename>
SELECT FROM <table name> WHERE <condition>;
SELECT FROM <table name> WHERE <condition> ORDER BY <column>;
Ex:
--
SELECT * FROM emp;
SELECT emp_name, emp_sal from emp;
SELECT * FROM emp WHERE emp_sal=50000;
SELECT * FROM emp WHERE emp_sal=50000 ORDER BY sal;
--------------------------------------------------------------------------------
----------------------------------------------------
DELETE DATA:
7. ====== ====
Deletes data from a table.
Syntax
------
DELETE FROM <identifier> WHERE <condition>;
Ex:
--
DELETE emp_sal FROM emp WHERE emp_id=3;
DELETE FROM emp WHERE emp_id=3;
verification:
------------
SELECT * FROM emp;
********************************************************************************
*****************************************************
CQL COLLECTION TYPES
********************************************************************************
*****************************************************
List
====
? the order of the elements is to be maintained, and
? a value is to be stored multiple times.
create
------
CREATE TABLE data(name text PRIMARY KEY, email list<text>);
inert
-----
INSERT INTO data(name, email) VALUES ('ramu',
['abc@gmail.com','cba@yahoo.com']);
update
------
UPDATE data SET email = email +['xyz@tutorialspoint.com'] where name = 'ramu';
verifiaction
------------
SELECT * FROM data;
--------------------------------------------------------------------------------
----------------------------------------------------
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.
create
------
CREATE TABLE data2 (name text PRIMARY KEY, phone set<varint>);
insert
------
INSERT INTO data2(name, phone)VALUES ('rahman', {9848022338,9848022339});
update
------
UPDATE data2 SET phone = phone + {9848022330} where name='rahman';
verifiaction
------------
SELECT * FROM data2;
--------------------------------------------------------------------------------
8. ----------------------------------------------------
Map
===
Map is a data type that is used to store a key-value pair of elements.
create
------
CREATE TABLE data3 (name text PRIMARY KEY, address map<timestamp, text>);
insert
------
INSERT INTO data3 (name, address) VALUES ('robin', {'home' : 'hyderabad' ,
'office' : 'Delhi' } );
update
------
UPDATE data3 SET address = address+{'office':'mumbai'} WHERE name = 'robin';
verifiaction
------------
SELECT * FROM data3;
********************************************************************************
*****************************************************
CQL User-Defined Data Types
********************************************************************************
*****************************************************
CREATE TYPE:
====== ====
Creates a type in a KeySpace.
Syntax
------
CREATE TYPE <keyspace name > . <data typename> ( variable1, variable2).
Ex:
--
CREATE TYPE card_details (
num int,
pin int,
name text,
cvv int,
phone set<int>
);
verifiaction
------------
DESCRIBE TYPES;
--------------------------------------------------------------------------------
----------------------------------------------------
ALTER TYPE:
===== ====
Modifies the column properties of a type.
Syntax
------
ALTER TYPE typename ADD field_name field_type;
ALTER TYPE typename RENAME existing_name TO new_name;
Ex:
--
ALTER TYPE card_details ADD email text;
ALTER TYPE card_details RENAME email TO mail;
verifiaction