2017-07-25 69 views
0

我有以下卡桑德拉表结构:卡桑德拉Murmur3Partitioner行顺序

CREATE TABLE example.posts (
    name text, 
    post_topic text, 
    post_date timeuuid, 
    post_text text, 
    PRIMARY KEY (name, post_topic, post_date) 
) WITH CLUSTERING ORDER BY (post_topic ASC, post_date ASC) 

我的分区键是name和集群的关键是post_topic, post_date

我需要遍历表中的所有元素,所以我执行查询SELECT * FROM posts并返回数据如下。

name | post_topic | post_date       | post_text 
    tom | cassandra | 86feab80-710d-11e7-898a-176eb9e01b3a |  hi 
    tom | cassandra | 8a4dd680-710d-11e7-898a-176eb9e01b3a |  bye 
    john | cassandra | 930ee570-710d-11e7-898a-176eb9e01b3a | whats up 

我正在使用Murmur3Partitioner。

如果我通过在表中,当我这样做处理它们在代码中一个name的时间,我可以依靠的所有行对同一name未来一前一后的所有元素要循环(如tom, tom, john ,而不是tom, john, tom

根据卡桑德拉文档It is important to understand that the order in which partitioned rows are returned, depends on the order of the hashed token values and not on the key values themselves.

如果我有产生同理2个分区键,然后将我可能得到行的混了不同的名字呢?也就是说,如果汤姆和约翰所产生的同令牌会回到tom, tom, john或者它可能会混合起来,如tom, john, tom

回答

0

不同的名字会产生不同的令牌,Murmur3Partitioner确保这一点。

Cassandra通过分区键存储您的所有数据组。卡桑德拉将存储你的数据如下图所示:

------------------------------------------------------------------------------------------------------------------| 
| tom | cassandra : 86feab80-710d-11e7-898a-176eb9e01b3a | cassandra : 8a4dd680-710d-11e7-898a-176eb9e01b3a | 
|   | ---------------------------------------------------|--------------------------------------------------| 
|   |     hi        |     bye        |  
|-----------------------------------------------------------------------------------------------------------------|  
| john | cassandra : 930ee570-710d-11e7-898a-176eb9e01b3a | 
|   |----------------------------------------------------| 
|   |    whats up       | 
---------------------------------------------------------------- 

你可以看到所有分区键顶部的卡桑德拉的内部结构在同一行中的数据。 Cassandra按分区扫描分区,按分区键的标记排序。

所以cassandra会选择一个parition并不断返回该分区的所有值。然后下一个分区。 在你的情况下,或者“汤姆汤姆,约翰”或“约翰,汤姆汤姆”

0

MurmurHash3

当前版本为MurmurHash3其产生一个32位的 或128位的散列值。当使用128位时,x86和x64版本 不会生成相同的值,因为算法针对其各自的平台进行了优化。

Cassandra将返回按集群密钥排序的每个分区键的数据。

在您的案例中,name的数据将按post_topicpost_date排序。

所以返回的数据可以tom,tom,john OR john,tom,tom ...但它永远不会是汤姆·约翰,汤姆·...

Murmur3哈希不会给副本令牌不同的分区键。

注意:Select * from table可能会导致超时如果表是巨大的......不知道你的用例......但你可能想看看spark-cassandra连接器。