加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

开源Nosql数据库Cassandra3.0实战-集群部署与插件使用

发布时间:2020-12-13 13:36:18 所属栏目:百科 来源:网络整理
导读:简介 Cassandra是一套开源分布式NoSQL数据库系统,Cassandra的主要特点是无中心的设计,其分布式集群由一堆数据库节点共同构成一个分布式网络服务,对Cassandra 的一个写操作,会被复制到其他节点上去,对Cassandra的读操作,也会被路由到某个节点上面去读

简介

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

$wgethttp://download.oracle.com/otn-pub/java/jdk/8u112-b15/jdk-8u112-linux-x64.tar.gz
$tarxfjdk-8u112-linux-x64.tar.gz-C/opt
$vim/etc/profile
增加
exportJAVA_HOME=/opt/jdk1.8.0_112
exportPATH=$PATH:$JAVA_HOME/bin
exportCLASSPATH=$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

$wgethttp://apache.fayea.com/cassandra/3.0.9/apache-cassandra-3.0.9-bin.tar.gz
$tarxvfapache-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

$copyconf/cassandra.yamlconf/cassandra.yaml.bak
$vimconf/cassandra.yaml
#cassandra-3.0.9的精简配置,可以运行集群的最低配置。
cluster_name:'MyCluster'#集群名
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的默认配置,只需要修改如下行即可,其他性能参数请参照官方文档。

10cluster_name:'MyCluster'
71hints_directory:/data/cassandra/hints
169data_file_directories:170-/data/cassandra/dbdata
175commitlog_directory:/data/cassandra/commitlog
287saved_caches_directory:/data/cassandra/caches
343-seeds:"10.10.8.3,10.10.8.5"
473listen_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/nodetoolstatus
Datacenter:datacenter1
=======================
Status=Up/Down
|/State=Normal/Leaving/Joining/Moving
--AddressLoadTokensOwns(effective)HostIDRack
UN10.10.8.3304.71KB25668.0%64b6a935-caa6-4ed5-857b-70963e74a81drack1
UN10.10.8.4173.84KB25665.3%db77bd8a-2655-41c6-b13e-584cf44b8162rack1
UN10.10.8.5297.2KB25666.7%8fac64f8-1ed9-4ca3-af70-dee9ebcf77c2rack1

spacer.gif

2:当前节点状态

$/opt/cassandra/bin/nodetoolinfo
ID:db77bd8a-2655-41c6-b13e-584cf44b8162
Gossipactive:true
Thriftactive:true
NativeTransportactive:true
Load:173.84KB
GenerationNo:1478159246
Uptime(seconds):4554
HeapMemory(MB):297.65/7987.25
OffHeapMemory(MB):0.00
DataCenter:datacenter1
Rack:rack1
Exceptions:0
KeyCache:entries14,size1.08KB,capacity100MB,110hits,127requests,0.866recenthitrate,14400saveperiodinseconds
RowCache:entries0,size0bytes,capacity0bytes,0hits,0requests,NaNrecenthitrate,0saveperiodinseconds
CounterCache:entries0,capacity50MB,7200saveperiodinseconds
Token:(invokewith-T/--tokenstoseeall256tokens)


3:关闭cassandra的进程

$/opt/cassandra/bin/nodetoolstopdaemon
Cassandrahasshutdown.
error:Connectionrefused
--StackTrace--
java.net.ConnectException:Connectionrefused
atjava.net.PlainSocketImpl.socketConnect(NativeMethod)
atjava.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
atjava.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
atjava.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
atjava.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)


4:查看各个列的数据详细信息、读写次数,响应时间等

$/opt/cassandra/bin/nodetoolcfstats
Keyspace:system_traces
ReadCount:0
ReadLatency:NaNms.
WriteCount:0
WriteLatency:NaNms.
PendingFlushes:0
Table:events
SSTablecount:0
Spaceused(live):0
Spaceused(total):0
Spaceusedbysnapshots(total):0
Offheapmemoryused(total):0
SSTableCompressionRatio:0.0
Numberofkeys(estimate):0
Memtablecellcount:0
Memtabledatasize:0
Memtableoffheapmemoryused:0
Memtableswitchcount:0


(2)cqlsh 命令行工具

cqlsh是cassandra的客户端命令行工具,替代了之前版本中的cassandra-cli,能实现对数据的增删改查等一些列的操作。

$/opt/cassandra/bin/cqlsh
Usage:cqlsh[options][host[port]]
CQLShellforApacheCassandra


1:安装python2.7(依赖python)

$yuminstallopenssl-devel#防止python编译后没有ssl模块,导致cqlsh不可用
$wgethttps://www.python.org/ftp/python/2.7/Python-2.7.tgz
$tarxfPython-2.7.tgz
$cdPython-2.7
$mkdir/usr/local/python27
$./configure--prefix=/usr/local/python27
$make&&makeinstall
$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/cqlsh10.10.8.39042
ConnectedtoMyClusterat10.10.8.3:9042.
[cqlsh5.0.1|Cassandra3.0.9|CQLspec3.4.0|Nativeprotocolv4]
UseHELPforhelp.
cqlsh>showversion
[cqlsh5.0.1|Cassandra3.0.9|CQLspec3.4.0|Nativeprotocolv4]
cqlsh>showhost
ConnectedtoMyClusterat10.10.8.3:9042.


3:help命令可以看到 CQL数据操作语言的相关命令

cqlsh>help
Documentedshellcommands:
===========================
CAPTURECLSCOPYDESCRIBEEXPANDLOGINSERIALSOURCEUNICODE
CLEARCONSISTENCYDESCEXITHELPPAGINGSHOWTRACING
CQLhelptopics:
================
AGGREGATESCREATE_KEYSPACEDROP_TRIGGERTEXT
ALTER_KEYSPACECREATE_MATERIALIZED_VIEWDROP_TYPETIME
ALTER_MATERIALIZED_VIEWCREATE_ROLEDROP_USERTIMESTAMP
ALTER_TABLECREATE_TABLEFUNCTIONSTRUNCATE
ALTER_TYPECREATE_TRIGGERGRANTTYPES
ALTER_USERCREATE_TYPEINSERTUPDATE
APPLYCREATE_USERINSERT_JSONUSE
ASCIIDATEINTUUID
BATCHDELETEJSON
BEGINDROP_AGGREGATEKEYWORDS
BLOBDROP_COLUMNFAMILYLIST_PERMISSIONS
BOOLEANDROP_FUNCTIONLIST_ROLES
COUNTERDROP_INDEXLIST_USERS
CREATE_AGGREGATEDROP_KEYSPACEPERMISSIONS
CREATE_COLUMNFAMILYDROP_MATERIALIZED_VIEWREVOKE
CREATE_FUNCTIONDROP_ROLESELECT
CREATE_INDEXDROP_TABLESELECT_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/ ---原理

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读