Search code examples
cassandracassandra-2.0cqlshcassandra-2.1cassandra-3.0

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!


Solution

  • 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.