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]# 

你可能感兴趣的:(java,thrift,hbase)