前言
cassandra是一种NoSQL数据库,No是指No Relational。cassandra的数据模型结合了Dynamo的key/value和BigTable 的面向列的特点,主要被设计为存储大规模的分布式数据。
高可靠性:gossip座位通信协议,节点同等地位,无主从之分.(hbase是master/slaver,单点失效的可能) p2p 去中心化
高可扩展:级联可扩展,添加新节点操作简单
最终一致性:cap定律
高效写操作, 读取指定键值的记录较快, 但范围查询,与多个节点有关会慢, 读取全表也慢
cql语言,与sql语句相似度高
https://my.oschina.net/silentriver/blog/182678 Cassandra – 理解关键概念和数据模型
1.官网情况
http://cassandra.apache.org/
"Manage massive amounts of data, fast, without losing sleep"
目前版本 3.9 (pgp, md5 and sha1), released on 2016-09-29.
线性扩展(Linear scalability)
每个节点identical.
2.安装Cassandra
我的机器是windows10
下载 apache-cassandra-3.9-bin.tar.gz 后解压
添加系统变量 : CASSANDRA_HOME
然后添加path环境变量为%CASSANDRA_HOME%\bin
windows 就可以输入 cassandra 回车启动了
Linux
启动 bin/cassandra -f 关闭Control-C
或者 bin/cassandra 关闭 kill pid or pkill -f CassandraDaemon 查找进程 pgrep -f CassandraDaemon
查看状态
bin/nodetool status
3.配置集群
如果只是单点,则上面的配置就ok了,但如果要部署集群,则需要另外配置.
cassandra.yaml
最基本的参数
-
cluster_name
: the name of your cluster. -
seeds
: 以逗号分隔的集群种子IP地址. 当一个节点启动的时候,它会从配置文件中读取配置信息,这样它就知道它属于哪个集群,它需要跟哪个节点通信以获取其他节点信息(这个通信节点称为种子节点)。这些信息是必须在每个节点的cassandra.yaml里配置的。 -
storage_port
: 非必须改变,除非此端口被防火墙墙了.整个集群内部要相同 -
listen_address
: 本节点的IP, 因为要与其他节点通讯,所以这个参数设置正确非常重要. 或者,可以设置listen_interface告诉
Cassandra which interface to use. Set only one, not both. -
native_transport_port
: 客户端与Cassandra通讯的端口,保证不被墙.
更改目录位置
-
data_file_directories
: one or more directories where data files are located. -
commitlog_directory
: the directory where commitlog files are located. -
saved_caches_directory
: the directory where saved caches are located. -
hints_directory
: the directory where hints are located.
环境变量 cassandra-env.sh
- JVM_OPTS:Cassandra启动时候会把这个参数传递给JVM,所以额外的JVM命令行参数可以在这里设置
日志
logback.xml
3. CQLSH的使用
前期准备,需要python运行环境, python版本用2.7 不要用更高的版本
$ bin/cqlsh localhost
Connected to Test Cluster at localhost:9042.
[cqlsh 5.0.1 | Cassandra 3.8 | CQL spec 3.4.2 | Native protocol v4]
Use HELP for help.
cqlsh> SELECT cluster_name, listen_address FROM system.local; cluster_name | listen_address
--------------+----------------
Test Cluster | 127.0.0.1 (1 rows)
cqlsh>
步骤一,create a keyspace
CREATE KEYSPACE devJavaSource WITH REPLICATION={'class': 'SimpleStrategy','replication_factor': };
tip:可能运行失败,报错 OperationTimedOut: errors={'127.0.0.1': 'Client request timeout. See Session.execute[_async](timeout)'}, last_host=127.0.0.1
修改 cqlsh.py 文件的参数DEFAULT_REQUEST_TIMEOUT_SECONDS
为更大的值可以解决(修改后重启cassandra), 然而我也不知道具体原因.
步骤二, use created keyspace
USE devJavaSource;
步骤三, 新建表 插入数据 查询
CREATE TABLE USERS (ID int PRIMARY KEY,NAME text,ADDRESS text);
INSERT INTO USERS (ID, NAME, ADDRESS) VALUES (, ‘john’, ‘Oakland’); INSERT INTO USERS (ID, NAME, ADDRESS) VALUES (, ‘smith’, ‘California’); INSERT INTO USERS (ID, NAME, ADDRESS) VALUES (, ‘Joe’, ‘Nederland’);
SELECT * FROM USERS;
步骤四,建立索引
CREATE CUSTOM INDEX index_name ON keyspace_name.table_name ( column_name )
(USING class_name) (WITH OPTIONS = map)
cqlsh:devjavasource> create index on users(name);
4.节点之间的交互 gossip
节点独立,对等
最终一致性原理
gossip协议来发现集群种其他节点的位置和状态信息
peer-to-peer,定期交换状态信息
节点启动 ------------------>从cassandra.yaml得到集群名称,以及种子节点列表(因此每个节点的种子节点列表必须相同)
选派谁做种子节点没什么特别的意义,仅仅在于新节点加入到集群中时走gossip流程时有用,所以它们没什么特权
Gossiper(进程)通过每个节点的心跳来感知节点是否存活, 可以根据网络状况设置灵敏度参数phi_convict_threshold