Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View Cassandra Partitions using CQLSH

Using Cassandra, how do i see how many partitions were created base on how i created the primary key? I have been following a tutorial and it mentions to go to bin/cassandra-cli and use the LIST command. However, the latest Cassandra install does not come with this and I have read other articles online that have indicated that cli is now deprecated.

Is there anyway for me to see the partitions that were created using cqlsh?

Thanks in advance!

like image 890
BigBug Avatar asked Sep 12 '17 01:09

BigBug


People also ask

How can I see my Cassandra partitions?

First of all you have to investigate your cassandra. yaml file to see the number of tokens that are currently configured. This tells you how many partitions each node will own: $ grep num_tokens conf/cassandra.

How do I find a large partition in Cassandra?

Try nodetool tablehistograms -- <keyspace> <table> command provides statistics about a table, including read/write latency, partition size, column count, and number of SSTables. This provides proper stats of the table like 95% percentile of raw_data table has partition size of 107MB and max of 3.44GB.

How do I use the token function in Cassandra?

Use the TOKEN function to express a conditional relation on a partition key column. In this case, the query returns rows based on the token of the partition key rather than on the value. As an example, you could issue: SELECT * FROM test WHERE TOKEN(username) <= TOKEN('abcf');


1 Answers

First of all you have to investigate your cassandra.yaml file to see the number of tokens that are currently configured. This tells you how many partitions each node will own:

$ grep num_tokens conf/cassandra.yaml
...
num_tokens: 128
...
$ grep initial_token conf/cassandra.yaml
...
# initial_token: 1
...

If initial token is commented out, that means that the node will figure out it's own partition ranges during start-up.

Next you can check partition ranges using nodetool ring command:

$ bin/nodetool ring

Datacenter: DC1
==========
Address    Rack        Status State   Load            Owns                Token                                       
                                                                          9167006318991683417                         
127.0.0.2  r1          Down   Normal  ?               ?                   -9178420363247798328                        
127.0.0.2  r1          Down   Normal  ?               ?                   -9127364991967065057                        
127.0.0.3  r1          Down   Normal  ?               ?                   -9063041387589326037 

This shows you which partition range belongs to which node in the cluster.

In the example above each node owns 128 partition ranges. The range between -9178420363247798327 and -9127364991967065057 belongs to the node 127.0.0.2.

You can use this simple select to tell each row's partition key:

cqlsh:mykeyspace> select token(key), key, added_date, title from mytable;

 system.token(key)    | key       | added_date               | title
----------------------+-----------+--------------------------+----------------------
 -1651127669401031945 |  first    | 2013-10-16 00:00:00+0000 | Hello World
 -1651127669401031945 |  first    | 2013-04-16 00:00:00+0000 | Bye World
   356242581507269238 | second    | 2014-01-29 00:00:00+0000 | Lorem Ipsum
   356242581507269238 | second    | 2013-03-17 00:00:00+0000 | Today tomorrow
   356242581507269238 | second    | 2012-04-03 00:00:00+0000 | It's good to meet you

(5 rows)

Finding the partition key in partition ranges will tell you where the record is stored.

Also you can use nodetool to do the same in one simple step:

$ bin/nodetool getendpoints mykeyspace mytable 'first'
127.0.0.1
127.0.0.2

This tells where the records with the partition key 'first' are located.

NOTE: If some of the nodes are down, getendpoints command won't list those nodes, even though they should store the record based on replication settings.

like image 95
Oresztesz Avatar answered Sep 21 '22 20:09

Oresztesz