Cassandra安装及其简单试用

官方主页:
http://cassandra.apache.org/

简介:
The Apache Cassandra Project develops a highly scalable second-generation distributed database, bringing together Dynamo's fully distributed design and Bigtable's ColumnFamily-based data model.
Cassandra was open sourced by Facebook in 2008, and is now developed by Apache committers and contributors from many companies.

Apache Cassandra是一套开源分布式NoSQL数据库系统。它最初由Facebook开发,用于储存收件箱等简单格式数据,集Google BigTable的数据模型与Amazon Dynamo的完全分布式的架构于一身。Facebook于2008将 Cassandra 开源,此后,由于Cassandra良好的可扩放性,被Digg、Twitter等知名Web 2.0网站所采纳,成为了一种流行的分布式结构化数据存储方案。
架构
Cassandra使用了Google BigTable的数据模型,与面向行的传统的关系型数据库不同,这是一种面向列的数据库,列被组织成为列族(Column Family),在数据库中增加一列非常方便。对于搜索和一般的结构化数据存储,这个结构足够丰富和有效。
Cassandra的系统架构与Dynamo一脉相承,是基于O(1)DHT(分布式哈希表)的完全P2P架构,与传统的基于Sharding的数据库集群相比,Cassandra可以几乎无缝地加入或删除节点,非常适于对于节点规模变化比较快的应用场景。
Cassandra的数据会写入多个节点,来保证数据的可靠性,在一致性、可用性和网络分区耐受能力(CAP)的折衷问题上,Cassandra比较灵活,用户在读取时可以指定要求所有副本一致(高一致性)、读到一个副本即可(高可用性)或是通过选举来确认多数副本一致即可(折衷)。这样,Cassandra可以适用于有节点、网络失效,以及多数据中心的场景。
特性
和其他数据库比较,Cassandra有三个突出特点:
模式灵活 :使用Cassandra,像文档存储,你不必提前解决记录中的字段。你可以在系统运行时随意的添加或移除字段。这是一个惊人的效率提升,特别是在大型部署上。
真正的可扩展性 :Cassandra是纯粹意义上的水平扩展。为给集群添加更多容量,可以指向另一台电脑。你不必重启任何进程,改变应用查询,或手动迁移任何数据。
多数据中心识别 :你可以调整你的节点布局来避免某一个数据中心起火,一个备用的数据中心将至少有每条记录的完全复制。
一些使Cassandra提高竞争力的其他功能:
范围查询 :如果你不喜欢全部的键值查询,则可以设置键的范围来查询。
列表数据结构 :在混合模式可以将超级列添加到5维。对于每个用户的索引,这是非常方便的。
分布式写操作 :有可以在任何地方任何时间集中读或写任何数据。并且不会有任何单点失败。
参考:
http://zh.wikipedia.org/zh/Cassandra


在CentOS 5.5上安装:
Cassandra requires the most stable version of Java 1.6 you can deploy. For Sun's jvm, this means at least u19; u21 is better.

解压安装包
tar zxvf apache-cassandra-0.7.0-bin.tar.gz 
创建cassandra默认需要的目录
mkdir -p /data/logs/cassandra
ln -s /data/logs/cassandra /var/log/cassandra
ln -s /data/logs/cassandra /var/lib/cassandra
ln -s /root/apache-cassandra-0.7.0 /usr/local/cassandra
cd /usr/local/cassandra

启动:
bin/cassandra -f

使用命令行连接本地服务:
bin/cassandra-cli --host localhost
在命令行模式执行:
(1)可理解为创建一个数据库
create keyspace Cassandra_Test;
use Cassandra_Test;
(2)可理解为创建一个表
create column family Users with comparator=UTF8Type and default_validation_class=UTF8Type;
(3)插入数据
set Users[author][nick_name] = 'preftest';
set Users[author][age] = long(31);
(4)查询数据
get Users[author];
count Users['author'];
list Users;


使用Python客户端
安装所需的库:
easy_install pycassa
easy_install thrift05
注意,安装thrift05时如果提示找不到python.h文件,则需要先装python-dev:
yum install python-devel

编写Python脚本连接Cassandra:
import pycassa
pool=pycassa.connect('Cassandra_Test',['localhost:9160'])
col_fam=pycassa.ColumnFamily(pool,'Users')
col_fam.insert('tester',{'nick_name':'performance','age':'31'})
print col_fam.get('author')
print col_fam.get_count('tester')

参考:
http://pycassa.github.com/pycassa/tutorial.html


做集群请参考:
Get Started:
http://wiki.apache.org/cassandra/GettingStarted

## 添加新节点后,两控制台都能智能感应
INFO 15:52:20,128 Node /10.20.223.111 is now part of the cluster
INFO 15:52:21,137 Started hinted handoff for endpoint /10.20.223.111
INFO 15:52:21,138 InetAddress /10.20.223.111 is now UP
INFO 15:52:21,139 Finished hinted handoff of 0 rows to endpoint /10.20.223.111
# 节点故障时的感应
INFO 15:59:37,046 error writing to /10.20.223.111
INFO 15:59:42,051 InetAddress /10.20.223.111 is now dead.
# 节点恢复时的感应
INFO 16:00:19,430 Node /10.20.223.111 has restarted, now UP again
INFO 16:00:19,430 Started hinted handoff for endpoint /10.20.223.111
INFO 16:00:19,431 Node /10.20.223.111 state jump to normal
INFO 16:00:19,431 Finished hinted handoff of 0 rows to endpoint /10.20.223.111

参考:
http://hi.baidu.com/higkoo/blog/item/e5e1cd34d278fba4d1a2d3d7.html

原文地址:https://www.cnblogs.com/preftest/p/1948841.html