Nic*_*aro 2 algorithm hash cassandra murmurhash
我试图了解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();
Run Code Online (Sandbox Code Playgroud)