Workshop
Apache Cassandra Workshop
1 ) Keyspaces:
a) Create Keyspace:
CREATE KEYSPACE MY_KEYSPACE WITH REPLICATION = {'class':'SimpleStrategy', 'replication_factor':'1'} AND durable_writes='true';
b) Describe Keyspace:
DESCRIBE KEYSPACES;
C)Use Keyspace:
USE MY_KEYSPACE;
2)Create Table :
Here primary key is id
CREATE TABLE employee_by_id(id int PRIMARY KEY, name text, position text);
Here primary key is car_make and clustering id is ‘id’
CREATE TABLE employee_by_car_make(car_make text, id int,car_model text, name text, PRIMARY KEY (car_make, id));
Here primary key are car_make & car_model
CREATE TABLE employee_by_car_make_and_model(car_make text, car_model text, id int, name text, PRIMARY KEY((car_make, car_model), id));
3) Describe Table:
DESCRIBE TABLE employee_by_car_make;
4)Insert
INSERT INTO employee_by_id(id, name, position) VALUES (1, 'SUMITH', 'ARCHITECT');
INSERT INTO employee_by_car_make(car_make, id, car_model) VALUES ('TATA MOTORS',1, 'NEXON');
INSERT INTO employee_by_car_make_and_model(car_make,car_model, id, name) VALUES ('TATA MOTORS','NEXON', 1, 'sumith');
5) Select
SELECT * FROM employee_by_id ;
SELECT * FROM employee_by_id where id =1 ;
We cannot query for name, position but we can only query by primary key I.e id
SELECT * FROM employee_by_car_make where id = 1; INVALID
Only we can query by primary key
SELECT * FROM employee_by_car_make_and_model where car_make= 'TATA MOTORS' and car_model='NEXON' ; VALID
SELECT * FROM employee_by_car_make_and_model where car_make= 'TATA MOTORS' ; INVALID
6)UPDATE
Update using TTL lifespan is 60 sec
UPDATE employee_by_car_make USING TTL 60 SET car_modeL= 'PUNCH' WHERE car_make='TATA MOTORS' and id=1;
After 60 sec
Data is null its expired
7)Alter
alter table employee_by_id add phone set<text>;
update employee_by_id set phone={'343', '1123334'} where id =1;
8)Materialized View
Materialized views are used to create alternate query paths for your data, making it easier to retrieve information using different attributes (columns) than the primary key of the original table.
Materialized views in Cassandra are similar to a “snapshot” of the original table but with a different primary key arrangement. Whenever the base table changes (inserts, updates, deletes), those changes are automatically propagated to the materialized view, ensuring eventual consistency.
create materialized view test_mv_employee as select * from employee_by_id where phone is not null and id is not null primary key(id);
- Manual flushing of MM-table to SS-Table
If you want to manually flush the data from the memtable to the SSTable (e.g., before a maintenance task), use the following command:
nodetool flush <keyspace_name> <table_name>
To flush all data from the users table in the mykeyspace keyspace:
nodetool flush mykeyspace users
If you want to flush data from all tables in all keyspaces, run:
nodetool flush
You can check the status of the flush and other operations using:
nodetool compactionstats
10 Tracing :
Tracing On
Comments
Post a Comment