HBase Thrift接口C++客户端

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

前提条件

1)下载,解压,安装 thrift-0.8.0

wget https://dist.apache.org/repos/dist/release/thrift/0.8.0/thrift-0.8.0.tar.gz
tar zxvf thrift-0.8.0.tar.gz
sudo yum install automake libtool flex bison pkgconfig gcc-c++ boost-devel libevent-devel zlib-devel python-devel ruby-devel
cd thrift-0.8.0
./configure
make
sudo make install

2)下载,解压 hbase-0.92.1 或之前的版本,用户根据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 Destructor
    HbCli(const char *server, const char *port);
    ~HbCli();

    // Util Functions
    bool connect();
    bool disconnect();
    bool reconnect();
    inline bool isconnect();
   
    // HBase DDL Functions
    bool 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 Functions
    void 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>

暂时更新这些内容,感兴趣的,详见:https://github.com/ypf412/hbase-thrift

posted on 2012-08-28 13:08  大圆那些事  阅读(11469)  评论(0编辑  收藏  举报

导航