cassandra集羣環境搭建

老實說,雖然cassandra在業界不是很受待見,可是但從使用上來看,cassandra仍是挺簡單的,本次測試環境仍然是UBUNTU,集羣中最簡單的使用了2個節點。html

首先安裝java,這個能夠參考我以前的博客:Ubuntu 11.04上安裝jdk1.6+oracle 11g筆記 。一系列各類JAVA_HOME什麼的配置完以後,應該能夠看到這個java

~$ java -version
java version "1.8.0"
Java(TM) SE Runtime Environment (build 1.8.0-b132)
Java HotSpot(TM) 64-Bit Server VM (build 25.0-b70, mixed mode)

如今我用的版本是java 8。而後須要從cassandra官網上下載安裝包,這個再也不多說,個人版本是apache-cassandra-2.0.6-bin.tar.gz,ubuntu中好像沒有cassandra的源。node

而後是創建數據文件和log的目錄並改變其所屬用戶和所屬用戶組,apache

XXXXX@XXXXX-asus:~$ sudo mkdir /var/lib/cassandra
XXXXX@XXXXX-asus:~$ sudo chown XXXXX /var/lib/cassandra/
XXXXX@XXXXX-asus:~$ sudo chgrp XXXXX /var/lib/cassandra/
XXXXX@XXXXX-asus:~$ sudo mkdir /var/log/cassandra
XXXXX@XXXXX-asus:~$ sudo chown XXXXX /var/log/cassandra/
XXXXX@XXXXX-asus:~$ sudo chgrp XXXXX /var/log/cassandra/
XXXXX@XXXXX-asus:~$ ll /var/lib/ | grep cas
drwxr-xr-x  2 XXXXX         XXXXX         4096  4月 13 11:09 cassandra/
XXXXX@XXXXX-asus:~$ ll /var/log/ | grep cas
drwxr-xr-x  2 XXXXX   XXXXX      4096  4月 13 11:09 cassandra/
XXXXX@XXXXX-asus:~$ 

固然,cassandra的數據和log文件是否是非要死板的放在/var/lib和/var/log下面呢?固然不是,有地方能夠配置的,這裏,爲了便於實驗,仍然採用默認配置。json

解壓cassandra的安裝包後能夠查看主要的配置文件,都在conf/目錄下,conf/cassandra.yaml比較重要,其中須要着重注意的有如下一些配置項ubuntu

cluster_name: 'TC01'
num_tokens: 256
seed_provider:
    - class_name: org.apache.cassandra.locator.SimpleSeedProvider
      parameters:
          - seeds: "192.168.2.103"
listen_address: 192.168.2.103
rpc_address: 192.168.2.103
endpoint_snitch: SimpleSnitch

cluster_name必定要修改,省得用默認的「Test Cluster」和他人衝突,全部有顯示ip的地方都須要注意並修改,將127.0.0.1和localhost改成你的對外訪問的ip,好比個人局域網ip就是192.168.2.103。session

conf/cassandra.yaml中還有改動數據文件位置的選項,搜索/var/lib相關的選項修改便可,conf/log4j-server.properties中有改動log文件位置的選項,搜索/var/log相關的選項。此次實驗中都沒有修改這些選項。oracle

而後啓動cassandra,ide

XXXXX@XXXXX-asus:~/apache-cassandra-2.0.6$ cd bin/
XXXXX@XXXXX-asus:~/apache-cassandra-2.0.6/bin$ ./cassandra -f

用-f參數啓動,能夠看到cassandra啓動超長的log,接着看一下狀態函數

XXXXX@XXXXX-asus:~/apache-cassandra-2.0.6$ cd bin/
XXXXX@XXXXX-asus:~/apache-cassandra-2.0.6/bin$ ls
cassandra          cassandra-shuffle      debug-cql.bat     sstable2json      sstableloader.bat    sstablesplit.bat
cassandra.bat      cassandra-shuffle.bat  json2sstable      sstable2json.bat  sstablemetadata.bat  sstableupgrade
cassandra-cli      cqlsh                  json2sstable.bat  sstablekeys       sstablescrub         sstableupgrade.bat
cassandra-cli.bat  cqlsh.bat              nodetool          sstablekeys.bat   sstablescrub.bat     stop-server
cassandra.in.sh    debug-cql              nodetool.bat      sstableloader     sstablesplit
XXXXX@XXXXX-asus:~/apache-cassandra-2.0.6/bin$ ./nodetool status
Datacenter: datacenter1
=======================
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
--  Address        Load       Tokens  Owns (effective)  Host ID                               Rack
UN  192.168.2.103  40.92 KB   256     100.0%            80a96105-819a-40f2-ac7c-489285017de1  rack1

