Skip to content

Cassandra

Cassandra Important Queries

  1. systectl cassandra status/ start/ stop
  2. nodetool status
  3. CREATE KEYSPACE testkeyspace WITH replication = {'class': 'SimpleStrategy', replication_factor:'1' } AND durable_write= 'true';
  4. DESCRIBE KEYSPACES
  5. USE testkeyspace;
  6. CREATE TABLE employee_by_id (id int primary key, name text, position text);
  7. DESCRIBE TABLES;
  8. DROP TABLE employee_by_id;
  9. CREATE TABLE empleoyee_by_carmake (car_make text, id int , car_model text, PRIMARY KEY (car_make, id)); Note: in the above query we create a composite query by addding the partition key (car_make ) and primary key
  10. CREATE TABLE employee_by_carmake_sorted (car_make text, age int. name text, PRIMARY KEY (car_make, age, id));
  11. CREATE TABLE employee_by_carmake_and_model (car_make text, car_model text, name text, PRIMARY KEY ((car_make, car_model), id));
  12. SELECT car_make, car_model writetime(car_model) FROM employee_by_carmake;
  13. UPADTE employee_by_car_make SET car_model= "TRUCK" WHERE car_make="BMW" AND id=1;
  14. UPDATE employee_by_car_make USING TTL 60 SET car_model ="TRUCK" WHERE car_make="BMW" AND id=1;
  15. ALTER TABLE employee_by_id ADD phone set; // could have been list also - > list id ordered set - > set is unordered;
  16. UPDATE employee_by_id SET phone= {'1234','1323','1343'} WHERE id=1; // WHERE clause should mention the entire primary key
  17. UPDATE employee_by_id SET phone= phone+{'5454'} WHERE id=1; // WHERE clause should mention the entire primary key
  18. CREATE INDEX ON employee_id (name); // Not recommended but you can create secondary index later.
  19. CREATE TABLE employee_by_uuid (id uuid PRIMARY KEY, first_name text, last_name text);
  20. INSERT INTO employee_bu_uuid VALUES (uuid(), 'tom','jerry');
  21. CREATE TABLE employee_by_timeuuid (id timeuuid PRIMARY KEY, first_name text, last_name text);
  22. INSERT INTO employee_bu_uuid VALUES (now(), 'tom','jerry');
  23. CREATE TABLE purchase_by_customer_id (id uuid PRIMARY KEY, purchases counter);
  24. UPDATE purchase_bu_customer_id SET purchase = purchase + 1 WHERE id = uudi());
  25. CREATE MATERIALIZED VIEW test_keyspace.employee_by_department AS SELECT * FROM test_keyspace.employee_by_car_make WHERE department_id IS NOT NULL, car_make IS NOT NULL, car_model IS NOT NULL, id IS NOT NULL PRIMARY KEY (department_id, car_make, car_model, id); // this will create materialized view -> without creating extra table, that can be queried using department_id