前言:本教程以Cassandra 0.6.12为例,简述单机配置及C++客户端的使用。
1、下载
目前支持c++接口的最新稳定版是0.6.12
wget http://apache.etoak.com//cassandra/0.6.12/apache-cassandra-0.6.12-bin.tar.gz
2、单机配置
cd apache-cassandra-0.6.12-bin cd conf #编辑配置文件,它包含了单机的基本配置 vim storage-conf.yaml #确保data_file_directories, commitlog_directory 和 saved_caches_directory目录存在 mkdir -p /var/lib/cassandra/data mkdir -p /var/lib/cassandra/commitlog mkdir -p /var/lib/cassandra/saved_caches chown -R liheyuan:liheyuan /var/lib/cassandra #StoragePort更改为空 #检查log配置 vim log4j-server.properties #确保log文件存在 mkdir /var/log/cassandra chown -R liheyuan:liheyuan /var/log/cassandra
3、单机运行
cd .. ./bin/cassandra -f
一般如果上一步当中,4个目录的权限没有错误的话,就运行成功了。下面,我们使用cli学习Cassandra的data model操作方法
#启动clis ./bin/cassandra-cli localhost 9160 #显示当前的keyspace(单机暂时不能创建新的keyspace?) cassandra> show keyspaces Keyspace1 system #继续查看keyspaces Keyspace1的详情 cassandra> describe keyspace Keyspace1 Keyspace1.Super1 Column Family Type: Super Columns Sorted By: org.apache.cassandra.db.marshal.BytesType@d67067 Column Family Type: Super Column Sorted By: org.apache.cassandra.db.marshal.BytesType flush period: null minutes ------ Keyspace1.Standard2 Column Family Type: Standard Columns Sorted By: org.apache.cassandra.db.marshal.UTF8Type@6db724 Column Family Type: Standard Column Sorted By: org.apache.cassandra.db.marshal.UTF8Type flush period: null minutes ------ Keyspace1.Super2 Column Family Type: Super Columns Sorted By: org.apache.cassandra.db.marshal.UTF8Type@112da40 Column Family Type: Super Column Sorted By: org.apache.cassandra.db.marshal.UTF8Type flush period: null minutes ------ Keyspace1.Standard1 Column Family Type: Standard Columns Sorted By: org.apache.cassandra.db.marshal.BytesType@b6d6ab Column Family Type: Standard Column Sorted By: org.apache.cassandra.db.marshal.BytesType flush period: null minutes ------ Keyspace1.StandardByUUID1 Column Family Type: Standard Columns Sorted By: org.apache.cassandra.db.marshal.TimeUUIDType@1c7865b Column Family Type: Standard Column Sorted By: org.apache.cassandra.db.marshal.TimeUUIDType flush period: null minutes ------ #创建记录,在Keyspace1下的Standard2下创建1条key为1的记录,包含3列:title、url和content cassandra> set Keyspace1.Standard2['1']['tiele'] = 'Test Title' Value inserted. cassandra> set Keyspace1.Standard2['1']['url'] = 'http://iamurl.com' Value inserted. cassandra> set Keyspace1.Standard2['1']['content'] = 'I am content' Value inserted. #读取记录 cassandra> get Keyspace1.Standard2['1'] => (column=url, value=http://iamurl.com, timestamp=1301643434657000) => (column=tiele, value=Test Title, timestamp=1301643410873000) => (column=content, value=I am content, timestamp=1301643462022000) Returned 3 results.
4、使用C++接口(LibCassandra)
目前,C++接口的稳定版只有0.6.x的,0.7的分支还在测试中。
首先编译:
#下载 wget http://cloud.github.com/downloads/posulliv/libcassandra/libcassandra-0.6.5.tar.gz tar -xzvf libcassandra-0.6.5.tar.gz cd libcassandra #编译 ./config/autorun.sh ./configure make sudo make install
安装好后,我们写个例子,在examples/目录下有例子。
需要特别说明的是,编译参数相当费劲,这个库是用c++最新标准gnu++0x写的(GNU的c++0x标准),但是所有地方都没有说明!自己琢磨出来的编译参数,我的版本是g++ 4.4,如果你得不是,可能要适当调整,主要是试试其他的-std=?这个标准,自己man吧。
这也就是我讨厌c/c++的原因,特别是开源软件,移植性非常差。
#正确的编译参数 g++ -I/usr/local/include/thrift -std=gnu++0x -lcassandra -lthrift xx.cc
/*
* main.cpp
*
* Created on: 2011-4-5
* Author: liheyuan
*/
#include <libcassandra/cassandra.h>
#include <libcassandra/cassandra_factory.h>
#include <libcassandra/keyspace.h>
#include <iostream>
#include <cstdlib>
#include <stdint.h>
using namespace libcassandra;
using namespace std;
using namespace std;
#define HOST "127.0.0.1"
#define PORT 9160
int main()
{
CassandraFactory factory(HOST, PORT);
tr1::shared_ptr<Cassandra> client(factory.create());
//Get cluster info
cout << "Cluster name: " << client->getClusterName() << endl;
//Show All keyspace
set<string> kss = client->getKeyspaces();
for (set<string>::iterator itr = kss.begin(); itr != kss.end(); itr++)
{
cout << "Keyspace: " << *itr << endl;
}
try
{
//Insert/Get with keyspace
tr1::shared_ptr<Keyspace> ks(client->getKeyspace("Keyspace1"));
//insert
ks->insertColumn("1104", "Standard2", "title", "I'm title");
//get
cout << "retrieved values:" << ks->getColumnValue("1104", "Standard2",
"title") << endl;
} catch (org::apache::cassandra::InvalidRequestException &ire)
{
cout << ire.why << endl;
return -1;
}
return 0;
}
5、关于Keyspace和ColumnFamily
这个也是在storage-conf.xml中定义的,默认如下:
<Keyspaces>
<Keyspace Name="Keyspace1">
<ColumnFamily Name="Standard1" CompareWith="BytesType"
KeysCached="1000"
RowsCached="100"
RowCacheSavePeriodInSeconds="0"
KeyCacheSavePeriodInSeconds="3600"/>
<ColumnFamily Name="Standard2"
CompareWith="UTF8Type"
KeysCached="100%"/>
<ColumnFamily Name="StandardByUUID1" CompareWith="TimeUUIDType" />
<ColumnFamily Name="Super1"
ColumnType="Super"
CompareWith="BytesType"
CompareSubcolumnsWith="BytesType" />
<ColumnFamily Name="Super2"
ColumnType="Super"
CompareWith="UTF8Type"
CompareSubcolumnsWith="UTF8Type"
RowsCached="10000"
KeysCached="50%"
Comment="A column family with supercolumns, whose column and subcolumn names are UTF8 strings"/>
<ReplicaPlacementStrategy>org.apache.cassandra.locator.RackUnawareStrategy</ReplicaPlacementStrategy>
<ReplicationFactor>1</ReplicationFactor>
<EndPointSnitch>org.apache.cassandra.locator.EndPointSnitch</EndPointSnitch>
</Keyspace>
</Keyspaces>
如果初步测试的话,用Keyspace1、Standard2即可。
我在第4步执行到./config/autorun.sh
./configure
时 出现了 如下问题:
configure:error :thrift required for libcassandra
然后configure退出,再make 提示没有makefile
你一定要C++访问?建议用0.7.8以上版本吧。。有Thrift接口,安装Thrift就可以直接用了。。
不用C++访问只安装Cassandra就行了,libcassandra是C++访问用的。