C++读写HBase代码

1. C++读写HBase代码

1.1 HbaseClient.cpp

#include "THBaseService.h" 
#include <config.h> 
#include <vector> 
#include <transport/TSocket.h> 
#include <transport/TBufferTransports.h>
#include <protocol/TBinaryProtocol.h> 
#include <string.h>
 
using namespace std; 
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport; 
using namespaceapache::hadoop::hbase::thrift2; 
 
using boost::shared_ptr; 
 
 
int readdb(int argc, char** argv)
{
 
   fprintf(stderr, "readdb start\n");
       
    int port= atoi(argv[2]);
   boost::shared_ptr<TSocket> socket(new TSocket(argv[1],port)); 
   boost::shared_ptr<TTransport> transport(newTBufferedTransport(socket)); 
   boost::shared_ptr<TProtocol> protocol(newTBinaryProtocol(transport)); 
    try {
 
       transport->open(); 
       printf("open\n");
       THBaseServiceClient client(protocol); 
       TResult tresult; 
        TGetget;
 
 
       std::vector<TColumnValue> cvs;
       const std::string table("m"); 
       const std::string thisrow="ss-9-8";
       
 
 
       //get data
       get.__set_row(thisrow); 
        boolbe = client.exists(table,get);
       
       printf("exists result value = %d\n", be);
 
 
       client.get(tresult,table,get);
       vector<TColumnValue> list=tresult.columnValues; 
       std::vector<TColumnValue>::const_iterator iter; 
       for(iter=list.begin();iter!=list.end();iter++) { 
           printf("%s, %s,%s\n",(*iter).family.c_str(),(*iter).qualifier.c_str(),(*iter).value.c_str());
        }
       transport->close(); 
        printf("close\n"); 
     } catch(const TException &tx) {
              fprintf(stderr,"error:%s\n", tx.what());
     }
   fprintf(stderr, "readdb stop\n");
    return0;
}
 
int writedb(int argc, char** argv)
{
   fprintf(stderr, "writedb start\n");
    int port= atoi(argv[2]);
   boost::shared_ptr<TSocket> socket(new TSocket(argv[1], port));
   boost::shared_ptr<TTransport> transport(newTBufferedTransport(socket));
   boost::shared_ptr<TProtocol> protocol(newTBinaryProtocol(transport));
    try
    {
        charbuf[128];
       transport->open(); 
       printf("open\n");
       THBaseServiceClient client(protocol); 
       TResult tresult; 
        TGetget;
       std::vector<TPut> puts;
       const std::string table("m"); 
        for(inti = 0; i < 10; i++) {
           fprintf(stderr, "%d, ", i);
           for(int j = 0; j < 10; j++) {
               TPut put;
               std::vector<TColumnValue> cvs;
               //put data
               sprintf(buf, "ss-%d-%d", i, j);
               const std::string thisrow(buf);
               put.__set_row(thisrow);
               TColumnValue tcv;
               tcv.__set_family("info");
               tcv.__set_qualifier("age");
               sprintf(buf, "%d", i * j);
                tcv.__set_value(buf);
               cvs.insert(cvs.end(), tcv);
               put.__set_columnValues(cvs);
               puts.insert(puts.end(), put);
           }
           client.putMultiple(table, puts);
           puts.clear();
        }
 
 
       transport->close(); 
       printf("close\n"); 
    } catch(const TException &tx) {
              fprintf(stderr,"error:%s\n", tx.what());
    }
   fprintf(stderr, "writedb stop\n");
    return0;
}
 
 
int main(int argc, char **argv) { 
    if(argc!= 3) {
       fprintf(stderr, "param  is:XX ip port\n");
       return -1;
    }
//   writedb(argc, argv);
   readdb(argc, argv);
    return0; 
} 


1.2 Makefile

THRIFT_DIR = /usr/local/include/thrift
LIB_DIR = /usr/local/lib
 
GEN_SRC = ./gen-cpp/hbase_types.cpp ./gen-cpp/hbase_constants.cpp./gen-cpp/THBaseService.cpp
 
.PHONY: clean help
 
default: HbaseClient
HbaseClient: HbaseClient.cpp
       g++  -o HbaseClient -I${THRIFT_DIR}  -I./gen-cpp -L${LIB_DIR}-Wl,-rpath,${LIB_DIR} HbaseClient.cpp ${GEN_SRC} -lthrift -g
 
clean:
       rm -rfHbaseClient
 
help:
       $(warning"Makefile for C++ Hbase Thrift HbaseClient. Modify THRIFT_DIR and LIB_DIRin the \
file to point to correct locations. See$${HBASE_ROOT}/hbase-examples/README.txt for \
details.")
       @:


 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值