HBase thrift C++编程
HBase & thrift & C++編程.pdf
目錄
目錄 1
1.?前言 1
2.?啟動(dòng)和停止thrift2 1
2.1.?啟動(dòng)thrift2 1
2.2.?停止thrift2 1
2.3.?啟動(dòng)參數(shù) 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相關(guān)的。hbase-1.1.2使用的thrift估計(jì)是thrift-0.9.0版本。
2.?啟動(dòng)和停止thrift2
2.1.?啟動(dòng)thrift2
登錄HBase?master機(jī)器,執(zhí)行以下命令啟動(dòng)thrift2:hbase-daemon.sh?start?thrift2。
thrift默認(rèn)的監(jiān)聽端口是9090,可以通過參數(shù)“-p”指定其它端口。默認(rèn)使用的Server是TThreadPoolServer。默認(rèn)使用的Protocol是TBinaryProtocol。
注意客戶端使用的Protocol和Transport和服務(wù)端的要保持一致,否則客戶端在調(diào)用時(shí),可能遇到“EAGAIN?(timed?out)”等錯(cuò)誤。
2.2.?停止thrift2
hbase-daemon.sh?stop?thrift2
2.3.?啟動(dòng)參數(shù)
使用“hbase-daemon.sh?start?thrift2”時(shí),還可以帶以下參數(shù):
| 參數(shù)名 | 是否默認(rèn) | 參數(shù)說明 |
| -h,?--help | ? | 顯示幫助信息 |
| -b,?--bind | ? | 綁定指定地址,但不支持TNonblockingServer和THsHaServer,兩者總是使用“0.0.0.0” |
| -p,?--port | 9090 | 綁室指定端口,默認(rèn)為9090 |
| -f,?--framed | ? | 使用TFramedTransport |
| -c,?--compact | ? | 使用TCompactProtocol,默認(rèn)是TBinaryProtocol |
| -threadpool | 是 | 使用TThreadPoolServer,為默認(rèn)Server |
| -nonblocking | ? | 使用實(shí)現(xiàn)了FramedTransport的TNonblockingServer |
| -hsha | ? | 使用實(shí)現(xiàn)了FramedTransport的THsHaServer |
客戶端和hbase?thrift2的Transport和Protocol需保持一致,比如客戶端為FramedTransport,則也需以“-f”啟動(dòng)hbase?thrift2。
否則客戶端在調(diào)用時(shí),可能會(huì)遇到“EAGAIN?(timed?out)”等錯(cuò)誤。
啟動(dòng)參數(shù)信息來源于官網(wǎng)的頁面:
https://hbase.apache.org/devapidocs/org/apache/hadoop/hbase/thrift2/package-summary.html。
以上參數(shù)不是給hbase-daemon.sh使用,而是被hbase?thrift2使用,可以瀏覽相關(guān)源代碼了解細(xì)節(jié):
hbase-thrift\src\main\java\org\apache\hadoop\hbase\thrift2\ThriftServer.java
hbase-thrift\src\main\java\org\apache\hadoop\hbase\thrift2\ThriftHBaseServiceHandler.java
??啟動(dòng)示例:
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
保持機(jī)器上已安裝好thrift(經(jīng)測(cè)試hbase-1.1.2和thrift-0.9.0兼容),然后使用下列命令編譯:thrift?--gen?cpp?-out?.?hbase.thrift,編譯成功后,會(huì)在“-out”指定的目錄下生成以下五個(gè)文件:
THBaseService.h
THBaseService.cpp
hbase_types.h
hbase_types.cpp
hbase_constants.h
hbase_constants.cpp
其中供客戶端使用的是位于文件THBaseService.h中的類THBaseServiceClient。
4.?thrift_helper.h
為了簡(jiǎn)化C++客戶端的編程,可以使用thrift_helper.h:
https://github.com/eyjian/mooon/blob/master/common_library/include/mooon/net/thrift_helper.h,它可以幫助簡(jiǎn)化對(duì)HBase?thrift2的調(diào)用:
| //?thrift客戶端輔助類 // //?使用示例: //?mooon::net::CThriftClientHelper?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除默認(rèn)的TFramedTransport?(TBufferTransports.h),還可選擇: //?TBufferedTransport?(TBufferTransports.h) //?THttpTransport //?TZlibTransport //?TFDTransport?(TSimpleFileTransport) // //?Protocol除默認(rèn)的apache::thrift::protocol::TBinaryProtocol,還可選擇: //?TCompactProtocol //?TJSONProtocol //?TDebugProtocol template? ?thriftclient, ??????????class?Protocol=apache::thrift::protocol::TBinaryProtocol, ??????????class?Transport=apache::thrift::transport::TFramedTransport> class?CThriftClientHelper |
5.?示例代碼
| //?HBase?thrift2?C++編程示例 #include?"THBaseService.h" #include??//?PRIu64 #include? #include? #include? ? //?請(qǐng)注意客戶端使用的thrift的Transport和Protocol要和hbase?thrift2服務(wù)端保持一致, //?否則調(diào)用時(shí),可能總是報(bào)超時(shí),或其它錯(cuò)誤!!! // //?運(yùn)行之前,請(qǐng)通過HBase?shell創(chuàng)建好表:create?'test','cf1','cf2' //?或指定版本數(shù):create?'test',{NAME=>'cf1',VERSIONS=>2},{NAME=>'cf2',VERSIONS=>3} //?刪除表,按順序執(zhí)行以下兩條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_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";?????//?表名,確保運(yùn)行之前已創(chuàng)建好 ????????std::string?rowkey?=?"row1";????????//?行Key ????????std::string?family?=?"cf1";?????????//?例族名 ????????std::string?columnname?=?"f1";??????//?例名 ????????std::string?columnvalue?=?"value1";?//?例值 ? ????????//?插入?yún)?shù)設(shè)置 ????????std::vector?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);?//?插入,出錯(cuò)拋異常hbase::thrift2::TIOError ? ????????//?查詢參數(shù)設(shè)置 ????????hbase::thrift2::TGet?input; ????????input.__set_row(rowkey); ? ????????hbase::thrift2::TResult?result;?//?查詢結(jié)果存放在這里 ????????hbase_client->get(result,?tablename,?input);?//?查詢,出錯(cuò)拋異常hbase::thrift2::TIOError ? ????????//?顯示查詢結(jié)果 ????????for?(int?i=0;?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) ????{ ????????//?如果和服務(wù)端的Transport和Protocol不同,這里的錯(cuò)誤是“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客戶端報(bào)如下錯(cuò)誤,有可能是因?yàn)橐淮螌懭氲臄?shù)據(jù)太多,導(dǎo)致包過大:
Thrift:?Fri?Apr?22?17:30:41?2016?TSocket::write_partial()?send()?Connection?reset?by?peer
Thrift:?Fri?Apr?22?17:30:41?2016?TSocket::write_partial()?send()?Connection?reset?by?peer
總結(jié)
以上是生活随笔為你收集整理的HBase thrift C++编程的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 月读の自动读取 根据注释解析列名和字典
- 下一篇: 人生百味,浓缩到最后就是一个淡字