使用C++(通过Thrift) 操作Hbase

来源:互联网 发布:阿里云服务器ftp搭建 编辑:程序博客网 时间:2024/05/17 02:30

下载Thrift         http://thrift.apache.org/

下载libevent     http://monkey.org/~provos/libevent/

下载boost         http://www.boost.org/

thrift依赖libevent,boost.

安装libevent:

      ./configure --prefix=/usr/local/libevent

      make

      make install

安装boost

       ./bootstrap.sh --prefix=/usr/local/boost

安装Thrift

        chmod +x configure

        ./configure --with-boost=/usr/loca --prefix=/usr/local/thrift

         make

         make install

用Thrift生成访问Hbase所需的C++文件

        /usr/local/thrift/bin/thrift --gen cpp $hbase/hbase-thritf/src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift

       注意不同的版本Hbase.thrift在不同的地方,需要find $hbase -name "hbase.thrift"

       在执行目录下生成gen-cpp目录,包括:

       Hbase_constants.cpp Hbase_constants.h Hbase.cpp Hbase.h Hbase_server.skeleton.cpp Hbase_types.cpp Hase_types.h

       除了Hbase_server.skeleton.cpp之外,其余文件都是程序中要用到的。

在程序中使用thrift来访问Hbase

       要能通过Thrift访问Hbase,首先打开Hbase的Thrift服务

       $hbase/bin/hbase-daemon.sh start thrift

       //thrift默认的监听端口是9090,可以用netstat -n |grep 9090 查看是否被使用

//简单实例

//oper_hbase.h

#ifndef __OPER_HBASE_H
#define __OPER_HBASE_H   

#include<string>

#include<protocol/TBinaryProtocol.h>

#include<transport/TSocket.h>

#include<transport/TTransportUtils.h>

#include"Hbase.h"

using namespace apache::thrift;

using namespace apache::thrift::protocol;

using namespace apache::thrift::transport;

using namespace apache::hadoop::hbase::thrift;

typedef struct hbaseRet{

       std::string rowValue;

       time_t ts;

       hbaseRet(){

               ts=0;

       }

}hbaseRet;

class COperHbase

{

         public:

                COperHbase();

                 virtual ~COperHbase();

          private:

                 boost::shared_ptr<TTransport> socket;

                 boost::shared_ptr<TTransport> transport;

                 boost::shared_ptr<TProtocol> protocol;

                 HbaseClient *client;

                 std::string hbaseServiceHost;

                 int hbaseServicePort;

                 bool isConnected;

           public:

                  bool connect();

                  bool connect(std::string host,int port);

                  bool disconnect();

                  bool putRow(const Text &tableName,const Text &rowKey,const Text &column,const Text &rowValue);

                  bool getRow(hbaseRet &result,const Text &tableName,const Text &rowKey,const Text &columnName);

};

#endif

//oper_hbase.cpp

#include "oper_hbase.h"

#include <iostream>

using namespace std;

using namespace apache::thrift;

using namespace apache::thrift::protocol;

using namespace apache::thrift::transport;

using namespace apache::hadoop::hbase::thrift;

COperHbase::COperHbase():

         socket((TSock*)NULL),transport((TBufferedTransport*)NULL),protocol((TBinaryProtocol*)NULL),client(NULL),hbaseServicePort(9090),isConnected(false)

         {

          }

COperHbase::~COperHbase()

{

        if(isConnected){

                disconnect();

         }

         if(NULL!=client){

                  delete client;

                  client = NULL;

         }

}

bool COperHbase::connect()

{

        if(isConnected){

                  std::cout<<"Already connected,don't need to connect it again"<<std:endl;

                  return true;

         }

         try{

               socket.reset(new TSocket(hbaseServiceHost,hbaseServicePort));

               transport.reset(new TBufferedTransport(socket));

               protocol.reset(new TBinaryProtocol(transport));

               client=new HbaseClient(protocol);

               transport->open();

         }catch(const TException &tx){

               std::cout<<"Connect hbase error:"<<tx.what()<<std::endl;

               return false;

         }

         isConnected=true;

         return isConnected;

}

bool COperHbase::connect(std::string host,int port)

{

       hbaseServiceHost = host;

       hbaseServicePort = port;

       return connect();

}

bool COperHbase::disconnect()

{

        if(!isConnected){

              std::cout<<"Haven't connected to Hbase yet,can't disconnect from it"<<std::endl;

               return false;

        }

        if(NULL!=transport){

                 try{

                        transport->close();

                 }catch(const TException &tx){

                         std::cout<<"Disconnect Hbase error:"<<tx.what()<<std:endl;

                         return false;

                 }

        }else{

               return false;

        }

        isConnected=false;

        return true;

}

bool COperHbase::putRow(const Text &tableName,const Text &rowKey,const Text &column,const Text &rowValue)

{

      if(!isConnected){

             std::cout<<"Haven't connected hbase yet,can't put row"<<std::endl;

             return false;

      }

      try{

            std::vector<Mutation> mutations;

            std::map<Text,Text> attributes;

            mutations.push_back(Mutation());

            mutations.back().column = column;

            mutations.back().value = rowValue;

            attributes.insert(pair<Text,Text>(column,rowVallue));

            client->mutateRow(tableName,rowKey,mutations,attributes);

       }catch(const TException &tx){

              std::cout<<"Operate hbase error:"<<tx.what()<<std::endl;

              return false;

       }

       return true;

}

bool COperHbase::getRow(hbaseRet &result,const Text &tableName,const Text &rowKey,const Text &columnName)

{

       if(!isConnected){

              std::cout<<"Haven't connected to hbase yet,can't read data from it"<<std::endl;

               return false;

       }

        std::vector<std::string> columnNames;

        columnNames.push_back(columnName);

        std::vector<TRowResult> rowResult;

        std::map<Text,Text> attributes;

        try{

              client->getRowWithColumns(rowResult,tableName,rowKey,columnNames,attributes);

        }catch(const TException &tx){

               std::cout<<"Operate hbase error:"<<tx.what()<<std::endl;

               return false;

        }

        if(0==rowResult.size()){

              std::cout<<"get no record with the key:["<<rowKey<<"]"<<std:endl;

              return false;

        }

        std::map<std::string,TCell>::const_iterator it=rowResult[rowResult.size()-1].columns.begin();

        result.rowValue = it->second.value;

        return true;

}

/* test main

int main()

{

      hbaseRet hbr;

      COperHbase coh;

      coh.connect("10.1.1.2",9090);

      coh.putRow("test","1","info:test","123456");

      coh.getRow(hbr,"test","1","info:test");

      std::cout<<"rowkey:1 info:test,value:"<<hbr.rowValue<<std::endl;

      coh.disconnect();

       return 0;

}

*/

g++ Hbase.cpp Hbase_types.cpp TApplicationException.cpp TSocket.cpp Thrift.cpp TBufferTransports.cpp Hbase_constants.cpp oper_hbase.cpp \

       -I$thriftxx/lib/cpp/thrift/ \ 

       -I$thriftxx/lib/cpp/src/thrift/protocol/ \

       -I$thriftxx/lib/cpp/src/thrift/transport/ \

       -I$thriftxx/lib/cpp/src/

 

0 0
原创粉丝点击