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.")
@: