[英]Is there a consistent hashing algorithm that support zero remapping of keys?
[英]Cassandra hashing algorithm with composite keys
我試圖了解Cassandra用於生成復合分區鍵的murmur3哈希的算法。 我知道我可以直接從CQL獲取值,但是我想直接從Java / scala代碼為任何給定的元組重現Cassandra的行為。
對於簡單的分區鍵,以下函數計算正確的值(至少在很多情況下,通過查看源代碼,我知道它是不正確的):
long l = com.google.common.hash.Hashing.Hashing.murmur3_128()。hashString(“ my-string”,Charset.forName(“ UTF-8”))。asLong();
如果我在分區鍵上有兩列怎么辦?
兩個字符串的串聯的哈希值不相同。
感謝您提供有關該算法的更多詳細信息。 我編寫了示例代碼以共享解決方案。
byte[] keyBytes;
try(ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(bos)) {
String[] keys = new String[] {"key1", "key2"};
for(String key : keys) {
byte[] arr = key.getBytes("UTF-8");
out.writeShort(arr.length);
out.write(arr, 0, arr.length);
out.writeByte(0);
}
out.flush();
keyBytes = bos.toByteArray();
}
long hash = Hashing.murmur3_128().hashBytes(keyBytes).asLong();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.