Hash value from keys on Cassandra

2019-09-05 05:13发布

问题:

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.

回答1:

Hector's CqlQuery is poorly supported and buggy. You should use the native Java CQL driver instead: https://github.com/datastax/java-driver



回答2:

You could just reuse the partitioners defined in Cassandra: https://github.com/apache/cassandra/tree/trunk/src/java/org/apache/cassandra/dht and then using the token ranges you could do the routing.



回答3:

The CQL driver offers token-aware routing out of the box. I would use that instead of trying to reinvent the wheel in Hector, especially since Hector uses the legacy Thrift API instead of CQL.



回答4:

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());
            }

    }