C++通过thrfit2高效读写HBase

hbase没有提供直接供c++的访问方法,而是通过thrift2服务提供。如需要通过c++访问hbase就需要hbase开启thrift server。

开启方法:

$HBASE_HOME/bin/hbase-daemon.sh start thrift2 -t 60000

thrift server默认开启9090端口。hbase对客户端读取有超时策略,默认1分钟,启动时可通过-t参数指定超时时长,超时后hbase客户端会关闭连接,这时如果用原连接读取数据会产生Broken pipe的异常。其他参数可通过查看thrift server源码了解 。

创建客户端:(这里我创建了2个,一个用于读,一个用于写)。默认的客户端不是线程安全的,如需要在线程间共享客户端连接,可使用线程安全的THBaseServiceConcurrentClient。详细见生成的hbase的thrift客户端代码。

bool HBaseDriver::getClient(string ip,int port){
    try {
				this->thriftIp = ip;
				this->thriftPort = port;
				socket_ = SHARED_PTR_NAMESPACE::shared_ptr(new TSocket(ip, port));
				#ifdef FRAMED
						transport_ = SHARED_PTR_NAMESPACE::shared_ptr(new TFramedTransport(socket_));
				#else
						transport_ = SHARED_PTR_NAMESPACE::shared_ptr(new TBufferedTransport(socket_));
				#endif
				protocol_ = SHARED_PTR_NAMESPACE::shared_ptr(new TBinaryProtocol(transport_));
				transport_->open();
				client_ = new THBaseServiceClient(protocol_);
				put_socket_ = SHARED_PTR_NAMESPACE::shared_ptr(new TSocket(ip, port));
				put_transport_ = SHARED_PTR_NAMESPACE::shared_ptr(new TBufferedTransport(put_socket_));
				put_protocol_ = SHARED_PTR_NAMESPACE::shared_ptr(new TBinaryProtocol(put_transport_));
				put_transport_->open();
				put_client_ = new THBaseServiceClient(put_protocol_);
    } catch(TException tex) {
				LOG(string(tex.what()));
				return false;
    }
    return true;
}

批量PUT数据:

void HBaseDriver::putPerson(string& tableName, string& rowKey, unordered_map& qualifierVal, string& family){
				count +=1;
				TPut put;
				std::vector cvs;
				put.__set_row(rowKey);
				if(BOOL_TEST){
				        start = clock();
				        BOOL_TEST = false;
				}
				unordered_map::iterator it;
				try{
				        it = qualifierVal.begin();
				        while(it != qualifierVal.end()) {
				                TColumnValue cv;
				                cv.__set_family(family);
				                cv.__set_qualifier(it->first);
				                cv.__set_value(it->second);
				                /*if(it->first == "feature"){ //if the key is 'feature'
				                        assert(it->second.length() == BASE64_FEATURE_LEN);
				                }*/
				                cvs.insert(cvs.end(), cv);
				                put.__set_durability(TDurability::SKIP_WAL);//skit write ahead log for speed up
				                put.__set_columnValues(cvs);
				                personPuts.insert(personPuts.end(), put);
				                it++;
				        }
				        if(this->count % this->PUT_BATCH_SIZE == 0){
				                start = clock();
				                put_client_->putMultiple(tableName, personPuts);
				                finish = clock();
				                duration = (finish - start) / CLOCKS_PER_SEC;
				                printf( "===================================== %f seconds cost for hbase put.\n", duration );
				                BOOL_TEST = true;
				                //long end = CURRENT_TIME();
				                //LOG(string("Time Cost :"+to_string(end-start)+" ms"));
				                personPuts.clear();
				        }
				        cvs.clear();
				}catch(TException tex) {
								LOG(string(tex.what()));
				}
}

 

 

 

你可能感兴趣的:(C/C++/VC,hbase,thrift)