Cassandra是去中心化的集羣架構,沒有傳統集羣的中心節點,各個節點地位都是平等的,經過Gossip協議維持集羣中的節點信息。爲了使集羣中的各節點在啓動時能發現其餘節點,須要指定種子節點(seeds),各節點都先和種子節點通訊,經過種子節點獲取其餘節點列表,而後和其餘節點通訊。種子節點能夠指定多個,經過在 conf/ cassandra.yaml中的seeds屬性配置。java
主機信息以下表所示:
全部節點已安裝了jdk 8。以下:node
[root@db03 ~]# java -version java version "1.8.0_212" Java(TM) SE Runtime Environment (build 1.8.0_212-b10) Java HotSpot(TM) 64-Bit Server VM (build 25.212-b10, mixed mode)
編輯sysctl.conf文件,加入如下內容:數據庫
vm.swappiness=10 net.core.rmem_default = 16777216 net.core.rmem_max = 16777216 net.core.wmem_default = 16777216 net.core.wmem_max = 16777216 net.core.optmem_max = 40960 net.ipv4.tcp_rmem = 4096 87380 16777216 net.ipv4.tcp_wmem = 4096 65536 16777216 vm.max_map_count=1048575
編輯/etc/security/limits.conf文件,加入如下內容: apache
soft nofile 32768 hard nofile 32768 hard nproc 32768 soft nproc 32768
完成後,建議重啓操做系統使其生效。架構
這裏使用的是二進制rpm包進行安裝。在各個節點建立yum倉庫,內容以下:app
[root@db03 ~]# vi /etc/yum.repos.d/cass.repo [cassandra] name=Apache Cassandra baseurl=https://www.apache.org/dist/cassandra/redhat/311x/ gpgcheck=1 repo_gpgcheck=1 gpgkey=https://www.apache.org/dist/cassandra/KEYS
而後經過yum命令在各個節點進行安裝:tcp
[root@db03 ~]# yum -y install cassandra
更改各個節點的配置文件內容以下:ide
[root@db03 ~]# vi /etc/cassandra/default.conf/cassandra.yaml cluster_name: 'TCS01' num_tokens: 256 seed_provider: - class_name: org.apache.cassandra.locator.SimpleSeedProvider parameters: - seeds: "192.168.120.83,192.168.120.85" listen_address:192.168.120.83 endpoint_snitch: GossipingPropertyFileSnitch start_rpc: true rpc_address: 192.168.120.83
編輯cassandra-rackdc.properties文件,設置dc參數,以下:ui
[root@db03 ~]# vi /etc/cassandra/default.conf/cassandra-rackdc.properties dc=dc1 rack=rack1
根據以前的規劃,db03和db04屬於dc1;db05和db06屬於dc2。url
先啓動種子節點,再啓動其餘支節點。
[root@db03 ~]# systemctl enable cassandra [root@db03 ~]# systemctl start cassandra [root@db05 ~]# systemctl enable cassandra [root@db05 ~]# systemctl start cassandra
[root@db04 ~]# systemctl enable cassandra [root@db04 ~]# systemctl start cassandra [root@db06 ~]# systemctl enable cassandra [root@db06 ~]# systemctl start cassandra
cassandra提供了nodetool命令,能夠查看集羣節點的狀態信息,以下:
[root@db03 ~]# nodetool status
鍵空間(Keyspace)是用於保存列族,用戶定義類型的對象。 鍵空間(Keyspace)就像RDBMS中的數據庫,其中包含列族,索引,用戶定義類型,數據中心意識,鍵空間(Keyspace)中使用的策略,複製因子等。
查看系統中默認存在的keyspace:
[root@db03 ~]# cqlsh 192.168.120.83 Connected to TCS01 at 192.168.120.83:9042. [cqlsh 5.0.1 | Cassandra 3.11.4 | CQL spec 3.4.4 | Native protocol v4] Use HELP for help. cqlsh> desc keyspaces; system_traces system_schema system_auth system system_distributed
建立keyspace:
cqlsh> CREATE KEYSPACE spacewalk WITH replication = {'class':'SimpleStrategy', 'replication_factor' : 4}; cqlsh> desc keyspaces; system_schema system_auth spacewalk system system_distributed system_traces cqlsh>
要刪除自定義的keyspace,使用下面的命令:
cqlsh> drop keyspace spacewalk;
在spacewalk鍵空間上建立表以及導入數據:
cqlsh:spacewalk> desc tables; rhnpackagecapability
cqlsh:spacewalk> copy rhnpackagecapability(id,name,version,created,modified) from '/tmp/d.csv' with delimiter=',' and header=false;
cqlsh:spacewalk> drop table rhnpackagecapability;
在導入數據過程當中,會遇到各類各樣的報錯,下面是我遇到的兩種問題:
<stdin>:1:Failed to import 5000 rows: Error - field larger than field limit (131072), given up after 1 attempts
建立cqlshrc文件:
[root@db03 ~]# cp /etc/cassandra/default.conf/cqlshrc.example ~/.cassandra/cqlshrc [root@db03 ~]# vi ~/.cassandra/cqlshrc [csv] --加大filed_size_limit的值,默認爲131072 field_size_limit = 13107200000
Failed to import 20 rows: InvalidRequest - Error from server: code=2200 [Invalid query] message="Batch too large", will retry later, attempt 1 of 5
編輯cassandra.yaml文件,加大batch_size_fail_threshold_in_kb參數值,好比5120。而後在copy後面加maxbatchsize=1 and minbatchsize=1,以下:
cqlsh> copy mykeysp01.rhnpackagerepodata(id,primary_xml,filelist,other,created,modified) from '/u02/tmp/rhnpackagerepodata.csv' with maxbatchsize=1 and minbatchsize=1;