日期:2014-05-16  浏览次数:20399 次

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);//注意这里