Search code examples
hashcassandrakeyhectorpartitioner

Hash value from keys on Cassandra


I'm developing a mechanism for Cassandra using Hector. What I need at this moment is to know which are the hash values of the keys to look at which node is stored (looking at the tokens of each one), and ask directly this node for the value. What I understood is that depending on the partitioner Cassandra uses, the values are stored independently from one partitioner to other. So, are the hash values of all keys stored in any table? In case not, how could I implement a generic class that once I read from System Keyspace the partitioner that is using Cassandra this class could be an instance of it without the necessity of modifying the code depending on the partitioner? I would need it to call the getToken method to calculate the hash value for a given key.


Solution

  • Finally after testing different implementations I found the way to get the partitioner using the next code:

                CqlQuery<String, String, String> cqlQuery = new CqlQuery<String, String, String>(
                ksp, StringSerializer.get(), StringSerializer.get(),   StringSerializer.get());
                cqlQuery.setQuery("select partitioner from local");
                QueryResult<CqlRows<String, String, String>> result = cqlQuery.execute();
                CqlRows rows = result.get();
                for (int i = 0; i < rows.getCount(); i++) {
                    RowImpl<String, String, String> row = (RowImpl<String, String, String>) rows
                    .getList().get(i);
                    List<HColumn<String, String>> column = row.getColumnSlice().getColumns();
                    for (HColumn<String , String> c: column) {
                        System.out.println(c.getValue());
                }
    
        }