Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 具有复合密钥的Cassandra散列算法_Algorithm_Hash_Cassandra_Murmurhash - Fatal编程技术网

Algorithm 具有复合密钥的Cassandra散列算法

Algorithm 具有复合密钥的Cassandra散列算法,algorithm,hash,cassandra,murmurhash,Algorithm,Hash,Cassandra,Murmurhash,我试图理解Cassandra使用什么算法来生成复合分区键的3个散列。我知道我可以直接从CQL获得值,但我想直接从Java/scala代码中复制任意给定元组的Cassandra行为 对于简单的分区键,以下函数计算正确的值(至少在许多情况下,我通过查看源代码知道它不精确): long l=com.google.common.hash.Hashing.Hashing.3_128().hashString(“我的字符串”,Charset.forName(“UTF-8”)).asLong() 如果分区键上

我试图理解Cassandra使用什么算法来生成复合分区键的3个散列。我知道我可以直接从CQL获得值,但我想直接从Java/scala代码中复制任意给定元组的Cassandra行为

对于简单的分区键,以下函数计算正确的值(至少在许多情况下,我通过查看源代码知道它不精确):

long l=com.google.common.hash.Hashing.Hashing.3_128().hashString(“我的字符串”,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();

可能重复的问题请点击以上问题的链接,并查看对答案的评论。