Centos7 C++连接hbase环境搭建及代码实现

1. 环境准备

下载内容 命令
开发工具 sudo yum -y groupinstall "Development Tools"
依赖库 sudo yum -y install automake libtool flex bison pkgconfig gcc-c++ boost-devel libevent-devel python-devel ruby-devel zlib-devel openssl-devel
autoconf wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.69.tar.gz
bison wget http://ftp.gnu.org/gnu/bison/bison-2.5.1.tar.gz
boost wget https://dl.bintray.com/boostorg/release/1.64.0/source/boost_1_64_0.tar.gz
libevent wget https://github.com/libevent/libevent/releases/download/release-2.1.8-stable/libevent-2.1.8-stable.tar.gz
thrift wget https://dist.apache.org/repos/dist/release/thrift/0.10.0/thrift-0.10.0.tar.gz
hbase wget http://archive.apache.org/dist/hbase/1.3.0/hbase-1.3.0-src.tar.gz

2. 编译安装

安装autoconf

  1. tar xvf autoconf-2.69.tar.gz
  2. cd ./autoconf-2.69/
  3. ./configure --prefix=/usr
  4. make
  5. make install

安装bison

  1. tar xvf bison-2.5.1.tar.gz
  2. cd ./bison-2.5.1/
  3. ./configure --prefix=/usr
  4. make
  5. make install

安装libevent

  1. tar xzvf libevent-2.1.8-stable.tar.gz
  2. cd ./libevent-2.1.8-stable/
  3. ./configure --prefix=/usr/local/libevent
  4. make && make install

安装boost

1.tar xvf boost_1_64_0.tar.gz
2.cd ./boost_1_64_0/
3../bootstrap.sh --prefix=/usr/local/boost
4../b2
5../b2 install

安装thrift

  1. cp /usr/local/boost/lib/libboost_unit_test_framework.a /usr/local/lib64/libboost_unit_test_framework.a
  2. cp /usr/local/boost/lib/libboost_unit_test_framework.a /usr/lib64/libboost_unit_test_framework.a
  3. tar xvf thrift-0.10.0.tar.gz
  4. cd ./thrift-0.10.0/
  5. ./configure --libdir=/usr/lib --with-boost-libdir=/usr/local/boost/lib/
  6. make && make install

生成接口文件(thrift2)

关于hbase对thrift与thrift2的接口比较可以查看这篇博客
1. tar xvf hbase-1.3.0-src.tar.gz
2. cd ./hbase-1.3.0
3. thrift --gen cpp ./hbase-thrift/src/main/resources/org/apache/hadoop/hbase/thrift2/Hbase.thrift
4. 将生成的gen-cpp目录拷贝到工程文件夹下

thrift2连接hbase代码实现

注:各个类的使用可以着重查看gen-cpp/hbase2_types.h、gen-cpp/THBaseService.h

导入文件与命名空间

#include 
#include 
#include 
#include "THBaseService.h"
#include 
#include 
#include 
using namespace std;
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace apache::hadoop::hbase::thrift2;
using boost::shared_ptr;

建立连接

    boost::shared_ptr socket(new TSocket(hbase_host,hbase_port));
        boost::shared_ptr transport(new TBufferedTransport(socket));
        boost::shared_ptr protocol(new TBinaryProtocol(transport));
        transport->open();
        THBaseServiceClient client(protocol);

查询一行

TGet get;
TResult tresult;
string rowkey="";
string tableName="";
get.__set_row(rowkey);
bool be = client.exists(tableName,get);
cout< vecColum=tresult.columnValues;
cout<<"rowkey:"<::iterator iter=vecColum.begin();iter!=vecColum.end();iter++){
    cout<<(*iter).qualifier<<":"<<(*iter).value<

插入一行记录

TPut put;
string rowkey="rowkey";
string tableName="table";
vector cvs;
TColumnValue colum;
colum.__set_family("wa");
colum.__set_qualifier("myKey");
colum.__set_value("myValue");
cvs.push_back(colum);
put.__set_row(rowkey);
put.__set_columnValues(cvs);
client.put(tableName,put);

扫描表

int scanId;
int num=10000; //需扫描的条目数 
TScan scan;
string tableName="table";
vector vecRes;
string startRow="";
scan.__set_startRow(startRow); //如果不设置则从表头开始扫描
scanId=client.openScanner(tableName,scan);
client.getScannerRows(vecRes,scanId,num); //结果存入vecRes中
client.closeScanner(scanId);
for(std::vector::iterator iter=vecRes.begin();iter!=vecRes.end();iter++)
{
    //TODO
}

你可能感兴趣的:(linux)