開源Nosql數據庫Cassandra3.0實戰-集羣部署與插件使用

簡介

   Cassandra是一套開源分佈式NoSQL數據庫系統,Cassandra的主要特點是無中心的設計,其分佈式集羣由一堆數據庫節點共同構成一個分佈式網絡服務,對Cassandra 的一個寫操作,會被複制到其他節點上去,對Cassandra的讀操作,也會被路由到某個節點上面去讀取。對於一個Cassandra羣集來說,擴展性能是比較簡單的事情,只管在羣集裏面添加節點就可以了。

   隨着Nosql的火熱,Hbase、Mongodb已然成了NoSQL數據庫的代表,而Cassandra在國內的使用卻不多(據說360公司在大規模使用),根據百度指數的顯示cassandra的火熱度遠遠低於mongodb和Hbase。

wKioL1ga_sWjGbd1AAFcCfrGdvA835.png

    而在國外,根據數據庫評分網站DB-Engines的16.10的最新數據,cassandra排名上升到了第7,排名遠遠高於Hbase。

wKiom1ga_sbD9EH1AAJCM09U_lA528.png


  • 優點:

1、模式靈活

使用Cassandra,像文檔存儲,你不必提前解決記錄中的字段。你可以在系統運行時隨意的添加或移除字段。這是一個驚人的效率提升,特別是在大型部署上。

2、真正的可擴展性

Cassandra是純粹意義上的水平擴展。爲給集羣添加更多容量,可以指向另一臺電腦。你不必重啓任何進程,改變應用查詢,或手動遷移任何數據。

3、多數據中心識別

你可以調整你的節點佈局來避免某一個數據中心起火,一個備用的數據中心將至少有每條記錄的完全複製。

4、範圍查詢

如果你不喜歡全部的鍵值查詢,則可以設置鍵的範圍來查詢。

5、列表數據結構

在混合模式可以將超級列添加到5維。對於每個用戶的索引,這是非常方便的。

6、分佈式寫操作

有可以在任何地方任何時間集中讀或寫任何數據。並且不會有任何單點失敗


  • 缺點

1. 讀的性能太慢

無中心的設計,造成讀數據時通過逆熵做計算,性能損耗很大,甚至會嚴重影響服務器運作。

2. 數據同步太慢(最終一致性延遲可能非常大)

由於無中心設計,要靠各節點傳遞信息。相互發通知告知狀態,如果副本集有多份,其中又出現節點有宕機的情況,那麼做到數據的一致性,延遲可能非常大,效率也很低的。

3. 用插入和更新代替查詢,缺乏靈活性,所有查詢都要求提前定義好。

與大多數數據庫爲讀優化不同,Cassandra的寫性能理論上是高於讀性能的,因此非常適合流式的數據存儲,尤其是寫負載高於讀負載的。與HBase比起來,它的隨機訪問性能要高很多,但不是很擅長區間掃描,因此可以作爲HBase的即時查詢緩存,由HBase進行批量的大數據處理,由Cassandra提供隨機查詢的接口

4. 不支持直接接入hadoop,不能實現MapReduce。

現在大數據的代名詞就是hadoop,做爲海量數據的框架不支持hadoop及MapReduce,就將被取代。除非Cassandra能夠給出其他的定位,或者海量數據解決方案。DataStax公司,正在用Cassandra重構HDFS的文件系統,不知道是否可以成功。



一:部署cassandra

規劃:

集羣節點:3

10.10.8.3 

10.10.8.4

10.10.8.5


(1)配置jdk

  • 10.10.8.3、10.10.8.4、10.10.8.5

$ wget http://download.oracle.com/otn-pub/java/jdk/8u112-b15/jdk-8u112-linux-x64.tar.gz
$ tar xf jdk-8u112-linux-x64.tar.gz -C /opt
$ vim /etc/profile
增加
export JAVA_HOME=/opt/jdk1.8.0_112
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
$ source /etc/profile

(2)安裝Cassandra

  • 10.10.8.3、10.10.8.4、10.10.8.5

$ wget http://apache.fayea.com/cassandra/3.0.9/apache-cassandra-3.0.9-bin.tar.gz
$ tar xvf apache-cassandra-3.0.9-bin.tar.gz -C /opt
$ ln -s  /opt/apache-cassandra-3.0.9 /opt/cassandra


(3)配置

  • 10.10.8.3、10.10.8.4、10.10.8.5

$ copy conf/cassandra.yaml conf/cassandra.yaml.bak
$ vim conf/cassandra.yaml
#cassandra-3.0.9的精簡配置,可以運行集羣的最低配置。
cluster_name: 'My Cluster'            #集羣名
num_tokens: 256
seed_provider:
    - class_name: org.apache.cassandra.locator.SimpleSeedProvider
      parameters:
          - seeds: "10.10.8.3,10.10.8.4,10.10.8.5"   #節點ip列表
listen_address: 10.10.8.5                 #進程監聽地址
storage_port: 7000                        #集羣中節點通信的端口號
start_native_transport: true              #開啓native協議
native_transport_port: 9042               #客戶端的交互端口

data_file_directories:
    - /data/cassandra/dbdata              # 數據位置,多盤的話可以寫多個目錄
commitlog_directory:
    - /data/cassandra/commitlog           #commitlog的路徑,與data目錄分開磁盤,提高性能
saved_caches_directory:
    - /data/cassandra/caches              #緩存數據目錄
hints_directory:
    - /data/cassandra/hints
commitlog_sync: batch                     #批量記錄commitlog,每隔一段時間將數據commitlog
commitlog_sync_batch_window_in_ms: 2      #batch模式下,批量操作緩存的時間間隔
#commitlog_sync: periodic                 #週期記錄commitlog,每一次有數據更新都commitlog
#commitlog_sync_period_in_ms: 10000       #periodic模式,刷新commitlog的時間間隔

partitioner: org.apache.cassandra.dht.Murmur3Partitioner
endpoint_snitch: SimpleSnitch

 


如果使用cassandra的默認配置,只需要修改如下行即可,其他性能參數請參照官方文檔。

10 cluster_name: 'My Cluster'
71 hints_directory: /data/cassandra/hints
169 data_file_directories:170      - /data/cassandra/dbdata
175 commitlog_directory: /data/cassandra/commitlog
287 saved_caches_directory: /data/cassandra/caches
343           - seeds: "10.10.8.3,10.10.8.4,10.10.8.5"
473 listen_address: localhost


(4)創建對應的目錄

  • 10.10.8.3、10.10.8.4、10.10.8.5

$ mkdir  -p /data/cassandra/{dbdata,commitlog,caches,hints}


(5)啓動進程

  • 10.10.8.3、10.10.8.4、10.10.8.5

$ /opt/cassandra/bin/cassandra


二:插件工具使用

(1)nodetool工具

nodetool是cassandra的集羣和節點的管理和信息查看工具。

$ /opt/cassandra/bin/nodetool
usage: nodetool [(-u <username> | --username <username>)]
        [(-pw <password> | --password <password>)]
        [(-pwf <passwordFilePath> | --password-file <passwordFilePath>)]
        [(-h <host> | --host <host>)] [(-p <port> | --port <port>)] <command> [<args>]


1:查看集羣狀態

$/opt/cassandra/bin/nodetool status
Datacenter: datacenter1
=======================
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
--  Address    Load       Tokens       Owns (effective)  Host ID                               Rack
UN  10.10.8.3  304.71 KB  256          68.0%             64b6a935-caa6-4ed5-857b-70963e74a81d  rack1
UN  10.10.8.4  173.84 KB  256          65.3%             db77bd8a-2655-41c6-b13e-584cf44b8162  rack1
UN  10.10.8.5  297.2 KB   256          66.7%             8fac64f8-1ed9-4ca3-af70-dee9ebcf77c2  rack1

spacer.gif

2:當前節點狀態

$/opt/cassandra/bin/nodetool info
ID                     : db77bd8a-2655-41c6-b13e-584cf44b8162
Gossip active          : true
Thrift active          : true
Native Transport active: true
Load                   : 173.84 KB
Generation No          : 1478159246
Uptime (seconds)       : 4554
Heap Memory (MB)       : 297.65 / 7987.25
Off Heap Memory (MB)   : 0.00
Data Center            : datacenter1
Rack                   : rack1
Exceptions             : 0
Key Cache              : entries 14, size 1.08 KB, capacity 100 MB, 110 hits, 127 requests, 0.866 recent hit rate, 14400 save period in seconds
Row Cache              : entries 0, size 0 bytes, capacity 0 bytes, 0 hits, 0 requests, NaN recent hit rate, 0 save period in seconds
Counter Cache          : entries 0, size 0 bytes, capacity 50 MB, 0 hits, 0 requests, NaN recent hit rate, 7200 save period in seconds
Token                  : (invoke with -T/--tokens to see all 256 tokens)


3:關閉cassandra的進程

