Cassandraを触ってみています。
Cassandra 3.7をDebian 8.5(jessie)にaptでインストールするCassandra 3.7 cqlshでテーブルを作成してデータの登録、検索Cassandra 外部アクセスを許可しPython(Datastax)で接続する同じ構成の端末を2台用意し、クラスタ環境の構築を試してみます。
環境
OS:Debian 8.5
Cassandra:3.7
1台目IP:192.168.1.102
2台目IP:192.168.1.104
こんな環境です。
1台目の設定
1台目(192.168.1.102)の設定ファイルを編集していきます。
こちらを参考にしました。
Configuring Cassandra6. Cassandraのクラスタ構築設定ファイルを編集
# vi /etc/cassandra/cassandra.yaml
変更箇所は390行目付近のseedsの設定。
「127.0.0.1」となっている箇所を「192.168.1.102」に変更します。
seed_provider:
# Addresses of hosts that are deemed contact points.
# Cassandra nodes use this list of hosts to find each other and learn
# the topology of the ring. You must change this if you are running
# multiple nodes!
- class_name: org.apache.cassandra.locator.SimpleSeedProvider
parameters:
# seeds is actually a comma-delimited list of addresses.
# Ex: "<ip1>,<ip2>,<ip3>"
- seeds: "192.168.1.102"
次は525行目。
listen_addressを「localhost」から「192.168.1.102」に変更します。
#listen_address: localhost
listen_address: 192.168.1.102
加えて、外部からアクセス可能なように構成しました。
Cassandra 外部アクセスを許可しPython(Datastax)で接続する580行目の「start_rpc」をfalseからtrueに。
#start_rpc: false
start_rpc: true
600行目の「rpc_address」をlocalhostからサーバーのIPアドレスに。
#rpc_address: localhost
rpc_address: 192.168.1.102
これで1台目の設定は完了です。
cassandraを再起動しました。
# service cassandra restart
2台目の設定
2台目(192.168.1.104)の設定ファイルを編集していきます。
設定ファイルを編集
# vi /etc/cassandra/cassandra.yaml
変更箇所は390行目付近のseedsの設定。
「127.0.0.1」となっている箇所を「192.168.1.102」に変更します。
※192.168.1.104ではないことに注意。
seed_provider:
# Addresses of hosts that are deemed contact points.
# Cassandra nodes use this list of hosts to find each other and learn
# the topology of the ring. You must change this if you are running
# multiple nodes!
- class_name: org.apache.cassandra.locator.SimpleSeedProvider
parameters:
# seeds is actually a comma-delimited list of addresses.
# Ex: "<ip1>,<ip2>,<ip3>"
- seeds: "192.168.1.102"
次は525行目。
listen_addressを「localhost」から「192.168.1.104」に変更します。
#listen_address: localhost
listen_address: 192.168.1.104
580行目の「start_rpc」をfalseからtrueに。
#start_rpc: false
start_rpc: true
600行目の「rpc_address」をlocalhostからサーバーのIPアドレスに。
#rpc_address: localhost
rpc_address: 192.168.1.104
これで2台目の設定は完了です。
cassandraを再起動しました。
# service cassandra restart
nodetoolで状態を表示してみます。
# nodetool status
Datacenter: datacenter1
=======================
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
-- Address Load Tokens Owns (effective) Host ID Rack
UN 192.168.1.104 223.81 KiB 256 49.2% 2a03c49b-6a65-4b73-9eb0-428da65e97b1 rack1
UN 192.168.1.102 257.18 KiB 256 50.8% 20067ac6-49aa-4987-9c88-bef1e4e01514 rack1
2つのノードを認識してくれたようです。
Owns (effective)
Owns (effective)は、データをどのように配分して保持しているかの割合のようです。
各々のノードが50%なので、半分ずつデータを保持しているということでしょうか。
これ、調べてみるとどうやら「CREATE KEYSPACE」で作成した時のオプションに依存するようです。
Cassandra 3.7 cqlshでテーブルを作成してデータの登録、検索
CREATE KEYSPACE sample
WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 1};
値に1を指定しています。
Pythonのドライバーからの引用ですが、
http://datastax.github.io/python-driver/api/cassandra.html#cassandra.ConsistencyLevel
ANY = 0
Only requires that one replica receives the write or the coordinator stores a hint to replay later. Valid only for writes.
ONE = 1
Only one replica needs to respond to consider the operation a success
TWO = 2
Two replicas must respond to consider the operation a success
THREE = 3
Three replicas must respond to consider the operation a success
QUORUM = 4
ceil(RF/2) replicas must respond to consider the operation a success
ALL = 5
All replicas must respond to consider the operation a success
LOCAL_QUORUM = 6
Requires a quorum of replicas in the local datacenter
EACH_QUORUM = 7
Requires a quorum of replicas in each datacenter
SERIAL = 8
For conditional inserts/updates that utilize Cassandra’s lightweight transactions, this requires consensus among all replicas for the modified data.
LOCAL_SERIAL = 9
Like SERIAL, but only requires consensus among replicas in the local datacenter.
LOCAL_ONE = 10
Sends a request only to replicas in the local datacenter and waits for one respo
1は1つのノードに書き込めばOKという指定でした。
「ALTER KEYSPACE」で「5:ALL」に変更してみます。
cqlsh:sample> ALTER KEYSPACE sample WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 5};
cqlsh:sample>
各々のノードのOwns (effective)が100%になりました。
# nodetool status
Datacenter: datacenter1
=======================
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
-- Address Load Tokens Owns (effective) Host ID Rack
UN 192.168.1.104 270.01 KiB 256 100.0% 2a03c49b-6a65-4b73-9eb0-428da65e97b1 rack1
UN 192.168.1.102 237.36 KiB 256 100.0% 20067ac6-49aa-4987-9c88-bef1e4e01514 rack1
【参考URL】
Configuring Cassandra6. Cassandraのクラスタ構築