您的位置:首页 > 编程语言 > C语言/C++

C++语言使用thrift1方式访问hbase

2016-03-31 17:05 351 查看

开发环境

rhel-server-6.3-x86_64-dvd.iso

cloudera-manager-el6-cm5.5.1_x86_64.tar.gz

搭建步骤

1.安装bison-2.6.2(./configure &&make&&make install)

2.安装boost_1_60_0(././bootstrap.sh &&./b2&&./b2 install)

3.升级openssl

rpm -Uvh openssl-1.0.1e-42.el6.x86_64.rpm

rpm -Uvh openssl-devel-1.0.1e-42.el6.x86_64.rpm

4.安装thrift-0.9.3(./configure &&make&&make install)

5.thrift --gen cpp /opt/cloudera/parcels/CDH/lib/hbase/include/thrift/hbase1.thrift

6.测试代码参考http://haoningabc.iteye.com/blog/2032878

7.测试之前注意启动thrift demain:/opt/cloudera/parcels/CDH/lib/hbase/bin/hbase-daemon.sh start thrift

测试代码

#include "Hbase.h"
#include <config.h>
#include <vector>
#include <transport/TSocket.h>
#include <transport/TBufferTransports.h>
#include <protocol/TBinaryProtocol.h>
#include <time.h>
#include <sys/timeb.h>
#include <stdlib.h>
#include <stdio.h>

using namespace std;
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace apache::hadoop::hbase::thrift;
using boost::shared_ptr;

#if 1
#define TIME_SPEND_BEGIN \
time_t ltime1, ltime2, tmp_time; \
struct timeb tstruct1, tstruct2; \
ftime (&tstruct1); \
time (<ime1);

#define TIME_SPEND_END_PRINT \
time (<ime2); \
ftime (&tstruct2);\
tmp_time = (ltime2 * 1000 + tstruct2.millitm) - (ltime1 * 1000 + tstruct1.millitm);\
printf("Spend %dms.\n", tmp_time);
#else
#define TIME_SPEND_BEGIN
#define TIME_SPEND_END_PRINT
#endif

#define TABLE_NAME "test"
int main(int argc, char **argv) {
boost::shared_ptr<TSocket> socket(new TSocket("127.0.0.1", 9090));
boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
char szBuff[] = {'$', 'a', '\0', 'b', '!'};
HbaseClient client(protocol);
int iLoop;
char szNum[16];
vector < Mutation > vmutations;
Mutation stMutation;
map < Text, Text > attributes;
ColumnDescriptor stColumnDescriptor;
vector < ColumnDescriptor > astcolumnFamilies;
vector < TRowResult > astRowResult;

transport->open();

TIME_SPEND_BEGIN
/* create table */
stColumnDescriptor.__set_name("vedio");
astcolumnFamilies.push_back(stColumnDescriptor);
client.createTable(TABLE_NAME, astcolumnFamilies);

/* insert data to table */
Text stDataBuff(szBuff, sizeof(szBuff));
stMutation.__set_column("vedio:data");
stMutation.__set_value(stDataBuff);
stMutation.__set_isDelete(false);
stMutation.__set_writeToWAL(false);
vmutations.push_back(stMutation);
for (iLoop = 1; iLoop <=10000; iLoop++)
{
szNum[0] = '\0';
snprintf(szNum, sizeof(szNum), "test%05d", iLoop);

client.mutateRow(TABLE_NAME, szNum, vmutations, attributes);
}
client.getRow( astRowResult, TABLE_NAME, szNum, attributes);
TIME_SPEND_END_PRINT

transport->close();

return 0;
}


makefile

BOOST_DIR = /usr/include/boost
THRIFT_DIR = /root/mml/thrift-0.9.3/lib/cpp/src/thrift
THRIFT_SRC = /root/mml/thrift-0.9.3/lib/cpp/src
LIB_DIR = /usr/local/thrift/lib/
GEN_SRC = ./gen-cpp/hbase2_types.cpp ./gen-cpp/hbase2_constants.cpp ./gen-cpp/THBaseService.cpp
default: client
client: HbaseClientWrite.cpp
g++ -g -o HbaseClientWrite -lm -pthread -lz -lrt -lssl -I${THRIFT_SRC} -I${THRIFT_DIR} -I${BOOST_DIR}  -I./gen-cpp -L${LIB_DIR} -lthrift HbaseClientWrite.cpp ${GEN_SRC}
clean:
$(RM) -r HbaseClientWrite
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: