1)Creating a keyspace in cassandra
CREATE KEYSPACE developer WITH replication = {'class':'SimpleStrategy', 'replication_factor' : 3};
CREATE KEYSPACE developerIndian WITH REPLICATION = { 'class' : 'NetworkTopologyStrategy', 'datacenter1' : 3 } AND DURABLE_WRITES = false;
2)Droping key spaces in cassandra
Syntax:
DROP keyspace KeyspaceName
3)
In this cassandra tutorial
Main points while altering Keyspace :
- Keyspace Name: Keyspace name cannot be altered in Cassandra.
- Strategy Name: Strategy name can be altered by using a new strategy name.
- Replication Factor: Replication factor can be altered by using a new replication factor.
DURABLE_WRITES: DURABLE_WRITES value can be altered by specifying its value true/false. By default, it is true. If set to false, no updates will be written to the commit log and vice versa.
Cassandra Create Table:
In Cassandra, CREATE TABLE command is used to create a table. Here, column family is used to store data just like table in RDBMS.
So, you can say that CREATE TABLE command is used to create a column family in Cassandra.
CREATE TABLE student(
student_id int PRIMARY KEY,
student_name text,
student_city text,
student_fees varint,
student_phone varint
);
Printing data of table in cassandra
SELECT * FROM student;
Alter table in cassandra
Here we will drop two columns student_fees and student_phone.
ALTER TABLE student DROP (student_fees, student_phone);
Cassandra DROP table in cassandra
DROP TABLE student;
TRUNCATE command is used to truncate a table. If you truncate a table, all the rows of the table are deleted permanently.
Syntax:
TRUNCATE developerTable
Cassandra Batch
In Cassandra BATCH is used to execute multiple modification statements (insert, update, delete) simultaneously.
It is very useful when you have to update some column as well as delete some of the existing.
BEGIN BATCH
INSERT INTO Developer (Developer_id,Developer_fees,Developer_name) values (1,1100,'prafful');
INSERT INTO Developer (Developer_id,Developer_fees,Developer_name) values (4,4000,'rahul');
INSERT INTO Developer (Developer_id,Developer_fees,Developer_name) values (3,500,'push');
INSERT INTO Developer (Developer_id,Developer_fees,Developer_name) values (3,4000,'kavita');
UPDATE Developer SET Developer_fees=8000 where Developer_id=3;
DELETE Developer_fees FROM Developer where Developer_id=2;
APPLY BATCH
Insert script / Create Data in cassandra
INSERT command is used to insert data into the columns of the table.
INSERT INTO Developer (Developer_id, Developer_fees, Developer_name)VALUES(5,5000, 'rahul');
INSERT INTO Developer (Developer_id, Developer_fees, Developer_name)VALUES(6,3000, 'pushpa');
INSERT INTO Developer (Developer_id, Developer_fees, Developer_name)VALUES(7, 2000, 'ram');
Select script in cassandra
SELECT * FROM Developer WHERE Developer_id=2;
update statement in cassandra
UPDATE Developer SET Developer_fees=10000,Developer_name='Rahul'
WHERE Developer_id=2;
Delete script in cassadra
DELETE Developer_fees FROM Developer WHERE Developer_id=4;
Cassandra Collections
Cassandra collections are used to handle tasks. You can store multiple elements in collection. There are three types of collection supported by Cassandra:
Example of SET collection:
1)create table employee ( id int, name text, Email set
, primary key (id) );
2) INSERT INTO employee (id, email, name) VALUES(1, {'[email protected]'}, 'Ajeet');
INSERT INTO employee (id, email, name)VALUES(2,{'[email protected]'}, 'pushpa');
INSERT INTO employee (id, email, name)VALUES(3, {'[email protected]'}, 'ram');
List Collection :
The list collection is used when the order of elements matters.
Let's take the above example of "employee" table and a new column name "department" in the table employee.
alter table employee
add department list text;
insert into employee (id ,email,name,department) values (4,{'
[email protected]'},'ram',['computer Science']);
Map Collection:
The map collection is used to store key value pairs. It maps one thing to another. For example, if you want to save course name with its prerequisite course name, you can use map collection.
create table dev_course
(id int,
prereq map,
primary key(id) );
insert into dev_course (id,prereq) values (1,{'programing':'java','Neural Network': 'Artificial Intelligence','programming':'java'});
select * from dev_course