1.安装cassandra
#下载bin包
wget http://apache.mirror.testserver.li//cassandra/0.6.5/apache-cassandra-0.6.5-bin.tar.gz
#解压
tar xzvf apache-cassandra-0.6.5-bin.tar.gz
#创建目录
sudo mkdir -p /var/log/cassandra
sudo chown -R `whoami` /var/log/cassandra
sudo mkdir -p /var/lib/cassandra
sudo chown -R `whoami` /var/lib/cassandra
#前台运行cassandra
bin/cassandra -f
#cli连接测试
bin/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>
2.安装thrift
#下载源码包
wget http://apache.mirror.testserver.li//incubator/thrift/0.4.0-incubating/thrift-0.4.0.tar.gz
#解压
tar xzvf thrift-0.4.0.tar.gz
#安装需要的包
sudo apt-get install libboost-dev automake libtool flex bison pkg-config g++
#安装python包(因为我要用python开发)
sudo apt-get install python-dev
#安装
./bootstrap.sh
./configure
make
sudo make install
#安装python库
cd lib/py
sudo python setup.py install
3.安装python库
#安装setuptools
wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz#md5=7df2a529a074f613b509fb44feefe74e
tar xzvf setuptools-0.6c11.tar.gz
cd setuptools-0.6c11/
sudo python setup.py install
#安装python-cassandra
wget http://pypi.python.org/packages/source/p/python-cassandra/python-cassandra-0.6.1.tar.gz#md5=cfffb96329dd2acc5a75d0f2126a38a6
tar xzvf python-cassandra-0.6.1.tar.gz
cd python-cassandra-0.6.1/
sudo python setup.py install
4.OK,现在可以用python来访问cassandra了
#编写测试程序CassanTest.py
#!/usr/bin/env python # encoding: utf-8
"""Sample Cassandra Client
Created by Chris Goffinet on 2009-08-26."""
from thrift import Thrift
from thrift.transport import TTransport
from thrift.transport import TSocket
from thrift.protocol.TBinaryProtocol import TBinaryProtocolAccelerated
from cassandra import Cassandra
from cassandra.ttypes import *
import time
import pprint
def main():
socket = TSocket.TSocket("localhost", 9160)
transport = TTransport.TBufferedTransport(socket)
protocol = TBinaryProtocol.TBinaryProtocolAccelerated(transport)
client = Cassandra.Client(protocol)
pp = pprint.PrettyPrinter(indent=2)
keyspace = "Keyspace1"
column_path = ColumnPath(column_family="Standard1", column="email")
key = "1"
value = "[email protected] "
timestamp = time.time()
try:
transport.open()
#Insert the data into Keyspace 1
client.insert(keyspace,
key,
column_path,
value,
timestamp,
ConsistencyLevel.ZERO)
#Query for data
column_parent = ColumnParent(column_family="Standard1")
slice_range = SliceRange(start="", finish="")
predicate = SlicePredicate(slice_range=slice_range)
result = client.get_slice(keyspace,
key,
column_parent,
predicate,
ConsistencyLevel.ONE)
pp.pprint(result)
except Thrift.TException, tx:
print 'Thrift: %s' % tx.message
finally:
transport.close()
if __name__ == '__main__':
main()
#运行它
python CassanTest.py
[ ColumnOrSuperColumn(column=Column(timestamp=1286382520, name='email', value='[email protected] '), super_column=None)]
参考:
http://wiki.woodpecker.org.cn/moin/ApacheCassandra
http://wiki.apache.org/thrift/GettingUbuntuPackages
http://wiki.apache.org/thrift/ThriftInstallation