Thrift代理Hbase

使用HBase的2種方式:php

1.直接使用HBase客戶端API,這樣就限制了只能使用java語言html

2.使用一些可以將請求轉換成API的代理,這些代理將原始Java API包裝成其餘協議,這樣客戶端可使用API提供的任意外部語言來編寫程序。外部API實現了專門基於java的服務,而這種服務可以在內部使用由HTable客戶端提供的API。java

HBase自己對代理模式的支持也很普遍,好比支持的類型有:REST、Thrift、Avro等python

關於代理的模式,能夠看一張網上的架構圖:c++

這裏主要介紹一下Thrift做爲HBase的代理對外提供服務,主要是Thrift在性能上的優點以及對各類主流語言的支持shell

1.安裝HBase和Hadoopapache

Hadoop版本:2.5.1
Hbase版本:1.2.2windows

參考以前的文章:
Hadoop的版本選擇和單機模式:http://codingo.xyz/index.php/2016/08/16/hadoop-stand-alone/
Hadoop的僞分佈式模式:http://codingo.xyz/index.php/2016/08/16/hadoop_false_distribute/
Hbase版本選擇和單機模式入門:http://codingo.xyz/index.php/2016/08/17/hbase_standalone/
Hbase的僞分佈式模式:http://codingo.xyz/index.php/2016/08/16/hadoop_false_distribute/ruby

2.安裝Thrift
Thrift版本:0.9.3
下載地址:https://thrift.apache.org/download
windows平臺:直接使用thrift-0.9.3.exe
其餘平臺安裝:https://thrift.apache.org/docs/install/架構

3.編譯模式文件
Hbase提供了Thrift須要的模式文件,存放在Hbase的源碼中,須要下載的是:hbase-1.2.2-src.tar.gz
路徑:
$HBASE_HOOME/src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift 和
$HBASE_HOOME/src/main/resources/org/apache/hadoop/hbase/thrift2/hbase.thrift
提供了2套Thrift文件,它們並不兼容;根據官方文檔,thrift1極可能被拋棄,全部下面的例子中使用thrift2

windows平臺編碼模式文件:

thrift-0.9.3.exe --gen java hbase.thrift

其實java能夠根據不一樣語言進行選擇,好比c++、perl、php、python、ruby等
生成了以下結構代碼

拷貝到eclipse的開發環境中,這裏沒有直接使用官方提供的hbase-thrift-1.2.2.jar,jar文件中其實就是咱們上面的類文件。

4.獲取Thrift提供的語言支持庫
以上自動生成的類文件依賴於Thrift提供的支持庫,thrift-0.9.3/lib目錄下提供了各類語言的支持庫
選擇java支持庫,將其餘的代碼一樣拷貝到eclipse的開發環境中,這裏也沒有使用官方提供的:libthrift-0.9.3.jar

5.啓動服務
以上提供了Hbase和Hadoop的僞分佈式模式的安裝和啓動
Hbase提供了對ThriftServer啓動的支持

非守護模式啓動和中止Thrift服務:

hbase thrift2 start
hbase thrift2 stop

後臺進程啓動和中止Thrift服務:

hbase-daemon.sh start thrift2
hbase-daemon.sh stop thrift2

啓動日誌:

[root@bogon ~]# hbase thrift2 start
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/root/hbase-1.2.2/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/root/hadoop-2.5.1/share/hadoop/common/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
2016-09-20 08:57:05,211 INFO  [main] impl.MetricsConfig: loaded properties from hadoop-metrics2-hbase.properties
2016-09-20 08:57:05,367 INFO  [main] impl.MetricsSystemImpl: Scheduled snapshot period at 10 second(s).
2016-09-20 08:57:05,367 INFO  [main] impl.MetricsSystemImpl: HBase metrics system started
2016-09-20 08:57:06,123 INFO  [main] mortbay.log: Logging to org.slf4j.impl.Log4jLoggerAdapter(org.mortbay.log) via org.mortbay.log.Slf4jLog
2016-09-20 08:57:06,129 INFO  [main] http.HttpRequestLog: Http request log for http.requests.thrift is not defined
2016-09-20 08:57:06,148 INFO  [main] http.HttpServer: Added global filter 'safety' (class=org.apache.hadoop.hbase.http.HttpServer$QuotingInputFilter)
2016-09-20 08:57:06,148 INFO  [main] http.HttpServer: Added global filter 'clickjackingprevention' (class=org.apache.hadoop.hbase.http.ClickjackingPreventionFilter)
2016-09-20 08:57:06,151 INFO  [main] http.HttpServer: Added filter static_user_filter (class=org.apache.hadoop.hbase.http.lib.StaticUserWebFilter$StaticUserFilter) to context thrift
2016-09-20 08:57:06,151 INFO  [main] http.HttpServer: Added filter static_user_filter (class=org.apache.hadoop.hbase.http.lib.StaticUserWebFilter$StaticUserFilter) to context static
2016-09-20 08:57:06,152 INFO  [main] http.HttpServer: Added filter static_user_filter (class=org.apache.hadoop.hbase.http.lib.StaticUserWebFilter$StaticUserFilter) to context logs
2016-09-20 08:57:06,173 INFO  [main] http.HttpServer: Jetty bound to port 9095
2016-09-20 08:57:06,174 INFO  [main] mortbay.log: jetty-6.1.26
2016-09-20 08:57:06,680 INFO  [main] mortbay.log: Started SelectChannelConnector@0.0.0.0:9095
2016-09-20 08:57:06,695 INFO  [main] thrift2.ThriftServer: starting HBase ThreadPool Thrift server on 0.0.0.0/0.0.0.0:9090

默認對外提供的端口:9090

6.測試
關於eclipse遠程鏈接Hbase:https://my.oschina.net/OutOfMemory/blog/746860
開發環境中已經有了編譯模式文件生成的類文件以及Thrift提供的語言支持庫

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.hbase.thrift2.generated.TColumnValue;
import org.apache.hadoop.hbase.thrift2.generated.THBaseService;
import org.apache.hadoop.hbase.thrift2.generated.TPut;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

public class ThriftExample {

    public static void main(String[] args) throws Exception {
        TTransport transport = new TSocket("192.168.111.129", 9090, 20000);
        TProtocol protocol = new TBinaryProtocol(transport, true, true);
        THBaseService.Client client = new THBaseService.Client(protocol);
        transport.open();

        ByteBuffer table = ByteBuffer.wrap("testTable".getBytes());

        TPut put = new TPut();
        put.setRow("row1".getBytes());

        TColumnValue columnValue = new TColumnValue();
        columnValue.setFamily("family1".getBytes());
        columnValue.setQualifier("qualifier1".getBytes());
        columnValue.setValue("value1".getBytes());
        List<TColumnValue> columnValues = new ArrayList<TColumnValue>();
        columnValues.add(columnValue);
        put.setColumnValues(columnValues);

        client.put(table, put);
        transport.close();
    }
}

經過hbase shell查看:

hbase(main):003:0> scan 'testtable'
ROW                         COLUMN+CELL                                                
 rthrift                     column=colfam1:qualifier1, timestamp=1474376284145, value=value1

以上是用java語言進行的測試,以上的步驟只要在:編譯模式文件和Thrift提供的語言支持庫 進行稍微的修改也適用於其餘語言

我的博客:codingo.xyz

相關文章
相關標籤/搜索