//HBasethrift2C++緙栫▼紺轟緥 #include"THBaseService.h" #include//PRIu64 #include #include #include //璿鋒敞鎰忓鎴風浣跨敤鐨則hrift鐨凾ransport鍜孭rotocol瑕佸拰hbasethrift2鏈嶅姟絝繚鎸佷竴鑷達紝 //鍚﹀墶璋冪敤鏃訛紝鍙兘鎬繪槸鎶ヨ秴鏃訛紝鎴栧叾瀹冮敊璿紒錛侊紒 // //榪愯涔嬪墠錛岃閫氳繃HBaseshell鍒涘緩濂借〃錛歝reate'test','cf1','cf2' //鎴栨寚瀹氱増鏈暟錛歝reate'test',{NAME=>'cf1',VERSIONS=>2},{NAME=>'cf2',VERSIONS=>3} //鍒犻櫎琛紝鎸夐『搴忔墽琛屼互涓嬩袱鏉Baseshell鍛戒護錛?/span> //disable'test' //drop'test' STRING_ARG_DEFINE(hbase_ip,"192.168.0.1","hbasethriftip"); INTEGER_ARG_DEFINE(uint16_t,hbase_port,9090,1000,50000,"hbasethriftport"); intmain(intargc,char*argv[]) { std::stringerrmsg; if(!mooon::utils::parse_arguments(argc,argv,&errmsg)) { fprintf(stderr,"parametererror:%s\n",errmsg.c_str()); exit(1); } usingnamespaceapache; usingnamespaceapache::hadoop; std::stringhbase_ip=mooon::argument::hbase_ip->value(); uint16_thbase_port=mooon::argument::hbase_port->value(); mooon::net::CThriftClientHelperhbase_client(hbase_ip,hbase_port); try { hbase_client.connect();//榪炴帴hbasethrift2server fprintf(stdout,"connect%s:%dok\n",hbase_ip.c_str(),hbase_port); std::stringtablename="test";//琛ㄥ悕錛岀‘淇濊繍琛屼箣鍓嶅凡鍒涘緩濂?/span> std::stringrowkey="row1";//琛孠ey std::stringfamily="cf1";//渚嬫棌鍚?/span> std::stringcolumnname="f1";//渚嬪悕 std::stringcolumnvalue="value1";//渚嬪€?/span> //鎻掑靉鍙傛暟璁劇疆 std::vectorcolumns_value(1); columns_value[0].__set_family(family); columns_value[0].__set_qualifier(columnname); columns_value[0].__set_value(columnvalue); hbase::thrift2::TPutput; put.__set_row(rowkey); put.__set_columnValues(columns_value); hbase_client->put(tablename,put);//鎻掑靉錛屽嚭閿欐姏寮傚父hbase::thrift2::TIOError //鏌ヨ鍙傛暟璁劇疆 hbase::thrift2::TGetinput; input.__set_row(rowkey); hbase::thrift2::TResultresult;//鏌ヨ緇撴灉瀛樻斁鍦ㄨ繖閲?/span> hbase_client->get(result,tablename,input);//鏌ヨ錛屽嚭閿欐姏寮傚父hbase::thrift2::TIOError //鏄劇ず鏌ヨ緇撴灉 for(inti=0;i(result.columnValues.size());++i) { consthbase::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) { //濡傛灉鍜屾湇鍔$鐨凾ransport鍜孭rotocol涓嶅悓錛岃繖閲岀殑閿欒鏄€淓AGAIN(timedout)鈥?/span> 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()); } return0; } |