简体   繁体   English

如何为复合分区密钥生成Cassandra令牌?

[英]How to generate Cassandra Token for composite partition key?

My Cassandra ColumnFamily uses the Murmur3Partitioner, and has a composite partition key. 我的Cassandra ColumnFamily使用Murmur3Partitioner,并且具有复合分区键。 With this partitioner I was trying to create a Token, however it seems this token factory only allows Long values. 使用此分区程序,我试图创建一个令牌,但是似乎此令牌工厂仅允许使用Long值。 Is it possible to generate these hashes for something like "token(partition_column1, partition_column2)"? 是否可以为“ token(partition_column1,partition_column2)”之类的东西生成这些哈希?

It's supposed to work. 应该可以的 In fact, if your partition key is composite, you should be unable to create a token for only a single column. 实际上,如果您的分区键是复合键,则您将无法仅为单个列创建令牌。 Are you sure that you've defined the composite key properly? 您确定已经正确定义了组合键吗?

cqlsh:testks> create table t1(k1 int, k2 text, v text, primary key((k1, k2)));
cqlsh:testks> insert into t1(k1, k2, v) values (1, 'key', 'value');
cqlsh:testks> select * from t1;

 k1 | k2  | v
----+-----+-------
  1 | key | value

(1 rows)

cqlsh:testks> select token(k1) from t1;
Bad Request: Invalid number of arguments in call to function token: 2 required but 1 provided
cqlsh:testks> select token(k2) from t1;
Bad Request: Invalid number of arguments in call to function token: 2 required but 1 provided
cqlsh:testks> select token(k1, k2) from t1;

 token(k1, k2)
---------------------
 8064425790465501062

    (1 rows)

This is the formula for calculating Hash, I found on Datastax web : 这是计算哈希的公式,我在Datastax网站上发现:

(((2**64 / number_of_tokens) * i) - 2**63) for i in range(number_of_tokens)

This hashing function creates a 64-bit hash value of the partition key 此哈希函数创建分区键的64位哈希值

So the Hash can be in range of -2^63 to + 2^63-1 ( 2 to power 63 ) 因此哈希值可以在-2 ^ 63到+ 2 ^ 63-12等于幂63 )的范围内

The Algorithm to compute the token for a composite partition key : Primary_key((text, int)) -> therefore the partition key is a composite_partition_key (text, int). 用于计算复合分区键的令牌的算法为:Primary_key((text,int))->因此,分区键为Composite_partition_key(text,int)。

Example : a row with composite_partition_key ('hello', 1) 示例:具有Composite_partition_key('hello',1)的行

Applying the algorithm : 应用算法:

1- lay-out the components of the composite partition key in big-endian (16 bits) presentation : 1-在big-endian(16位)表示中布置复合分区键的组件:

first_component = 'hello' -> 68 65 6c 6c 6f first_component ='hello'-> 68 65 6c 6c 6f

sec_component = 1 -> 00 00 00 01 sec_component = 1-> 00 00 00 01

68 65 6c 6c 6f 00 00 00 01 68 65 6c 6c 6f 00 00 00 01

2- add two-bytes length of component before each component 2-在每个组件之前添加组件的两个字节的长度

first_component = 'hello', length= 5-> 00 05 68 65 6c 6c 6f first_component ='hello',长度= 5-> 00 05 68 65 6c 6c 6f

sec_component = 1, therefore length= 4 -> 00 04 00 00 00 01 sec_component = 1,因此长度= 4-> 00 04 00 00 00 01

00 05 68 65 6c 6c 6f 00 04 00 01 00 05 68 65 6c 6c 6f 00 04 00 01

3- add zero-value after each component 3-在每个组件之后添加零值

first_component = 'hello' -> 00 05 68 65 6c 6c 6f 00 first_component ='hello'-> 00 05 68 65 6c 6c 6f 00

sec_component = 1 -> 00 04 00 00 00 01 00 sec_component = 1-> 00 04 00 00 00 01 00

4- result 4结果

00 05 68 65 6c 6c 6f 00 00 04 00 00 00 01 00 00 05 68 65 6c 6c 6f 00 00 04 00 00 00 01 00

now pass the result as whatever binary base your murmur3 function understand (make sure it's cassandra variant). 现在将结果作为murmur3函数可以理解的二进制基础传递(确保它是cassandra变体)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM