前言
java
cassandra是一种NoSQL数据库,No是指No Relational。cassandra的数据模型结合了Dynamo的key/value和BigTable 的面向列的特色,主要被设计为存储大规模的分布式数据。node
高可靠性:gossip座位通讯协议,节点同等地位,无主从之分.(hbase是master/slaver,单点失效的可能) p2p 去中心化python
高可扩展:级联可扩展,添加新节点操做简单sql
最终一致性:cap定律数据库
高效写操做, 读取指定键值的记录较快, 但范围查询,与多个节点有关会慢, 读取全表也慢apache
cql语言,与sql语句类似度高windows
https://my.oschina.net/silentriver/blog/182678 Cassandra – 理解关键概念和数据模型网络
1.官网状况async
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
日志
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':1 };
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 (11101, ‘john’, ‘Oakland’); INSERT INTO USERS (ID, NAME, ADDRESS) VALUES (11102, ‘smith’, ‘California’); INSERT INTO USERS (ID, NAME, ADDRESS) VALUES (11103, ‘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