如今cassandra已經啓動了節點1,節點2在一臺虛擬機上(ip:192.168.2.102),其配置與節點1很是相似,要改動配置裏面的seeds那一項的ip爲節點1的ip

cluster_name: 'TC01'
num_tokens: 256
seed_provider:
    - class_name: org.apache.cassandra.locator.SimpleSeedProvider
      parameters:
          - seeds: "192.168.2.103"
listen_address: 192.168.2.102
rpc_address: 192.168.2.102
endpoint_snitch: SimpleSnitch

以一樣的方式啓動節點2,就能夠在節點1中看到節點2已經加入到集羣。此時,節點1的log中能夠看到如下信息

 WARN 17:36:15,477 Gossip stage has 1 pending tasks; skipping status check (no nodes will be marked down)
 INFO 17:36:16,358 Handshaking version with /192.168.2.102
 INFO 17:36:17,999 Node /192.168.2.102 is now part of the cluster
 INFO 17:36:18,496 Handshaking version with /192.168.2.102
 INFO 17:36:18,727 InetAddress /192.168.2.102 is now UP
 INFO 17:36:53,523 [Stream #24d92630-c2ef-11e3-9d13-81bcfc870408] Received streaming plan for Bootstrap
 INFO 17:36:54,336 [Stream #24d92630-c2ef-11e3-9d13-81bcfc870408] Session with /192.168.2.102 is complete
 INFO 17:36:54,586 [Stream #24d92630-c2ef-11e3-9d13-81bcfc870408] All sessions completed

若是用nodetool來看,能夠看到如下信息

XXXXX@XXXXX-asus:~/apache-cassandra-2.0.6/bin$ ./nodetool status
Datacenter: datacenter1
=======================
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
--  Address        Load       Tokens  Owns (effective)  Host ID                               Rack
UN  192.168.2.102  54.74 KB   256     100.0%            00782976-b020-43a7-8fa5-b7dee95e6f1e  rack1
UN  192.168.2.103  45.75 KB   256     100.0%            80a96105-819a-40f2-ac7c-489285017de1  rack1

很清楚的能夠看到2個節點所有在線。接下去開始實驗集羣的數據同步,先登錄node1節點,創建必要的keyspace和column family

XXXXX@XXXXX-asus:~/apache-cassandra-2.0.6/bin$ ./cassandra-cli -h 192.168.2.103 -p 9160
Connected to: "TC01" on 192.168.2.103/9160
Welcome to Cassandra CLI version 2.0.6

The CLI is deprecated and will be removed in Cassandra 3.0.  Consider migrating to cqlsh.
CQL is fully backwards compatible with Thrift data; see http://www.datastax.com/dev/blog/thrift-to-cql3

Type 'help;' or '?' for help.
Type 'quit;' or 'exit;' to quit.

[default@unknown] create keyspace test;
fa857b42-d483-31a8-8ef0-b30251cf6f72
[default@unknown] use test;
Authenticated to keyspace: test
[default@test] create column family test_cf;
869e20c5-ab6f-349d-8466-a86454af18cc
[default@test] show schema;

WARNING: CQL3 tables are intentionally omitted from 'show schema' output.
See https://issues.apache.org/jira/browse/CASSANDRA-4377 for details.

create keyspace test
  with placement_strategy = 'NetworkTopologyStrategy'
  and strategy_options = {datacenter1 : 1}
  and durable_writes = true;

use test;

create column family test_cf
  with column_type = 'Standard'
  and comparator = 'BytesType'
  and default_validation_class = 'BytesType'
  and key_validation_class = 'BytesType'
  and read_repair_chance = 0.1
  and dclocal_read_repair_chance = 0.0
  and populate_io_cache_on_flush = false
  and gc_grace = 864000
  and min_compaction_threshold = 4
  and max_compaction_threshold = 32
  and replicate_on_write = true
  and compaction_strategy = 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy'
  and caching = 'KEYS_ONLY'
  and default_time_to_live = 0
  and speculative_retry = 'NONE'
  and compression_options = {'sstable_compression' : 'org.apache.cassandra.io.compress.LZ4Compressor'}
  and index_interval = 128;

可是在插入數據的時候會發生奇怪的錯誤,用ascii函數能夠解決

[default@test] set test_cf['num']['num1']=1111;
org.apache.cassandra.serializers.MarshalException: cannot parse 'num1' as hex bytes
[default@test] set test_cf[ascii('num')][ascii('num1')]=1111;
Value inserted.
Elapsed time: 97 msec(s).

緣由是由於列族中使用了BytesType,若是用UTF8Type就不會有錯了,drop以前的列族,重建

[default@test] create column family testcf  with column_type = 'Standard'  and comparator = 'UTF8Type'  and default_validation_class = 'UTF8Type'  and key_validation_class = 'UTF8Type';
08656500-2f27-345f-bcf7-c58e225fb2ff
[default@test] show schema;

WARNING: CQL3 tables are intentionally omitted from 'show schema' output.
See https://issues.apache.org/jira/browse/CASSANDRA-4377 for details.

create keyspace test
  with placement_strategy = 'NetworkTopologyStrategy'
  and strategy_options = {datacenter1 : 1}
  and durable_writes = true;

use test;

create column family testcf
  with column_type = 'Standard'
  and comparator = 'UTF8Type'
  and default_validation_class = 'UTF8Type'
  and key_validation_class = 'UTF8Type'
  and read_repair_chance = 0.1
  and dclocal_read_repair_chance = 0.0
  and populate_io_cache_on_flush = false
  and gc_grace = 864000
  and min_compaction_threshold = 4
  and max_compaction_threshold = 32
  and replicate_on_write = true
  and compaction_strategy = 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy'
  and caching = 'KEYS_ONLY'
  and default_time_to_live = 0
  and speculative_retry = 'NONE'
  and compression_options = {'sstable_compression' : 'org.apache.cassandra.io.compress.LZ4Compressor'}
  and index_interval = 128;

測試插入數據和查詢,以及和node2之間的數據同步。先看node1上的插入數據

XXXXX@XXXXX-asus:~/apache-cassandra-2.0.6/bin$ ./cassandra-cli -h 192.168.2.103 -p 9160
Connected to: "TC01" on 192.168.2.103/9160
Welcome to Cassandra CLI version 2.0.6

The CLI is deprecated and will be removed in Cassandra 3.0.  Consider migrating to cqlsh.
CQL is fully backwards compatible with Thrift data; see http://www.datastax.com/dev/blog/thrift-to-cql3

Type 'help;' or '?' for help.
Type 'quit;' or 'exit;' to quit.

[default@unknown] use test;
Authenticated to keyspace: test
[default@test] set testcf['num']['num1']=1111;
Value inserted.
Elapsed time: 66 msec(s).
[default@test] list testcf;
Using default limit of 100
Using default cell limit of 100
-------------------
RowKey: num
=> (name=num1, value=1111, timestamp=1397389549047000)

1 Row Returned.
Elapsed time: 823 msec(s).
[default@test] 

再看node2上的同步的結果

XXXXX@XXXXX-asus:~/apache-cassandra-2.0.6/bin$ ./cassandra-cli -h 192.168.2.102 -p 9160
Connected to: "TC01" on 192.168.2.102/9160
Welcome to Cassandra CLI version 2.0.6

The CLI is deprecated and will be removed in Cassandra 3.0.  Consider migrating to cqlsh.
CQL is fully backwards compatible with Thrift data; see http://www.datastax.com/dev/blog/thrift-to-cql3

Type 'help;' or '?' for help.
Type 'quit;' or 'exit;' to quit.

[default@unknown] use test;
Authenticated to keyspace: test
[default@test] list testcf;
Using default limit of 100
Using default cell limit of 100
-------------------
RowKey: num
=> (name=num1, value=1111, timestamp=1397389549047000)

1 Row Returned.
Elapsed time: 914 msec(s).
[default@test] 

能夠看到數據已經同步到node2,時間戳同樣。

相關文章
相關標籤/搜索