

【赵强老师】NoSQL数据库之Cassandra基础
source link: http://www.cnblogs.com/collen7788/p/14376521.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

一、Cassandra简介
Cassandra是一个混合型的非关系的数据库,类似于Google的BigTable。其主要功能比Dynamo (分布式的Key-Value存储系统)更丰富,但支持度却不如文档存储MongoDB(介于关系数据库和非关系数据库之间的开源产品,是非关系数据库当中功能最丰富,最像关系数据库的。支持的数据结构非常松散,是类似json的bjson格式,因此可以存储比较复杂的数据类型)。Cassandra最初由Facebook开发,后转变成了开源项目。它是一个网络社交云计算方面理想的数据库。以Amazon专有的完全分布式的Dynamo为基础,结合了Google BigTable基于列族(Column Family)的数据模型。P2P去中心化的存储。很多方面都可以称之为Dynamo 2.0。
二、安装与配置
- 解压安装包
tar -zxvf apache-cassandra-3.11.3-bin.tar.gz -C ~/training/
- 设置环境变量
CASSANDRA_HOME=/root/training/apache-cassandra-3.11.3 export CASSANDRA_HOME PATH=$CASSANDRA_HOME/bin:$PATH export PATH
- 将以下三个地址设置为Linux相应的IP
listen_address: 192.168.56.111 rpc_address: 192.168.56.111 - seeds: "192.168.56.111"
- 启动Cassandra
注意:要以root用户启动Cassandra,需要使用-R参数。 命令:cassandra -R
- 验证Cassandra运行环境:nodetool工具
命令:nodetool status
从Cassandra 2.1版本开始,日志和数据都被存放在logs和data的子目录下。老版本默认保存在/var/log/cassandra和 /var/lib/cassandra。
三、Cassandra的配置参数
核心配置文件:conf/cassandra.yaml,启动过程中的日志信息如下图所示:

- 主要的运行时参数
cluster_name: 集群的名称 storage_port:节点内部通信的端口(默认: 7000) listen_address:Cassandra绑定的、用来连接其他Cassandra节点的IP地址或者主机名称。(默认: localhost) native_transport_port:客户端监听CQL本地传输的端口(默认: 9042)
- 目录相关的参数
data_file_directories:这个目录位置就是表数据存储的地方(在SSTables里)。Cassandra将数据均匀的分布在这个位置,受配置的压缩策略粒度的限制。 commitlog_directory:这个目录是commit log 存放的地方。为了获得最佳的写入性能,将commit log放在单独的磁盘分区,或者(理想情况下)和data文件目录分开的物理设备上。commit log只能追加的。 saved_caches_directory:这个目录是table key和row缓存存放的地方。默认位置:$CASSANDRA_HOME/data/saved_caches hints_directory:设置hints的存储位置(默认: $CASSANDRA_HOME/data/hints)
四、Cassandra的基本操作
(一)登录CQL客户端:cqlsh localhost
- 查看表system.local的结构:
- 查询系统的信息:
-
查看表空间:describe keyspaces;
-
查看已有表:describe tables;
-
查看表结构:describe table table_name;
(二)使用Cassandra的Java客户端
Cassandra使用cql语言作为操作语言,Cassandra在2.0之后,在操作上越来越像sql数据库的操作,这样想从传统关系型数据库,切换到Cassandra的话,上手成本也越来越低。使用官方java驱动操作Cassandra非常简单。maven引入相关的依赖如下所示:
<dependency> <groupId>info.archinnov</groupId> <artifactId>achilles-core</artifactId> <version>6.0.0</version> <classifier>shaded</classifier> </dependency>
下面执行CRUD操作:
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK