Cassandra – Command Line Operations

1. Install Cassandra

$ brew install cassandra

2. Start Cassandra

Using Brew:

 
$ brew services start cassandra

Without Brew:

 
$ cassandra 

3. Check if it’s up and running

Using Brew:

 
$ brew info cassandra 

Without Brew:

 
$ ps -ax | grep cassandra 

4. Stop Cassandra

Using Brew:

 
$ brew services stop cassandra 

Without Brew:

 
$ ps -ax | grep cassandra 
$ kill [pid] 

5. Start Cassandra Query Language Command Line Tool

 
$ cqlsh

5. Create KeySpace

A keyspace is the top-level database object that controls the replication for the object it contains at each datacenter in the cluster. Keyspaces contain tables, materialized views and user-defined types, functions, and aggregates.

 
cqlsh> CREATE KEYSPACE college
    WITH REPLICATION = {
      'class' : 'SimpleStrategy',
      'replication_factor' : 1
};

Replication factor: It is the number of machines in the cluster that will receive copies of the same data.

Replica placement strategy: It is nothing but the strategy to place replicas in the ring. We have strategies such as simple strategy (rack-aware strategy), old network topology strategy (rack-aware strategy), and network topology strategy (datacenter-shared strategy).

6. Get List of KeySpaces

 
cqlsh> DESC KEYSPACES; 

You’ll see output containing the list of keyspaces available in database as below.

system_schema college system_auth system

7. Switch to a keyspace

cqlsh> USE college

 

8. Create Table

 
cqlsh> CREATE TABLE college.students
(student_id int, first_name text, last_name text,
classes_taken frozen<set>, PRIMARY KEY (student_id)); 

A frozen value serializes multiple components into a single value. Non-frozen types allow updates to individual fields. Cassandra treats the value of a frozen type as a blob. The entire value must be overwritten.

Instead of set<text> type, Cassandra also provides the list and map data types. But unfortunately, Cassandra Query Language does not provide any way to get the size of the collections in the database. For example, there is no way to know the size of a set in classes_taken column.

8. Insert data into Table

 
cqlsh> INSERT INTO college.students (student_id,first_name,last_name, classes_taken)
VALUES( 12345, 'Rishabh', 'Jain', {'CSE508', 'CSE527', 'CSE512'});

 

9. Select all Rows from the Table

 
cqlsh> select * from college.students

Screen Shot 2019-05-08 at 1.52.41 AM.png

9. Select Row by Primary Key

 
cqlsh> select * from college.students where student_id = 12345

Screen Shot 2019-05-08 at 1.53.23 AM

10. Add a Column to the table

 

 
cqlsh> alter table college.students ADD cgpa float;

11. Drop a Column to the table

 
cqlsh> alter table college.students DROP cgpa; 

12. Update Row by Primary Key

 
cqlsh> update college.students set cgpa = 3.9 where student_id = 12346;

13. Capture CQL results in a file

cqlsh> capture 'results.txt'

This will start writing your results in the file at the path given in the above command.

14. Turn off results capture

cqlsh> capture off

14. Turn on Tracing

Cassandra allows us to trace a query session; this can be used to debug queries that perform badly. On the cqlsh prompt, you can enable tracing using the command TRACING ON. After that, each query run on the prompt will be traced, and the trace output will be displayed.

Cassandra automatically saves a traced session in the system_traces keyspace. A saved session can be referenced later on for up to 24 hours.

cqlsh> tracing on

Now, if you run any command, you’ll see the tracing on the screen like below:

Screen Shot 2019-05-08 at 1.51.42 AM

15. View Old Traces Logs

 
cqlsh> select * from system_traces.events 

Screen Shot 2019-05-08 at 1.50.43 AM

16. Turn Tracing OFF

 
cqlsh> tracing off

17. Get Cassandra ADDRESS : PORT

 
cqlsh> show host 

References:

[1] https://www.vskills.in/certification/tutorial/big-data/apache-cassandra/replica-and-their-placement/

[2] https://www.tutorialspoint.com/cassandra/cassandra_tutorial.pdf

[3] https://stackoverflow.com/questions/42532743/cassandra-frozen-keyword-meaning

[4] https://docs.datastax.com/en/cql/3.3/cql/

[5] https://stackoverflow.com/questions/20555428/in-cassandra-cql-is-there-a-way-to-query-the-size-of-a-collection-column-type

[6] https://subscription.packtpub.com/book/big_data_and_business_intelligence/9781783989102/4/ch04lvl1sec37/tracing-cassandra-queries

Leave a comment