View Cassandra Partitions using CQLSH
Asked Answered
G

3

5

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!

Gratitude answered 12/9, 2017 at 1:41 Comment(1)
Where did you find that tutorial? It's got to be at least 3 years out-of-date.Rossie
P
10

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.

Pasho answered 12/9, 2017 at 11:14 Comment(1)
For keys with multiple columns, separate them with a colon, e.g: nodetool getendpoints mykeyspace mytable 'first1:first2'Laminitis
W
4

cassandra-cli is not the same thing as cqlsh. Read this for more information: https://wiki.apache.org/cassandra/CassandraCli

The simplest way to get the number of partitions (keys) is with nodetool.

nodetool tablestats <keyspace>.<table>

Keyspace and table are optional. The number of partitions is listed under the value for Number of keys (estimate).

If you want the number of rows then Chris answer is correct.

SELECT * FROM <keyspace>.<table>;

This will show you all rows in the table. Just remember that this is a very costly operation as Cassandra has to fetch this data from all nodes in the cluster that has any data for that table.

Weisman answered 12/9, 2017 at 8:2 Comment(0)
E
0

can just do a select * from table, if look at headers the partition and row keys are colored differently so can figure it out that way.

Ecclesiastes answered 12/9, 2017 at 4:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.