走進cassandra之三 分區器

從這節開始,都是高年級知識了,須要一點分佈式的基礎知識。node

之前的文章說到, james(公司大佬)把任務分給了幾撥人來作,那麼怎麼個分法呢? 什麼任務交給什麼人呢?
James估計是有本身的一套算法的, 在cassandra裏面,這個對應的概念就叫 data partitioning.(數據分區)
數據分區,也是有一套算法的,裏面有這3個重要參數:
• A partitioner that determines which node to store the data on.
• The number of copies of data, which is determined by the replica placementstrategy.
• The topology of the cluster, which is the number of nodes, the distributionof the nodes on racks, and the number
of data centers.算法

第一個參數說的是,你是用的什麼分區器,打算怎麼分區,分區的意思,就是數據分佈,怎麼把數據分佈到不少不少的節點上。
第二個參數說的是,你的數據,有多少個副本,你是確定不能只有 一份數據的,不然你這臺死掉了,就消失了,必須準備好備胎。
第三個參數說的是,網絡拓撲結構,數據中心,機架。你想一想,機器老是放在機架上的,機架老是放在機房裏的,機房老是放在大樓上的,一個大樓就能夠視爲一個數據中心,你要是南京,北京,上海,有三個機房,那你就是三個數據中心了。網絡

在cassandra裏面,這一堆大量的數據,表現爲一個環(ring),爲何是一個環兒呢?
就是一個圓,無頭無尾,自稱一體,你要是看圖的話,書上舉的例子,都是畫了一個圓,來講明數據咋分佈的。就是james和韶涵他們手拉手,組織長了一個環兒,翩翩起舞。app

環裏的每個節點,負責一部分數據。(one or more ranges of the data)。
同時,環裏的節點,都持有一個令牌(token),這個令牌的做用,就是肯定 環裏翩翩起舞的韶涵,是真正的韶涵。
拿着令牌,james給你分任務,讓你承包哪一塊。dom

對於列族來講,承包給誰,是經過 row key來標示的。(Columnfamily data is partitioned across the nodes based on the row key). 一個節點,負責一小塊區域的row key.
上面說了,要搞承包,怎麼個包法呢?用什麼辦法呢?
Cassandra offers the following partitioners:分佈式

• RandomPartitioner翻譯

• ByteOrderedPartitionertoken

cassandra提供了2個辦法,第一個辦法叫RandomPartitioner。
啥意思呢?
擲骰子。ip

你們不要小瞧骰子,在不少狀況下,這都是最公平的一種方式。
爲了保證公平,得有一個算法來擲骰子。hash

The RandomPartition uses consistent hashing to determinewhich node will own a particular row。

這個算法翻譯過來叫, 一致性哈希算法。
哈希哈希,嘻嘻哈哈,稀里嘩啦,都散架了。

To distribute the data (including replicas) evenly acrossthe nodes, a hashing algorithm creates
an MD5 hash value of the row key.
這個分法,就是強調公平(evenly),各個節點,誰也別有怨言,上天來定,童叟無欺。

公平就是最好嗎?
NO.
公平不見得有效率。

因此,咱們還第二種算法來分包。
ByteOrderedPartitioner
這個是什麼意思呢?
按照byte 的順序, 來組織分區數據。
這個比剛纔擲骰子不同的地方是,它是有順序的。
有順序有什麼用呢?
能夠舉個例子,把這一排人,按照個高個矮 排個隊,排好對以後呢,若是你想要找 1.7 -1.8 米的人,就很容易找到了。
在cassandra裏對應的例子就是:
Using the ordered partitioner allows range scans over rows.For example, if yourapplication has user names as the row key, you can scan rows
for users whose names fall between Jake and Joe.

你能夠很容易找到 名字介於 Jake and Joe之間的人。

若是你有多個數據中心,分發數據就要使用另一個分區器。
NetworkTopologyStrategy
在現實世界中,多個數據中心通常分佈在不一樣的地理位置,好比說亞洲和美洲。
所以,對於這類狀況,要特殊處理一下。
The preferred replication placement strategy for multiple data centerdeployments is the  NetworkTopologyStrategy,which calculates replicaplacement per data center

這個分區策略,是基於數據中心考慮問題,舉例來講,你最少要保證,一個數據中內心,得有一個最新的記錄的副本。

上面說的,都是如何分發數據的事情。

在現實世界裏,每一個節點都不是絕對可靠的,說不定會生病,死掉,因此你的數據必需要有備份。

下篇就來講這個事, 副本機制。

相關文章
相關標籤/搜索