本文為您介紹HBase Thrift Server,以及通過示例介紹如何訪問EMR HBase集群。

背景信息

Thrift是一個可伸縮,跨語言服務RPC框架,集成了強大的軟件堆棧及代碼生成引擎,使得各種語言做到無障礙,高效通信,目前支持C++、Java、Python、PHP、Ruby、Erlang、Perl、Haskell、C#、Go、Cocoa、JavaScript、Node.js和Smalltalk等開發語言。

HBase為了多語言的開發支持,本身也實現了Thrift Server,該服務基于Apache Thrift開發。

EMR HBase Thrift Server

EMR HBase默認會在集群主節點上啟動Thrift Server服務,服務端口為9091。

負載均衡

EMR HBase集群在高可用模式下,會在3個主節點啟動Thrift Server服務,您可以根據需要實現自己的負載均衡策略,以將請求相對均衡的分配到3個Thrift Server服務上。

示例

以下以Python為例,說明如何使用Python編程來訪問EMR HBase集群。

  1. 登錄到EMR HBase集群節點,執行以下命令。
    sudo yum install python-pip
    sudo pip install hbase-thrift
  2. 新建hbase_thrift_test.py文件。
    vim hbase_thrift_test.py
    hbase_thrift_test.py文件內容如下。
    #! /usr/bin/env python
    #coding=utf-8
    from thrift import Thrift
    from thrift.transport import TSocket,TTransport
    from thrift.protocol import TBinaryProtocol
    from hbase import Hbase
    
    socket = TSocket.TSocket('master-1-1', 9091)
    socket.setTimeout(60000)
    transport = TTransport.TBufferedTransport(socket)
    transport.open()
    protocol = TBinaryProtocol.TBinaryProtocol(transport)
    client = Hbase.Client(protocol)
    result = client.getRow("test_table","test_rowkey")
    for r in result:
        print 'The rowkey is ', r.row
        print 'The value is ', r.columns.get('cf:q').value
    socket.close()
  3. 執行Python腳本。
    python hbase_thrift_test.py
    輸出以下信息。
    The rowkey is  rowkey
    The value is  aaaaaa