ImageVerifierCode 换一换
格式:DOCX , 页数:15 ,大小:21.25KB ,
资源ID:26247055      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/26247055.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(C++访问操作读写Hbase.docx)为本站会员(b****9)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

C++访问操作读写Hbase.docx

1、C+访问操作读写Hbase使用C+(通过Thrift)访问/操作/读写Hbase无奈,网上关于C+访问Hbase的文章实在太少,所以只好自己折腾一下,然后写出来了。要使用C+访问Hbase,可以走的途径少之又少,据说当前最好的方法就是通过Thrift来实现:http:/thrift.apache.org/所以本文分成几部分:(1)安装Thrift;(2)用Thrift 生成访问Hbase所需的C+文件;(3)在程序中通过Thrift来访问Hbase。另外,本文只包含读写Hbase数据的例子,不包含配置Hbase的方法,如需这些内容,请自行搜索。首先声明一下,本文基于以下环境:操作系统:RHEL

2、 5.3,64位Thrift 版本:0.7.0要访问的 Hbase 版本:0.20.6我使用0.90.4的 Hbase 安装包来生成C+所需的Hbase.h等文件(用新版的应该能兼容旧版的)下面开始,一步步来。(1)安装Thrift不是一件很轻松的事。如果你的系统比较干净,可能很顺利地就搞定了,如果有依赖库缺失问题或者库冲突问题,那么就只能根据具体情况,一个个问题去fix了,谁知道会有多少麻烦。我运气比较好,在一个干净的系统上,很快就完成了。Thrift 至少会依赖于两个系统中一般不会带的库:libevent,boost。libevent 到这里下载:http:/monkey.org/prov

3、os/libevent/ 我使用的版本是:2.0.12-stableboost 到这里下载:http:/www.boost.org/ 我使用的版本是:1.47.0安装libevent:123./configure -prefix=/usr/local/libeventmakemake install安装boost(boost不像一般的Linux源码安装包一样,它的安装不是configure,make,make install,有点怪):1./bootstrap.sh -prefix=/usr/local/boost不出错的话接着执行以下命令开始编译(也可以通过编辑project-config.

4、jam文件调整编译参数):12./b2./b2 install安装Thrift:1234chmod +x configure./configure -with-boost=/usr/local -prefix=/usr/local/thriftmakemake install至此,安装Thrift 的工作就完成了。(2)用Thrift 生成访问Hbase所需的C+文件访问Hbase需要在你的程序中使用若干.h,.cpp文件,这些文件是用 Thrift 生成的。解压Hbase源码安装包:12tar zxf hbase-0.90.4.tar.gzcd hbase-0.90.4在解压出来的文件中,

5、你可以找到一个名为 Hbase.thrift 的文件,这个文件定义了如何通过 Thrift 接口来访问Hbase。用这个Thrift文件,可以生成访问Hbase所需的C+文件:shel/usr/local/thrift/bin/thrift gen cpp ./src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift/shell会发现生成了gen-cpp目录:123ls gen-cpp/Hbase_constants.cpp Hbase_constants.h Hbase.cpp Hbase.h Hbase_server.sk

6、eleton.cpp Hbase_types.cpp Hbase_types.h除了Hbase_server.skeleton.cpp之外,其余文件都是在我们的程序里要用到的,将它们拷贝到我们的工程目录下。(3)在程序中使用Thrift来访问Hbase要能通过 Thrift 访问Hbase,你必须首先要打开HBase的 Thrift 服务,请参考其他文档确保这一点是可用的。下一步,我们在程序中如何读取Hbase的数据?我们先看看hbase源码安装包中自带的例子:在解压出来的安装包中的 examples/thrift/ 目录下的 DemoClient.cpp 文件,有如下代码:123456789

7、10111213boost:shared_ptr socket(new TSocket(localhost, 9090);boost:shared_ptr transport(new TBufferedTransport(socket);boost:shared_ptr protocol(new TBinaryProtocol(transport);HbaseClient client(protocol);try transport-open();/ do somethingtransport-close(); catch (TException &tx) printf(ERROR: %sn,

8、 tx.what();我们就仿照这个例子来做。从DemoClient.cpp可见,我们要先创建三个指针socket,transport和protocol,后两个 分别依赖于前两个,最后再创建一个client对象,我们操作Hbase就是使用这个client对象。在操作Hbase前,需要先打开到Hbase Thrift service的连接,即 transport-open(),在操作完 Hbase之后,需要关闭连接,即 transport-close(),这下就比较清楚了:我们可以写一个自己的类CHbaseOperate,它应该有一个open函数和一个 close函数,分别用于打开、关闭连接,还

9、应该有读写Hbase的基本功能。读写Hbase的方法,请参考Hbase.h中的函数,例子还是看 DemoClient.cpp。下面上代码:HbaseOperate.h:123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869#ifndef _HBASE_OPERATE_H#define _HBASE_OPERATE_H#include #include #include #include #in

10、clude Hbase.h/* Class to operate Hbase.* author Darran Zhang ()* version 11-08-24* declaration These codes are only for non-commercial use, and are distributed on an AS IS basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.* You must not remove this declaration at any time.*/using namesp

11、ace apache:thrift;using namespace apache:thrift:protocol;using namespace apache:thrift:transport;using namespace apache:hadoop:hbase:thrift;typedef struct hbaseRet std:string rowValue;time_t ts;hbaseRet() ts = 0; hbaseRet;class CHbaseOperatepublic:CHbaseOperate();virtual CHbaseOperate();private:boos

12、t:shared_ptr socket;boost:shared_ptr transport;boost:shared_ptr protocol;HbaseClient *client;std:string hbaseServiceHost;int hbaseServicePort;bool isConnected;public:bool connect();bool connect(std:string host, int port);bool disconnect();bool putRow(const std:string &tableName,const std:string &row

13、Key,const std:string &column,const std:string &rowValue);bool getRow(hbaseRet &result,const std:string &tableName,const std:string &rowKey,const std:string &columnName);#endifHbaseOperate.cpp:123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585

14、960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168#include Hba

15、seOperate.h#include log4cxx/log4cxx.h#include log4cxx/propertyconfigurator.hstatic log4cxx:LoggerPtr logger(log4cxx:Logger:getLogger(HbaseOperate.cpp);/* Class to operate Hbase.* author Darran Zhang ()* version 11-08-24* declaration These codes are only for non-commercial use, and are distributed on

16、 an AS IS basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.* You must not remove this declaration at any time.*/using namespace std;CHbaseOperate:CHbaseOperate() :socket(TSocket*)NULL), transport(TBufferedTransport*)NULL), protocol(TBinaryProtocol*)NULL), client(NULL), hbaseServicePort

17、(9090), isConnected(false)CHbaseOperate:CHbaseOperate()if (isConnected) disconnect();if (NULL != client) delete client;client = NULL;/* Connect Hbase.*/bool CHbaseOperate:connect()if (isConnected) LOG4CXX_INFO(logger, Already connected, dont need to connect it again);return true;try socket.reset(new

18、 TSocket(hbaseServiceHost, hbaseServicePort);transport.reset(new TBufferedTransport(socket);protocol.reset(new TBinaryProtocol(transport);client = new HbaseClient(protocol);transport-open(); catch (const TException &tx) LOG4CXX_ERROR(logger, Connect Hbase error : close(); catch (const TException &tx

19、) LOG4CXX_ERROR(logger, Disconnect Hbase error : tx.what();return false; else return false;isConnected = false;return true;/* Put a row to Hbase.* param tableName IN The table name.* param rowKey IN The row key.* param column IN The column family : qualifier.* param rowValue IN The row value.* retur

20、n True for successfully put the row, false otherwise.*/bool CHbaseOperate:putRow(const string &tableName, const string &rowKey, const string &column, const string &rowValue)if (!isConnected) LOG4CXX_ERROR(logger, Havent connected to Hbase yet, cant put row);return false;try std:vector mutations;muta

21、tions.push_back(Mutation();mutations.back().column = column;mutations.back().value = rowValue;client-mutateRow(tableName, rowKey, mutations); catch (const TException &tx) LOG4CXX_ERROR(logger, Operate Hbase error : tx.what();return false;return true;/* Get a Hbase row.* param result OUT The object w

22、hich contains the returned data.* param tableName IN The Hbase table name, e.g. MyTable.* param rowKey IN The Hbase row key, e.g. kdr23790.* param columnName IN The column family : qualifier.* return True for successfully get the row, false otherwise.*/bool CHbaseOperate:getRow(hbaseRet &result, con

23、st std:string &tableName, const std:string &rowKey, const std:string &columnName)if (!isConnected) LOG4CXX_ERROR(logger, Havent connected to Hbase yet, cant read data from it);return false;std:vector columnNames;columnNames.push_back(columnName);std:vector rowResult;try client-getRowWithColumns(rowR

24、esult, tableName, rowKey, columnNames); catch (const TException &tx) LOG4CXX_ERROR(logger, Operate Hbase error : tx.what();return false;if (0 = rowResult.size() LOG4CXX_WARN(logger, Got no record with the key : rowKey );return false;std:map:const_iterator it = rowResultrowResult.size() -1.columns.begin();result.rowValue = it-second.value;result.ts = it-second.timestamp;return true;注意我在程序中使用了Apache log4cxx这个记录日志的库来打印/保存程序运行日志,使用方法可参考【此链接】。如果你不想用,可以自己改为std:

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1