2017-07-25 66 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連接器。