`
haoningabc
  • 浏览: 1489737 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

thirft连接hbase的例子

阅读更多
hbase支持的thrift有两种方式
thrift和
thrift2
官方似乎推荐thrift2
一下是两种thrift的java客户端的连接方式
然后是一个cpp以thrift2的方式连接hbase的操作
如果是使用thrift1
hbase-daemon.sh start thrift
如果是使用thrift2
hbase-daemon.sh start thrift2
官方例子在
/data/hadoop/hbase/hbase-0.94.17/src/examples/thrift
thrift文件在
./src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift
thrift2 的例子在
/data/hadoop/hbase/hbase-0.94.17/src/examples/thrift2
thrift2文件在
./src/main/resources/org/apache/hadoop/hbase/thrift2/hbase.thrift
(只有java和python的)


先生成文件
thrift -r --gen java Hbase.thrift
客户端代码
HaoClient.java
package org.apache.hadoop.hbase.thrift.generated;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;

public class HaoClient {
	public static String byteBufferToString(ByteBuffer buffer) {
		CharBuffer charBuffer = null;
		try {
			Charset charset = Charset.forName("UTF-8");
			CharsetDecoder decoder = charset.newDecoder();
			charBuffer = decoder.decode(buffer);
			buffer.flip();
			return charBuffer.toString();
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}

	public static ByteBuffer getByteBuffer(String str) {
		return ByteBuffer.wrap(str.getBytes());
	}
	private void start() {
		try {
			TTransport socket = new TSocket("10.230.13.100", 9090);// 我的虚拟机,线上用的thrift2
			TProtocol protocol = new TBinaryProtocol(socket, true, true);// 注意这里
			Hbase.Client client = new Hbase.Client(protocol);
			socket.open();
			System.out.println("open");
			try {
				System.out.println("scanning tables...");
				for (ByteBuffer name : client.getTableNames()) {
					System.out.println("find:" + byteBufferToString(name));
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			socket.close();
			System.out.println("close");
		} catch (TTransportException e) {
			e.printStackTrace();
		} catch (TException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		HaoClient c = new HaoClient();
		c.start();

	}
}


如果是thrift2的话
hbase-daemon.sh start thrift2
thrift -r --gen java hbase.thrift
HaoClient2.java
package org.apache.hadoop.hbase.thrift2.generated;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
public class HaoClient2 {
	public static String byteBufferToString(ByteBuffer buffer) {
		CharBuffer charBuffer = null;
		try {
			Charset charset = Charset.forName("UTF-8");
			CharsetDecoder decoder = charset.newDecoder();
			charBuffer = decoder.decode(buffer);
			buffer.flip();
			return charBuffer.toString();
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}
	public static ByteBuffer getByteBuffer(String str) {
		return ByteBuffer.wrap(str.getBytes());
	}
    private void start() {
        try {
            TTransport socket = new TSocket("10.230.13.100", 9090);
            // TTransport socket = new TSocket("10.77.112.191",9090);
            //TTransport transport = new TFramedTransport(socket);
            // TProtocol protocol = new TCompactProtocol(socket);
            TProtocol protocol = new TBinaryProtocol(socket, true, true);//注意这里
            // THBaseService.Client client = new THBaseService.Client(protocol);
            THBaseService.Iface client = new THBaseService.Client(protocol);
            socket.open();
            System.out.println("open");
            ByteBuffer table = ByteBuffer.wrap("mytable".getBytes());

            /* TPut put = new TPut();
            put.setRow("first".getBytes());

            TColumnValue columnValue = new TColumnValue();
            columnValue.setFamily("cf".getBytes());
            columnValue.setQualifier("fromjava".getBytes());
            columnValue.setValue("java is ok".getBytes());
            List<TColumnValue> columnValues = new ArrayList<TColumnValue>();
            columnValues.add(columnValue);
            put.setColumnValues(columnValues);

            client.put(table, put);*/
            // byte[] aa=Bytes.toBytesBinary("\\x00\\x00\\x15o");
            TGet get = new TGet();
            get.setRow("first".getBytes());
            TResult result = client.get(table, get);
            System.out.println("row = " + new String(result.getRow()));
            for (TColumnValue resultColumnValue : result.getColumnValues()) {
              System.out.println("family = " + new String(resultColumnValue.getFamily()));
              System.out.println("qualifier = " + new String(resultColumnValue.getQualifier()));
              System.out.println("value = " + new String((resultColumnValue.getValue())));
              System.out.println("timestamp = " + resultColumnValue.getTimestamp());
              System.out.println("");
            }
            socket.close();
            System.out.println("close");
        } catch (TTransportException e) {
            e.printStackTrace();
        } catch (TException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        HaoClient2 c = new HaoClient2();
        c.start();
    }
}



注意需要,thrift1的Hbase.thirft
thrift2的hbase.thirft
还需要生成的代码,和hbase所有的jar

hbase的thrift1 和thrift2生成的类不一样
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
如果是c++连接的话
官方只给了个java和py的例子,网上有很多thrift1的接口的,但是没找到thirft2 的
自己写了个客户端如下
c++代码如下
thrift -r --gen cpp hbase.thrift
[root@mytest HbaseThrift]# cat HbaseClient.cpp 
#include "THBaseService.h"
#include <config.h>
#include <vector>
#include <transport/TSocket.h>
#include <transport/TBufferTransports.h>
#include <protocol/TBinaryProtocol.h>

using namespace std;
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace apache::hadoop::hbase::thrift2;

using boost::shared_ptr;

int main(int argc, char **argv) {
    boost::shared_ptr<TSocket> socket(new TSocket("10.230.13.100", 9090));
    boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
    boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));

    transport->open();
    printf("open\n");
    THBaseServiceClient client(protocol);
    TResult tresult;
    TGet get;
    //const std::string table="mytable";
    const std::string table("mytable");
    const std::string thisrow="first";
    get.__set_row(thisrow);
    //get.__set_row("first");
    client.get(tresult,table,get);
    vector<TColumnValue> list=tresult.columnValues;
    std::vector<TColumnValue>::const_iterator iter;
    for(iter=list.begin();iter!=list.end();iter++) {
      printf("list size: %d\n",list.size());
      printf("get : %s, %s,%s\n",(*iter).family.c_str(),(*iter).qualifier.c_str(),(*iter).value.c_str());//,(*iter).timestamp
    }    

    transport->close();
    printf("close\n");
    return 0;
}
[root@mytest HbaseThrift]# 

Makefile如下
[root@mytest HbaseThrift]# cat Makefile 
BOOST_DIR = /usr/include/boost
THRIFT_DIR = /usr/local/thrift/include/thrift
#LIB_DIR = /usr/local/lib
LIB_DIR = /usr/local/thrift/lib/
GEN_SRC = ./gen-cpp/hbase_types.cpp ./gen-cpp/hbase_constants.cpp ./gen-cpp/THBaseService.cpp
default: client
client: HbaseClient.cpp
        g++ -g -o HbaseClient -lm -pthread -lz -lrt -lssl -I${THRIFT_DIR} -I${BOOST_DIR}  -I./gen-cpp -L${LIB_DIR} -lthrift HbaseClient.cpp ${GEN_SRC}
clean:
        $(RM) -r HbaseClient


[root@mytest HbaseThrift]# 

代码目录
[root@mytest HbaseThrift]# tree
.
├── gen-cpp
│   ├── hbase_constants.cpp
│   ├── hbase_constants.h
│   ├── hbase_types.cpp
│   ├── hbase_types.h
│   ├── THBaseService.cpp
│   ├── THBaseService.h
│   └── THBaseService_server.skeleton.cpp
├── HbaseClient.cpp
├── hbase.thrift
└── Makefile

1 directory, 10 files
[root@mytest HbaseThrift]#
[root@mytest HbaseThrift]# ls
gen-cpp  HbaseClient  HbaseClient.cpp  hbase.thrift  Makefile
hbase需要先建立:
hbase shell
create 'mytable','cf'

[root@mytest HbaseThrift]# ./HbaseClient
open
list size: 4
get : cf, cf,value1
list size: 4
get : cf, foo,7
list size: 4
get : cf, fromjava,java is ok
list size: 4
get : cf, message,hello haonings hbase
close
[root@mytest HbaseThrift]#
注意设置
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib:/usr/local/thrift/lib
找thrift的so

插入的例子
[root@mytest HbaseThrift]# cat HbaseClient.cpp
#include "THBaseService.h"
#include <config.h>
#include <vector>
#include <transport/TSocket.h>
#include <transport/TBufferTransports.h>
#include <protocol/TBinaryProtocol.h>

using namespace std;
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace apache::hadoop::hbase::thrift2;

using boost::shared_ptr;

int main(int argc, char **argv) {
//    boost::shared_ptr<TSocket> socket(new TSocket("10.217.12.179", 9090));
    boost::shared_ptr<TSocket> socket(new TSocket("10.77.112.191", 9090));
    boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
    boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));

    transport->open();
    TPut put;
    THBaseServiceClient client(protocol);
    const std::string table="mytable";
    const std::string thisrow="second";
    put.__set_row(thisrow);
    TColumnValue columnValue;
    const std::string thisfamily="cf";
    columnValue.__set_family(thisfamily);
    const std::string thisqualifier="fromcpp";
    columnValue.__set_qualifier(thisqualifier);
    const std::string thisvalue="vppisok";
    columnValue.__set_value(thisvalue);
    columnValue.__set_timestamp(1395036661654);
    vector<TColumnValue> columnValues;
    columnValues.push_back(columnValue);
    put.__set_columnValues(columnValues);
//    std::cout<<"Texception"<<std::endl;
   /* try{

    }catch(TException &e){

    }*/
    client.put(table,put);
    /*printf("open\n");
    THBaseServiceClient client(protocol);
    TResult tresult;
    TGet get;
    //const std::string table="mytable";
    const std::string table("mytable");
    const std::string thisrow="first";
    get.__set_row(thisrow);
    //get.__set_row("first");
    client.get(tresult,table,get);
    vector<TColumnValue> list=tresult.columnValues;
    std::vector<TColumnValue>::const_iterator iter;
    for(iter=list.begin();iter!=list.end();iter++) {
      printf("list size: %d\n",list.size());
      printf("get : %s, %s,%s\n",(*iter).family.c_str(),(*iter).qualifier.c_str(),(*iter).value.c_str());//,(*iter).timestamp
    }    */

    transport->close();
    printf("close\n");
    return 0;
}
[root@mytest HbaseThrift]# 
分享到:
评论

相关推荐

    Python连接Hbase

    Python连接HBase的过程主要分为以下几个步骤: 1. **安装依赖**:在Python环境中,你需要安装`happybase`库,它是一个基于Thrift的Python库,使得与HBase的交互变得更加简单。你可以通过`pip`来安装: ``` pip ...

    python连接hbase的lib(版本0.98.8)

    在Python连接HBase的情况下,Thrift充当了Python与HBase之间的桥梁,允许Python程序通过Thrift调用HBase的API。 对于Python连接HBase的lib,通常我们需要一个名为`happybase`的Python库。这个库是一个易于使用的...

    虚拟机Centos安装thrift

    一旦thrift安装完成,开发者就可以开始编写服务定义和客户端代码,连接并操作HBase。 需要注意的是,操作过程中可能由于OCR扫描识别错误或者漏识别的情况,导致部分文字不准确。在实际操作中,应当根据提示和命令...

    ehbase:通过 Erlang Thrift 库的 Erlang HBase 驱动程序

    在表面之下, ehbase使用 Erlang Thrift 库通过其 Thrift 网关连接到 HBase,该网关包含在标准 HBase 0.9x 版本中。 另外, ehbase使用进行连接的池封装。 但是我使用了,它支持更多功能,并且出于某种原因暂时不向...

    HBase权威指南 中文版

    - **HBase MapReduce示例**:提供了一些使用HBase进行MapReduce编程的例子。 - **跨表访问**:在一个MapReduce作业中访问多个HBase表的方法。 #### 八、HBase安全 - **客户端访问控制**:确保只有授权用户可以访问...

    nosql 入门教程

    第一印象——两个简单的例子 17 2.1.1 简单的位置偏好数据集 17 2.1.2 存储汽车品牌和型号数据 22 2.2 使用多种语言 30 2.2.1 MongoDB驱动 30 2.2.2 初识Thrift 33 2.3 小结 34 第3章 NoSQL接口与交互 36 ...

    Hadoop权威指南(中文版)2015上传.rar

    2.4.2 Avro,REST,以及Thrift 2.5 示例 2.5.1 模式 2.5.2 加载数据 2.5.3 Web查询 2.6 HBase和RDBMS的比较 2.6.1 成功的服务 2.6.2 HBase 2.6.3 实例:HBase在Streamy.com的使用 2.7 Praxis 2.7.1 版本 2.7.2 HDFS ...

    Hadoop权威指南 第二版(中文版)

     2.4.2 Avro,REST,以及Thrift  2.5 示例  2.5.1 模式  2.5.2 加载数据  2.5.3 Web查询  2.6 HBase和RDBMS的比较  2.6.1 成功的服务  2.6.2 HBase  2.6.3 实例:HBase在Streamy.com的使用  2.7 Praxis  ...

    Hadoop 权威指南(中文前三章)

    - **数据传输**: 介绍了如何使用Hadoop管道来连接不同的MapReduce任务,实现数据的高效传输。 #### 三、Hadoop分布式文件系统(HDFS) **3.1 HDFS的设计** - **设计原则**: HDFS的设计着重于提供高吞吐量的数据访问...

Global site tag (gtag.js) - Google Analytics