thrift2访问hbase

使用thrift2访问hbase

需求与环境

项目中需要使用c++访问hbase批量导入数据,所以这两天配置了一个hadoop集群,安装了hadoop,zookeeper,hbase。
使用三台虚拟机,一个namenode节点和2个datanode节点。

hbase thrift2简介

hbase使用java语言编写,原生的hbase客户端是不支持C++的,所以选择使用thrift完成项目的开发。
thrift是一个有facebook贡献的开源项目,用于多语言之间的通信,定义好IDL接口后,可实现二进制形式的高性能通信。
运行方式是client/server方式。
在hbase中,官方实现了两个版本的thrift,分别是thrift和thrift2。官方推荐thrift2,thrift以后就不支持了。(但是从hbase的git提交时间来看,thrift2也是好久没更新了,thrift2的cpp示例更是没有,只有thrift的)

hbase的thrift运行方式如下:

server端

hbase本身已经集成了thrift,使用以下命令开启hbase thrift的服务器端。关闭是将start修改为stop即可。默认情况下监听9090端口。

hbase-daemon.sh start thrift2
hbase-daemons.sh start thrift2 (集群版本)

client端

首先需要安装apache thrift客户端,网上有大量的安装教程,这里不在复述,需要注意的是需要安装大量的依赖包,可以先用./configure发现需要安装的依赖包,然后再安装需要的依赖包,根据自己的系统选择需要的包,没有必要按照五花八门的教程安装好多已经安装的包。
安装过程一如既往

./configure & make & make install

hbase thrift2使用

由于官方没有给出thrift2示例,之前没有接触过thrift和IDL,所以刚开始有些吃力。所以上网找资料,感谢走在左边博客,在这个博客的基础上,根据自己的hbase版本和安装路径,成功编译成功了简单示例。

版本与文件

我使用的hbase版本是1.0.1,thrift客户端版本是0.9.2。需要下载hbase带源码的包,示例程序都以及hbase.thrift都在这个包里。
示例代码如下,可惜的是没有cpp的thrift2版本的代码

hbase-1.0.1.1/hbase-examples/src/main$ ll
total 44
drwxr-xr-x 11 zhijia zhijia 4096  5月 18 03:02 ./
drwxr-xr-x  4 zhijia zhijia 4096  5月 16 06:50 ../
drwxr-xr-x  2 zhijia zhijia 4096  5月 18 03:02 asciidoc/
drwxr-xr-x  4 zhijia zhijia 4096  6月  2 17:36 cpp/
drwxr-xr-x  3 zhijia zhijia 4096  5月 16 06:50 java/
drwxr-xr-x  3 zhijia zhijia 4096  6月  2 17:36 perl/
drwxr-xr-x  3 zhijia zhijia 4096  6月  2 17:36 php/
drwxr-xr-x  2 zhijia zhijia 4096  6月  2 17:36 protobuf/
drwxr-xr-x  4 zhijia zhijia 4096  5月 16 06:50 python/
drwxr-xr-x  3 zhijia zhijia 4096  6月  2 17:36 ruby/
drwxr-xr-x  3 zhijia zhijia 4096  5月 16 06:50 sh/

hbase thrift2的定义文件hbase.thrift存放路径如下

hbase-1.0.1.1/hbase-thrift/src/main/resources/org/apache/hadoop/hbase/thrift2$ ll
total 32
drwxr-xr-x 4 zhijia zhijia  4096  6月  2 17:36 ./
drwxr-xr-x 4 zhijia zhijia  4096  5月 16 06:50 ../
drwxrwxr-x 2 zhijia zhijia  4096  6月  2 10:56 gen-cpp/
drwxrwxr-x 3 zhijia zhijia  4096  6月  1 15:03 gen-py/
-rw-r--r-- 1 zhijia zhijia 14140  5月 18 03:12 hbase.thrift

启动thrift服务器

zhijia@HadoopMaster:~$ hbase-daemons.sh start thrift2
HadoopSlaver1: starting thrift2, logging to /usr/isms/hadoop/hbase1.0.1/bin/../logs/hbase-zhijia-thrift2-HadoopSlaver1.out
HadoopSlaver2: starting thrift2, logging to /usr/isms/hadoop/hbase1.0.1/bin/../logs/hbase-zhijia-thrift2-HadoopSlaver2.out

编写thrift客户端代码

makefile
THRIFT_DIR = /usr/local/include/thrift
LIB_DIR = /usr/local/lib

GEN_SRC = ./gen-cpp/Hbase.cpp \
     ./gen-cpp/Hbase_types.cpp \
     ./gen-cpp/Hbase_constants.cpp

GEN_SRC = ./gen-cpp/hbase_types.cpp ./gen-cpp/hbase_constants.cpp ./gen-cpp/THBaseService.cpp
.PHONY: clean help

default: DemoClient Insert

HabaseClietn.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 main(int argc, char **argv) {
    boost::shared_ptr<TSocket> socket(new TSocket("172.16.85.161", 9090));
    boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
    boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));

    transport->open();
    printf("open\n");
    THBaseServiceClient client(protocol);
    TResult tresult;
    TGet get;
    const std::string table("example");
    const std::string thisrow="family1";
    get.__set_row(thisrow);
    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("list size: %d\n",list.size());
     printf("get : %s, %s,%s\n",(*iter).family.c_str(),(*iter).qualifier.c_str(),(*iter).value.c_str());//,(*iter).timestamp
}

    transport->close();
    printf("close\n");
    return 0;
}
Insert.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 main(int argc, char **argv) {
    boost::shared_ptr<TSocket> socket(new TSocket("172.16.85.161", 9090));
    boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
    boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));

    transport->open();
    TPut put;
    THBaseServiceClient client(protocol);
    const std::string table="example";
    const std::string thisrow="first";
    put.__set_row(thisrow);
    TColumnValue columnValue;
    const std::string thisfamily="family1";
    columnValue.__set_family(thisfamily);
    const std::string thisqualifier="fromcpp";
    columnValue.__set_qualifier(thisqualifier);
    const std::string thisvalue="vppisok";
    columnValue.__set_value(thisvalue);
    vector<TColumnValue> columnValues;
    columnValues.push_back(columnValue);
    put.__set_columnValues(columnValues);
    client.put(table,put);
    transport->close();
    printf("close\n");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值