PolarSPARC

Cassandra Quick Notes :: Part - 3


Bhaskar S *UPDATED*11/28/2023


Overview


In Part-2 of this article series, we performed the necessary setup to start a 3-node Apache Cassandra cluster and got our hands dirty with some concepts.

In this part, we will continue to leverage the 3-node Apache Cassandra cluster setup. In addition, we will elaborate more on the concepts from Part-2 and introduce additional concepts on Apache Cassandra.


Concepts - Level III


The following section elaborates and expands on the concepts on Apache Cassandra:


Hands-on with Cassandra - III


To check the Apache Cassandra cluster nodes are up and running, execute the following command:


$ docker exec -it cas-node-3 nodetool describecluster


The following would be the typical output:


Output.1

Cluster Information:
  Name: Cassandra Cluster
  Snitch: org.apache.cassandra.locator.SimpleSnitch
  DynamicEndPointSnitch: enabled
  Partitioner: org.apache.cassandra.dht.Murmur3Partitioner
  Schema versions:
    d03783d7-b468-3c1a-82f1-8e30b2edde8b: [172.18.0.2, 172.18.0.3, 172.18.0.4]

Stats for all nodes:
  Live: 3
  Joining: 0
  Moving: 0
  Leaving: 0
  Unreachable: 0

Data Centers: 
  datacenter1 #Nodes: 2 #Down: 0

Database versions:
  5.0.0-alpha2: [172.18.0.2:7000, 172.18.0.3:7000, 172.18.0.4:7000]

Keyspaces:
  system_auth -> Replication class: SimpleStrategy {replication_factor=1}
  system_distributed -> Replication class: SimpleStrategy {replication_factor=3}
  system_traces -> Replication class: SimpleStrategy {replication_factor=2}
  system_schema -> Replication class: LocalStrategy {}
  system -> Replication class: LocalStrategy {}

To launch the CQL command-line interface, execute the following command:


$ docker run -it --rm --name cas-client --network cassandra-db-net cassandra:5.0 cqlsh cas-node-1


The following will be the output:


Output.2

WARNING: cqlsh was built against 5.0-alpha2, but this server is 5.0.  All features may not work!
Connected to Cassandra Cluster at cas-node-1:9042
[cqlsh 6.2.0 | Cassandra 5.0-alpha2 | CQL spec 3.4.7 | Native protocol v5]
Use HELP for help.
cqlsh>

On success, CQL will change the command prompt to "cqlsh>".

To create a Keyspace called mytestks3, input the following command at the "cqlsh>" prompt:


cqlsh> CREATE KEYSPACE IF NOT EXISTS mytestks3 WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 2};


There will be no output.

To use the Keyspace called mytestks3, input the following command at the "cqlsh>" prompt:


cqlsh> USE mytestks3;


There will be no output and the input prompt would change to "cqlsh:mytestks3>".

To create a Table called club_member with a composite Primary Key, input the following command at the "cqlsh:mytestks3>" prompt:


cqlsh:mytestks3> CREATE TABLE IF NOT EXISTS club_member (member_id uuid, member_name text, member_phone text, member_since timestamp, zip text, PRIMARY KEY ((member_id), zip));


There will be no output.

To check the various settings on the table club_member, input the following command at the "cqlsh:mytestks3>" prompt:


cqlsh:mytestks3> DESCRIBE TABLE club_member;


The following will be the output:


Output.3

CREATE TABLE mytestks3.club_member (
    member_id uuid,
    zip text,
    member_name text,
    member_phone text,
    member_since timestamp,
    PRIMARY KEY (member_id, zip)
) WITH CLUSTERING ORDER BY (zip ASC)
    AND additional_write_policy = '99p'
    AND allow_auto_snapshot = true
    AND bloom_filter_fp_chance = 0.01
    AND caching = {'keys': 'ALL', 'rows_per_partition': 'NONE'}
    AND cdc = false
    AND comment = ''
    AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold': '32', 'min_threshold': '4'}
    AND compression = {'chunk_length_in_kb': '16', 'class': 'org.apache.cassandra.io.compress.LZ4Compressor'}
    AND memtable = 'default'
    AND crc_check_chance = 1.0
    AND default_time_to_live = 0
    AND extensions = {}
    AND gc_grace_seconds = 864000
    AND incremental_backups = true
    AND max_index_interval = 2048
    AND memtable_flush_period_in_ms = 0
    AND min_index_interval = 128
    AND read_repair = 'BLOCKING'
    AND speculative_retry = '99p';

For the table club_member, the column member_id will be the Partiton Key and the column zip will be the Clustering Key.

To insert a row into the table club_member, input the following commands at the " cqlsh:mytestks3>" prompt:


cqlsh:mytestks3> INSERT INTO club_member (member_id, member_name, member_since, zip) VALUES (uuid(), 'alice', '2020-05-15', '10001');


There will be no output.

To select all the rows from the table book_catalog, input the following command at the "cqlsh:mytestks>" prompt:


cqlsh:mytestks3> SELECT * FROM book_catalog;


The following will be the output:


Output.4

 member_id                            | zip   | member_name | member_phone | member_since
--------------------------------------+-------+-------------+--------------+---------------------------------
 63b807d0-a629-477c-a085-98cdf8a03770 | 10001 |       alice |         null | 2020-05-15 00:00:00.000000+0000

(1 rows)

To select the values of the member_id, the member_phone, and the latest update timestamp (column metadata) of member_phone for all the rows from the table club_member, input the following command at the "cqlsh:mytestks3>" prompt:


cqlsh:mytestks3> SELECT member_id, member_phone, writetime(member_phone) FROM club_member;


The following will be the output:


Output.5

 member_id                            | member_phone | writetime(member_phone)
--------------------------------------+--------------+-------------------------
 63b807d0-a629-477c-a085-98cdf8a03770 |         null |                    null

(1 rows)

Since no value was provided for the column member_phone during the INSERT operation, the last timestamp for the column is null, which makes sense.

To update the value of the column member_phone for the row in club_member with the primary key values of 63b807d0-a629-477c-a085-98cdf8a03770 and 10001 , input the following command at the "cqlsh:mytestks3>" prompt:


cqlsh:mytestks3> UPDATE club_member SET member_phone = '212-111-1111' WHERE member_id = 63b807d0-a629-477c-a085-98cdf8a03770 AND zip = '10001';


There will be no output.

Once again, to select the values of the member_id, the member_phone, and the latest update timestamp (column metadata) of member_phone for all the rows from the table club_member, input the following command at the "cqlsh:mytestks3>" prompt:


cqlsh:mytestks3> SELECT member_id, member_phone, writetime(member_phone) FROM club_member;


The following will be the output:


Output.6

 member_id                            | member_phone | writetime(member_phone)
--------------------------------------+--------------+-------------------------
 63b807d0-a629-477c-a085-98cdf8a03770 | 212-111-1111 |        1701205884772244

(1 rows)

To display the current consistency level set of the Apache Cassandra cluster, input the following command at the "cqlsh:mytestks3>" prompt:


cqlsh:mytestks3> CONSISTENCY;


The following will be the output:


Output.7

Current consistency level is ONE.

To set the consistency level of the Apache Cassandra cluster to QUORUM, input the following command at the "cqlsh:mytestks3>" prompt:


cqlsh:mytestks3> CONSISTENCY QUORUM;


The following will be the output:


Output.8

Consistency level set to QUORUM.

To check which nodes in the cluster stored the row for 63b807d0-a629-477c-a085-98cdf8a03770, execute the following command:


$ docker exec -it cas-node-1 nodetool getendpoints mytestks3 club_member 63b807d0-a629-477c-a085-98cdf8a03770


The following will be the output:


Output.9

172.18.0.2
172.18.0.4

Note the IP address 172.18.0.4 is that of the node cas-node-3.

Let us now take DOWN the node cas-node-3 from our cluster by executing the following command:


$ docker stop cas-node-3