$ /opt/cassandra/bin/nodetool stopdaemon
Cassandra has shutdown.
error: Connection refused
-- StackTrace --
java.net.ConnectException: Connection refused
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)


4:查看各個列的數據詳細信息、讀寫次數,響應時間等

$ /opt/cassandra/bin/nodetool cfstats
Keyspace: system_traces
        Read Count: 0
        Read Latency: NaN ms.
        Write Count: 0
        Write Latency: NaN ms.
        Pending Flushes: 0
                Table: events
                SSTable count: 0
                Space used (live): 0
                Space used (total): 0
                Space used by snapshots (total): 0
                Off heap memory used (total): 0
                SSTable Compression Ratio: 0.0
                Number of keys (estimate): 0
                Memtable cell count: 0
                Memtable data size: 0
                Memtable off heap memory used: 0
                Memtable switch count: 0


(2)cqlsh 命令行工具

cqlsh是cassandra的客戶端命令行工具,替代了之前版本中的cassandra-cli,能實現對數據的增刪改查等一些列的操作。

$ /opt/cassandra/bin/cqlsh
Usage: cqlsh [options] [host [port]]
CQL Shell for Apache Cassandra


1:安裝python2.7(依賴python)

$ yum install openssl-devel              #防止python編譯後沒有ssl模塊,導致cqlsh不可用
$ wget https://www.python.org/ftp/python/2.7/Python-2.7.tgz
$ tar xf Python-2.7.tgz
$ cd Python-2.7
$ mkdir /usr/local/python27
$ ./configure --prefix=/usr/local/python27
$ make&&make install
$ ln -s /usr/local/python27/bin/python2.7 /usr/bin/python2.7

如果遇到 ImportError: No module named _ssl ,就安裝openssl-devel,然後再編譯安裝python

spacer.gif

2:連接host

$ /opt/cassandra/bin/cqlsh 10.10.8.3 9042
Connected to My Cluster at 10.10.8.3:9042.
[cqlsh 5.0.1 | Cassandra 3.0.9 | CQL spec 3.4.0 | Native protocol v4]
Use HELP for help.
cqlsh> show version
[cqlsh 5.0.1 | Cassandra 3.0.9 | CQL spec 3.4.0 | Native protocol v4]
cqlsh> show host
Connected to My Cluster at 10.10.8.3:9042.


3:help命令可以看到 CQL數據操作語言的相關命令

cqlsh> help
Documented shell commands:
===========================
CAPTURE  CLS          COPY  DESCRIBE  EXPAND  LOGIN   SERIAL  SOURCE   UNICODE
CLEAR    CONSISTENCY  DESC  EXIT      HELP    PAGING  SHOW    TRACING
CQL help topics:
================
AGGREGATES               CREATE_KEYSPACE           DROP_TRIGGER      TEXT    
ALTER_KEYSPACE           CREATE_MATERIALIZED_VIEW  DROP_TYPE         TIME    
ALTER_MATERIALIZED_VIEW  CREATE_ROLE               DROP_USER         TIMESTAMP
ALTER_TABLE              CREATE_TABLE              FUNCTIONS         TRUNCATE
ALTER_TYPE               CREATE_TRIGGER            GRANT             TYPES   
ALTER_USER               CREATE_TYPE               INSERT            UPDATE  
APPLY                    CREATE_USER               INSERT_JSON       USE     
ASCII                    DATE                      INT               UUID    
BATCH                    DELETE                    JSON           
BEGIN                    DROP_AGGREGATE            KEYWORDS       
BLOB                     DROP_COLUMNFAMILY         LIST_PERMISSIONS
BOOLEAN                  DROP_FUNCTION             LIST_ROLES     
COUNTER                  DROP_INDEX                LIST_USERS     
CREATE_AGGREGATE         DROP_KEYSPACE             PERMISSIONS    
CREATE_COLUMNFAMILY      DROP_MATERIALIZED_VIEW    REVOKE         
CREATE_FUNCTION          DROP_ROLE                 SELECT         
CREATE_INDEX             DROP_TABLE                SELECT_JSON    
cqlsh>




參考資料

連接:http://cassandra.apache.org/doc/latest/                          ---官方文檔

鏈接:http://jingyan.baidu.com/article/7e440953ec8a7e2fc0e2ef9b.html   ---優點

鏈接:https://www.zhihu.com/question/19592244/answer/21430967          ---缺點

連接:http://yikebocai.com/2014/06/cassandra-principle/                ---原理


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章