Debian部署Cassandra指南

On 2011年01月18日, in linux, by netoearth

Apache Cassandra

http://incubator.apache.org/cassandra/media/img/cassandra_logo.png

A highly scalable, eventually consistent, distributed, structured key-value store.

一个高度可扩展、最終一致、分布式和结构化key-value儲存方案。

下载

http://incubator.apache.org/cassandra/download/ 选择二进制包下载,包名类似 apache-cassandra-incubating-x.y.z-bin.tar.gz 。

设置和运行

为了方便,下面所有命令以root身份执行。

以一个独立节点运行

tar -zxvf cassandra-$VERSION.tar.gz
mv cassandra-$VERSION /opt/cassandra

sudo mkdir -p /var/log/cassandra
sudo mkdir -p /var/lib/cassandra

echo 'alias PATH=$PATH:/opt/cassandra/bin/' >> ~/.bashrc && source ~/.bashrc

创建start-cassandra.sh

/opt/cassandra/bin/cassand

创建stop-cassandra.sh

kill `ps aux |  fgrep  $USER | grep cassandra | grep -v 'grep' | awk '{print $2}'`

创建show-cassandra.sh

#/bin/bash
echo "cassandra PID: `ps aux |  fgrep  $USER | grep cassandra | grep -v 'grep' | awk '{print $2}'`"

以后台方式运行节点:

./start-cassandra.sh

或以前台方式运行:

cassandra -f

测试

cassandra-cli --host localhost --port 9160

如果成功会显示:

  Connected to localhost/9160
  Welcome to cassandra CLI.

  Type 'help' or '?' for help. Type 'quit' or 'exit' to quit.
  cassandra>

尝试写和读键:

set Keyspace1.Standard1['shugelee']['first'] = 'Lee'
set Keyspace1.Standard1['shugelee']['last'] = 'Li'
set Keyspace1.Standard1['shugelee']['age'] = '21'

get Keyspace1.Standard1['shugelee']

結果类似:

  (column=last, value=li; timestamp=1263050140529)
  (column=first, value=lee; timestamp=1263050117638)
  (column=age, value=18; timestamp=1263050155638)
Returned 3 rows.

以一个簇(集群)运行

假定两台debian均按上面安装并初步设置了cassandra环境:

  • A 192.168.1.101 [development server]
  • B 192.168.1.105 [developer Lee Li]

将A 192.168.1.101 [development server]作为主server运行。

development server的设置

    <Seed>127.0.0.1</Seed>

改为:

    <Seed>192.168.1.101</Seed>
    <ListenAddress>localhost</ListenAddress>

改为:

    <ListenAddress>192.168.1.105</ListenAddress>
    <ThriftAddress>localhost</ThriftAddress>

改为:

    <ThriftAddress>0.0.0.0</ThriftAddress>

developer Lee Li的设置

    <Seed>127.0.0.1</Seed>

改为:

    <Seed>192.168.1.101</Seed>
    <Seed>192.168.1.105</Seed>
    <ListenAddress>localhost</ListenAddress>

改为:

    <ListenAddress>192.168.1.101</ListenAddress>
    <ThriftAddress>localhost</ThriftAddress>

改为:

    <ThriftAddress>0.0.0.0</ThriftAddress>

分别运行A和B上的cassandra:

canssandra -f

分别在A和B上测试:

nodeprobe -host 192.168.1.101 ring

如果成功,結果类似:

DEBUG - Loading settings from /opt/cassandra/bin/../conf/storage-conf.xml
DEBUG - Syncing log with a period of 1000
Starting Token                                 Ending Token                                 Size Address        Ring
132617574668126261121070408499066554197        127319937893509951017249225297128612859         1 192.168.1.101  |<--|
127319937893509951017249225297128612859        132617574668126261121070408499066554197         1 192.168.1.105  |-->|

分别在A和B上测试链接到一个node:

cassandra-cli  --host 192.168.1.105 --port 9160

如果成功,結果类似:

# cassandra-cli  --host 192.168.1.105 --port 9160
Connected to 192.168.1.105/9160
Welcome to cassandra CLI.

Type 'help' or '?' for help. Type 'quit' or 'exit' to quit.
cassandra>

测试清单:

  • A连接到自己的cassandra
  • A连接到B的cassandra
  • B连接到自己的cassandra
  • B连接到A的cassandra
  • A连接到自己的cassandra,写入并读取键值
  • A连接到B的cassandra,写入并读取键值
  • B连接到自己的cassandra,写入并读取键值
  • B连接到A的cassandra,写入并读取键值

测试清单二:

  • B停止Cassandra服务,B连接到A并写入数据,重启B的Cassandra服务,B连接到自身的Casssandra,查看刚刚在A写入的数据(以-f方式运行观察,B重启时,立即自动与A同步!非常好!)

在各个平台下运行

Gentoo GNU/Linux + JRE6下运行成功: http://lee.youxu.info/blog/wp-content/uploads/2010/01/cassandra-on-gentoo.png

Windows XP SP3 + JRE6下运行成功: http://lee.youxu.info/blog/wp-content/uploads/2010/01/cassandra-on-windows.jpg

参考链接


TODO

  • Python + Thrift+ Cassandra写几个小玩具)
    • flex+python+thrift+cassandra的guestbook已完成,可惜公司老大不同意开源
    • Python/PyGTK + thrift + cassandra的字典已完成,稍后开源
  • K/V型和R型比较,各自的比较和总结
Tagged with:  

Comments are closed.