There will be no output.

To select the values of all the columns from the table club_member for the row with the row key of member_id = 63b807d0-a629-477c-a085-98cdf8a03770 AND zip = '10001', input the following command at the " cqlsh:mytestks3>" prompt:


cqlsh:mytestks3> SELECT * FROM club_member WHERE member_id = 63b807d0-a629-477c-a085-98cdf8a03770 AND zip = '10001';


The following will be the output:


Output.10

NoHostAvailable: ('Unable to complete the operation against any hosts', {<Host: 172.18.0.2:9042 datacenter1>: Unavailable('Error from server: code=1000 [Unavailable exception] message="Cannot achieve consistency level QUORUM" info={\'consistency\': \'QUORUM\', \'required_replicas\': 2, \'alive_replicas\': 1}')})

Let us bring UP the node cas-node-3 back in the cluster by executing the following command:


$ docker run --rm --name cas-node-3 --hostname cas-node-3 --network cassandra-db-net -e CASSANDRA_SEEDS=cas-node-1 -u $(id -u $USER):$(id -g $USER) -v $CASSANDRA_HOME/data3:/var/lib/cassandra/data -v $CASSANDRA_HOME/etc/cassandra:/etc/cassandra -v $CASSANDRA_HOME/logs3:/opt/cassandra/logs cassandra:5.0


Once again, to select the values of all the columns from the table club_member for the row with the row key of member_id = 63b807d0-a629-477c-a085-98cdf8a03770 AND zip = '10001', input the following command at the "cqlsh:mytestks3>" prompt:


cqlsh:mytestks3> SELECT * FROM club_member WHERE member_id = 63b807d0-a629-477c-a085-98cdf8a03770 AND zip = '10001';


The following will be the output:


Output.11

 member_id                            | zip   | member_name | member_phone | member_since
--------------------------------------+-------+-------------+--------------+---------------------------------
 63b807d0-a629-477c-a085-98cdf8a03770 | 10001 |       alice | 212-111-1111 | 2020-05-15 00:00:00.000000+0000

(1 rows)

Let us now take DOWN the node cas-node-2 from our cluster by executing the following command:


$ docker stop cas-node-2


One more time, to select the values of all the columns from the table club_member for the row with the row key of member_id = 63b807d0-a629-477c-a085-98cdf8a03770 AND zip = '10001', input the following command at the "cqlsh:mytestks3>" prompt:


cqlsh:mytestks3> SELECT * FROM club_member WHERE member_id = 63b807d0-a629-477c-a085-98cdf8a03770 AND zip = '10001';


The following will be the output:


Output.12

 member_id                            | zip   | member_name | member_phone | member_since
--------------------------------------+-------+-------------+--------------+---------------------------------
 63b807d0-a629-477c-a085-98cdf8a03770 | 10001 |       alice | 212-111-1111 | 2020-05-15 00:00:00.000000+0000

(1 rows)

Notice that the node cas-node-2 being DOWN did not impact the query as the consistency level was met.

Inserting new rows will not be an issue as the consitency level will be met because there are still two nodes cas-node-1 and cas-node-3 that are UP and running.

To drop the entire table club_member, input the following command at the " cqlsh:mytestks3>" prompt:

cqlsh:mytestks3> DROP TABLE club_member;


There will be no output.

To drop the entire keyspace mytestks3, input the following command at the " cqlsh:mytestks3>" prompt:


cqlsh:mytestks3> DROP KEYSPACE mytestks3;


There will be no output.

To exit the CQL command-line interface, input the following command at the " cqlsh:mytestks3>" prompt:


cqlsh:mytestks3> exit;


There will be no output.

To stop the nodes Apache Cassandra cluster, execute the following commands in that order:


$ docker stop cas-node-3

$ docker stop cas-node-2

$ docker stop cas-node-1

This concludes the hands-on demonstration for this part using the 3-node Apache Cassandra cluster !!!


References

Cassandra Quick Notes :: Part - 2

Cassandra Quick Notes :: Part - 1


© PolarSPARC