用c++ 通过thrift2 访问hbase数据库,
1、保证服务端的hbase thrift2服务正常运行。(hbase版本 1.1.2; hadoop版本 2.4.2; 系统 ubuntu14.04 x64)
2、客户端代码: cpp代码、Makefile、 gen-cpp
a、从hbase 安装目录下拷贝hbase2.thrift 到当前目录 (ex:/usr/hdp/2.4.2.0-258/hbase/include/thrift)。
b、用thrift --gen cpp hbase2.thrift 生成c++代码 ,生成位置为hbase2.thrift 同目录下的 gen-cpp(hbase2_constants.cpp hbase2_constants.h hbase2_types.cpp hbase2_types.h THBaseService.cpp THBaseService.h THBaseService_server.skeleton.cpp)。
c 、HbaseClient.cpp文件内容
#include "THBaseService.h"
#include <config.h>
#include <vector>
#include <transport/TSocket.h>
#include <transport/TBufferTransports.h>
#include <protocol/TBinaryProtocol.h>
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;
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(new TBufferedTransport(socket));
boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
try {
transport->open();
printf("open\n");
THBaseServiceClient client(protocol);
TResult tresult;
TGet get;
std::vector<TColumnValue> cvs;
const std::string table("m");
const std::string thisrow="sss";
//get data
get.__set_row(thisrow);
bool be = 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) {
std::cerr << "ERROR(exception): " << tx.what() << std::endl;
}
fprintf(stderr, "readdb stop\n");
return 0;
return 0;
}
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(new TBufferedTransport(socket));
boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
try {
char buf[128];
transport->open();
printf("open\n");
THBaseServiceClient client(protocol);
TResult tresult;
TGet get;
std::vector<TPut> puts;
const std::string table("m");
for(int i = 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) {
std::cerr << "ERROR(exception): " << tx.what() << std::endl;
}
fprintf(stderr, "writedb stop\n");
return 0;
}
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);
return 0;
}
d、Makefile文件
THRIFT_DIR = /usr/local/include/thrift
LIB_DIR = /usr/local/lib
GEN_SRC = ./gen-cpp/hbase2_types.cpp ./gen-cpp/hbase2_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 -rf HbaseClient
help:
$(warning "Makefile for C++ Hbase Thrift HbaseClient. Modify THRIFT_DIR and LIB_DIR in the \
file to point to correct locations. See $${HBASE_ROOT}/hbase-examples/README.txt for \
details.")
@:
e、执行make
f、生成可执行文件HbaseClient
g、执行指令 ./HbaseClient ip地址 端口号