Welcome

首页 / 软件开发 / C++ / HBase Thrift接口的C++客户端

HBase Thrift接口的C++客户端2015-05-03hbase-thrift项目是对HBase Thrift接口的封装,屏蔽底层的细节,使用户可以方便地通过HBase Thrift接口访问HBase集群,同时基于此对C++客户端读写的效率进行了简单的测试。该项目目前基于的是HBase thrift接口,至于HBase 0.94版本中的HBase thrift2接口,后续再考虑进一步的支持。

前提条件

1)下载,解压,安装 。

wget https://dist.apache.org/repos/dist/release/thrift/0.8.0/thrift-0.8.0.tar.gztar zxvf thrift-0.8.0.tar.gzsudo yum install automake libtool flex bison pkgconfig gcc-c++ boost-devel libevent-devel zlib-devel python-devel ruby-develcd thrift-0.8.0./configuremakesudo make install
2)下载,解压或之前的版本,用户根据Hbase.thrift文件生成客户端thrift接口代码实现。

wget http://www.fayea.com/apache-mirror/hbase/hbase-0.92.1/hbase-0.92.1.tar.gz

tar zxvf hbase-0.92.1.tar.gz

接口实现

目前已封装实现的接口如下:

class HbCli {public:// Constructor and DestructorHbCli(const char *server, const char *port);~HbCli();// Util Functionsbool connect();bool disconnect();bool reconnect();inline bool isconnect(); // HBase DDL Functionsbool createTable(const std::string table, const ColVec &columns);bool deleteTable(const std::string table);bool tableExists(const std::string table);// HBase DML Functions//bool putRow(const std::string table, const std::string row, const std::string column, const std::string value);bool putRowWithColumns(const std::string table, const std::string row, const StrMap columns);bool putRows(const std::string table, const RowMap rows);bool getRow(const std::string table, const std::string row, ResVec &rowResult);bool getRowWithColumns(const std::string table, const std::string row, const StrVec columns, ResVec &rowResult);bool getRows(const std::string table, const StrVec rows, ResVec &rowResult);bool getRowsWithColumns(const std::string table, const StrVec rows, const StrVec columns, ResVec &rowResult);bool delRow(const std::string table, const std::string row);bool delRowWithColumn(const std::string table, const std::string row, const std::string column);bool delRowWithColumns(const std::string table, const std::string row, const StrVec columns);bool scan(const std::string table, const std::string startRow, StrVec columns, ResVec &values);bool scanWithStop(const std::string table, const std::string startRow, const std::string stopRow, StrVec columns, ResVec &values);// HBase Util Functionsvoid printRow(const ResVec &rowResult);private:boost::shared_ptr<TTransport> socket;boost::shared_ptr<TTransport> transport;boost::shared_ptr<TProtocol> protocol;HbaseClient client;bool _is_connected;};
编译安装

1)运行thrift命令,生成C++模块的客户端代码:

thrift --gen cpp [hbase-root]/src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift

2)执行make生成二进制程序:

make demo

make perf

3)执行二进制程序:

./demo <host> <port>

./testput <host> <port> <key_len> <val_len> <list_num>

./testget <host> <port> <key_len> <val_len> <list_num> <block_cache_flag>

暂时更新这些内容,感兴趣的,详见: