目录

目录 1

1. 前言 1

2. 启动和停止thrift2 1

2.1. 启动thrift2 1

2.2. 停止thrift2 1

2.3. 启动参数 2

3. hbase.thrift 2

3.1. 编译hbase.thrift 2

4. thrift_helper.h 3

5. 示例代码 4

1. 前言

本文目的是介绍使用C++如何操作HBase。从HBase 0.94开始,HBase新增thrift2,本文只介绍和讨论thrift2相关的。hbase-1.1.2使用的thrift估计是thrift-0.9.0版本。

2. 启动和停止thrift2

2.1. 启动thrift2

登录HBase master机器,执行以下命令启动thrift2:hbase-daemon.sh start thrift2。

thrift默认的监听端口是9090,可以通过参数“-p”指定其它端口。默认使用的Server是TThreadPoolServer。默认使用的Protocol是TBinaryProtocol。

注意客户端使用的Protocol和Transport和服务端的要保持一致,否则客户端在调用时,可能遇到“EAGAIN (timed out)”等错误。

2.2. 停止thrift2

hbase-daemon.sh stop thrift2

2.3. 启动参数

使用“hbase-daemon.sh start thrift2”时,还可以带以下参数:

参数名

是否默认

参数说明

-h, --help

显示帮助信息

-b, --bind

绑定指定地址,但不支持TNonblockingServer和THsHaServer,两者总是使用“0.0.0.0”

-p, --port

9090

绑室指定端口,默认为9090

-f, --framed

使用TFramedTransport

-c, --compact

使用TCompactProtocol,默认是TBinaryProtocol

-threadpool

使用TThreadPoolServer,为默认Server

-nonblocking

使用实现了FramedTransport的TNonblockingServer

-hsha

使用实现了FramedTransport的THsHaServer

客户端和hbase thrift2的Transport和Protocol需保持一致,比如客户端为FramedTransport,则也需以“-f”启动hbase thrift2。

否则客户端在调用时,可能会遇到“EAGAIN (timed out)”等错误。

启动参数信息来源于官网的页面:

https://hbase.apache.org/devapidocs/org/apache/hadoop/hbase/thrift2/package-summary.html

以上参数不是给hbase-daemon.sh使用,而是被hbase thrift2使用,可以浏览相关源代码了解细节:

hbase-thrift\src\main\java\org\apache\hadoop\hbase\thrift2\ThriftServer.java

hbase-thrift\src\main\java\org\apache\hadoop\hbase\thrift2\ThriftHBaseServiceHandler.java

Ø 启动示例:

hbase-daemon.sh start thrift2 --framed -nonblocking

3. hbase.thrift

hbaser.thrift文件在hbase源代码包(以hbase-1.1.2为例)中的位置:

hbase-thrift\src\main\resources\org\apache\hadoop\hbase\thrift2\hbase.thrift

3.1. 编译hbase.thrift

保持机器上已安装好thrift(经测试hbase-1.1.2和thrift-0.9.0兼容),然后使用下列命令编译:thrift --gen cpp -out . hbase.thrift,编译成功后,会在“-out”指定的目录下生成以下五个文件:

THBaseService.h

THBaseService.cpp

hbase_types.h

hbase_types.cpp

hbase_constants.h

hbase_constants.cpp

其中供客户端使用的是位于文件THBaseService.h中的类THBaseServiceClient。

4. thrift_helper.h

为了简化C++客户端的编程,可以使用thrift_helper.h:

https://github.com/eyjian/mooon/blob/master/common_library/include/mooon/net/thrift_helper.h,它可以帮助简化对HBase thrift2的调用:

// thrift客户端辅助类

//

// 使用示例:

// mooon::net::CThriftClientHelper<ExampleServiceClient> client(rpc_server_ip, rpc_server_port);

// try

// {

//     client.connect();

//     client->foo();

// }

// catch (apache::thrift::transport::TTransportException& ex)

// {

//     MYLOG_ERROR("thrift exception: (%d)%s\n", ex.getType(), ex.what());

// }

// catch (apache::thrift::transport::TApplicationException& ex)

// {

//     MYLOG_ERROR("thrift exception: %s\n", ex.what());

// }

// catch (apache::thrift::TException& ex)

// {

//     MYLOG_ERROR("thrift exception: %s\n", ex.what());

// }

// Transport除默认的TFramedTransport (TBufferTransports.h),还可选择:

// TBufferedTransport (TBufferTransports.h)

// THttpTransport

// TZlibTransport

// TFDTransport (TSimpleFileTransport)

//

// Protocol除默认的apache::thrift::protocol::TBinaryProtocol,还可选择:

// TCompactProtocol

// TJSONProtocol

// TDebugProtocol

template <class ThriftClient,

class Protocol=apache::thrift::protocol::TBinaryProtocol,

class Transport=apache::thrift::transport::TFramedTransport>

class CThriftClientHelper

5. 示例代码

// HBase thrift2 C++编程示例

#include "THBaseService.h"

#include <inttypes.h> // PRIu64

#include <mooon/net/thrift_helper.h>

#include <mooon/utils/args_parser.h>

#include <vector>

// 请注意客户端使用的thrift的Transport和Protocol要和hbase thrift2服务端保持一致,

// 否则调用时,可能总是报超时,或其它错误!!!

//

// 运行之前,请通过HBase shell创建好表:create 'test','cf1','cf2'

// 或指定版本数:create 'test',{NAME=>'cf1',VERSIONS=>2},{NAME=>'cf2',VERSIONS=>3}

// 删除表,按顺序执行以下两条HBase shell命令:

// disable 'test'

// drop 'test'

STRING_ARG_DEFINE(hbase_ip, "192.168.0.1", "hbase thrift ip");

INTEGER_ARG_DEFINE(uint16_t, hbase_port, 9090, 1000, 50000, "hbase thrift port");

int main(int argc, char* argv[])

{

std::string errmsg;

if (!mooon::utils::parse_arguments(argc, argv, &errmsg))

{

fprintf(stderr, "parameter error: %s\n", errmsg.c_str());

exit(1);

}

using namespace apache;

using namespace apache::hadoop;

std::string hbase_ip = mooon::argument::hbase_ip->value();

uint16_t hbase_port = mooon::argument::hbase_port->value();

mooon::net::CThriftClientHelper<hbase::thrift2::THBaseServiceClient> hbase_client(hbase_ip, hbase_port);

try

{

hbase_client.connect(); // 连接hbase thrift2 server

fprintf(stdout, "connect %s:%d ok\n", hbase_ip.c_str(), hbase_port);

std::string tablename = "test";     // 表名,确保运行之前已创建好

std::string rowkey = "row1";        // 行Key

std::string family = "cf1";         // 例族名

std::string columnname = "f1";      // 例名

std::string columnvalue = "value1"; // 例值

// 插入参数设置

std::vector<hbase::thrift2::TColumnValue> columns_value(1);

columns_value[0].__set_family(family);

columns_value[0].__set_qualifier(columnname);

columns_value[0].__set_value(columnvalue);

hbase::thrift2::TPut put;

put.__set_row(rowkey);

put.__set_columnValues(columns_value);

hbase_client->put(tablename, put); // 插入,出错抛异常hbase::thrift2::TIOError

// 查询参数设置

hbase::thrift2::TGet input;

input.__set_row(rowkey);

hbase::thrift2::TResult result; // 查询结果存放在这里

hbase_client->get(result, tablename, input); // 查询,出错抛异常hbase::thrift2::TIOError

// 显示查询结果

for (int i=0; i<static_cast<int>(result.columnValues.size()); ++i)

{

const hbase::thrift2::TColumnValue& column_value_ref = result.columnValues[i];

fprintf(stdout, "family[%s]/qualifier[%s]/timestamp[%"PRIu64"]: %s\n", column_value_ref.family.c_str(),

column_value_ref.qualifier.c_str(),

column_value_ref.timestamp,

column_value_ref.value.c_str());

}

}

catch (hbase::thrift2::TIOError& ex)

{

fprintf(stderr, "IOError: %s\n", ex.what());

}

catch (apache::thrift::transport::TTransportException& ex)

{

// 如果和服务端的Transport和Protocol不同,这里的错误是“EAGAIN (timed out)”

fprintf(stderr, "(%d)%s\n", ex.getType(), ex.what());

}

catch (apache::thrift::TApplicationException& ex)

{

fprintf(stderr, "%s\n", ex.what());

}

catch (thrift::TException& ex)

{

fprintf(stderr, "%s\n", ex.what());

}

return 0;

}

如果thrift客户端报如下错误,有可能是因为一次写入的数据太多,导致包过大:

Thrift: Fri Apr 22 17:30:41 2016 TSocket::write_partial() send() <Host: 10.143.136.208 Port: 9090>Connection reset by peer

Thrift: Fri Apr 22 17:30:41 2016 TSocket::write_partial() send() <Host: 10.143.136.208 Port: 9090>Connection reset by peer

HBase & thrift & C++编程的更多相关文章

  1. python Hbase Thrift pycharm 及引入包

    cp -r hbase/ /usr/lib/python2.7/site-packages/ 官方示例子http://code.google.com/p/hbase-thrift/source/bro ...

  2. hbase thrift 定义

    /*  * Licensed to the Apache Software Foundation (ASF) under one  * or more contributor license agre ...

  3. HBase Thrift过滤语法

    摘抄自hbase ref guide 0.94: 在写本文的时候,hbase ref guide已经更新到1.2及2.0了,但是个人感觉Thrift过滤语法部分写得都没有0.94的好,省掉了examp ...

  4. Hbase使用MapReduce编程导出数据到HDFS

    废话少说,直接上代码! package cn.com.oozie.demo;  import java.io.IOException;  import org.apache.hadoop.conf.C ...

  5. hbase thrift 访问队列

    public class CallQueue implements BlockingQueue<Runnable> {   private static Log LOG = LogFact ...

  6. 搭建HBase+thrift+php环境

    http://www.beauty-soft.net/blog/ceiba/hadoop/2013-05-19/644.html http://www.360doc.com/content/11/07 ...

  7. 北风风hadoop课程体系

    课程一.基于Linux操作系统平台下的Java语言开发(20课时)课程简介本套课程主要介绍了Linux系统下的Java环境搭建及最基础的Java语法知识.学习Linux操作系统下Java语言开发的好处 ...

  8. 使用C#通过Thrift访问HBase

    前言 因为项目需要要为客户程序提供C#.Net的HBase访问接口,而HBase并没有提供原生的.Net客户端接口,可以通过启动HBase的Thrift服务来提供多语言支持. Thrift介绍 环境 ...

  9. 在Azure HDInsight HBase集群中使用Thrift接口

    Sun wei  Wed, Feb 25 2015 2:17 AM Apache Thrift 是一种可扩展的跨语言服务接口,可以通过内置的代码生成引擎帮助创建跨语言服务类库,Apache HBase ...

随机推荐

  1. python 常见问题总结

    1.ModuleNotFoundError: No module named 'urllib2' 在python3.x版本中,urllib和urllib2包集合成在一个包了import urllib2 ...

  2. Weex入门与进阶指南

    Weex入门与进阶指南 标签: WeexiOSNative 2016-07-08 18:22 59586人阅读 评论(8) 收藏 举报 本文章已收录于:  iOS知识库  分类: iOS(87)  职 ...

  3. coding github 配置ssl 免密拉取代码

    详细介绍: https://www.cnblogs.com/superGG1990/p/6844952.html 注:其中检验过程与下述不同,可以先在对应git库使用 git pull 一次,选择信任 ...

  4. emacs配置emacs-clang-complete-async

    debian下需要安装apt安装下clang和llvm sudo apt-get install llvm-dev sudo apt-get install libclang-dev 网上抄来一个大神 ...

  5. PHP常用获取文件路径的函数集合整理

    转自: http://blog.sina.com.cn/s/blog_71ed1b870102vslg.html 我们在开发PHP项目过程中,经常会用到包含文件,所以有时候需要获取文件的相对路径或者绝 ...

  6. week3-栈和队列

    1.学习总结 2.PTA实验作业 2.1 题目1:7-1 jmu-报数游戏 2.2 设计思路(伪代码或流程图) 2.3 代码截图 2.4 PTA提交列表说明. 答案错误:error少了 !: 非零返回 ...

  7. java基础-Map的静态初始化以及Map的遍历等.....................

    1.map的静态初始化,以及map遍历的几种方法: package com.cy.test; import java.util.HashMap; import java.util.Iterator; ...

  8. CentOS 添加新硬件硬盘,扩展空间而无需重启虚拟机

    运行如下命令,通过重新扫描 SCSI (注:Small Computer System Interface 小型计算机系统接口)总线并添加 SCSI 设备,系统就可以扩展操作系统的物理卷磁盘空间,而且 ...

  9. Oracle VM VirtualBox虚拟机安装Ubuntu Server

    安装过程如下:原文转自:http://www.linuxidc.com/Linux/2012-04/59368p8.htm

  10. grunt 不是内部或外部命令,也不是可运行的程序或批处理文件

    问题1 grunt 不是内部或外部命令,也不是可运行的程序或批处理文件 解决方法: Grunt和 Grunt 插件是通过 npm 安装并管理的,npm是 Node.js 的包管理器. 安装CLI 在继 ...