Pages

Friday, 13 April 2012

Collecting Cassandra RowKeys.

Cassandra-0.8  have introduced a utility to collect all the keys from the sstables.This is a nice feature in many cases like
  • If you have to collect the keys from the server which is no more in a running state.
  • If you need all the keys to migrate data to another cluster.
If the data size to be migrated is more this is a faster alternative to collect all keys.

 Using sstablekeys

To collect keys with this utility all we need is sstable.

apache_cassandra/bin/sstablekeys path_to_sstable/sstable.Data.db

Above command will start displaying  all rowkeys those belongs to this perticular sstable to standard output,one key per line.You can redirect these keys to some file for further use.

apache_cassandra/bin/sstablekeys path_to_sstable/sstable.Data.db >> testfile

once you have all the keys with you make sure you convert them into character format as they are in Hex format.

you can use following java function to convert these keys.

public static String convertHexToString(String hex)
 {

  StringBuilder sb = new StringBuilder();
  StringBuilder temp = new StringBuilder();

  // 49204c6f7665204a617661 split into two characters 49, 20, 4c...
  for (int i = 0; i < hex.length() - 1; i += 2)
  {

   // grab the hex in pairs
   String output = hex.substring(i, (i + 2));
   // convert hex to decimal
   int decimal = Integer.parseInt(output, 16);
   // convert the decimal to character
   sb.append((char) decimal);

   temp.append(decimal);
  }

  return sb.toString();
 }

Once you have converted keys you are ready to use these keys as you want.

No comments: