因工作需要用python通過hbase的thrift服務讀取Hbase表數據,發現公司的測試環境還不支持,于是自己動手準備環境,在此我將在安裝步驟盡可能描述清楚,旨在給第一次動手安裝的朋友,此過程親測成功!
安裝過程如下:
1、首先確保hbase安裝測試成功,再者確認下hbase的thrift服務是否啟動,注意目前的Hbase(本文基于版本0.98.17)有兩套thrift接口thrift和thrift2,本文使用thrift,啟動命令:hbase thrift -p 9090 start,確保該端口沒有被占用,使用lsof -i:9090查看),本公司測試環境該端口被占用,如果被占用換一個沒有被占用的端口即可;
2、安裝thrift,去官網下載thrift:http://thrift.apache.org/download,本人使用 thrift-0.10.0.tar.gz ,下載好后編譯安裝,解壓后進入安裝目錄/home/hadoop/thrift-0.10.0,分別執行./configure make,make,sudo make install,注意這邊可能因各環境不同可能遇到問題,具體安裝所需環境請參考官網:http://thrift.apache.org/docs/install/centos,安裝之后可以用thrift -version命令測試是否安裝成功,安裝成功后會顯示安裝的版本;
3、確保1和2沒有問題,接下來需要生成python腳本需要導入的hbase相關模塊,首先去官網下載hbase源碼,注意雖然本公司用的hbase版本是0.98.17但是只要版本相差不大都可以使用,本人使用的是 hbase-0.98.24-src.tar.gz,下載解壓后找到thrift目錄:hbase-0.98.24/hbase-thrift/src/main/resources/org/apache/hadoop/hbase,該目錄下有兩個thrift服務,進入thrift后執行thrift --gen pyHbase.thrift,不出現問題會在該thrift目錄下生成目錄 gen-py,里面具體是hbase模塊,將該目錄名稱改為hbase,并拷貝進python模塊包:cp -r hbase /usr/lib/python2.7/site-packages/,至此python需要使用的hbase模塊已經準備好;
4、寫python腳本測試
#! /usr/bin/pythonimport syssys.path.append('/usr/lib/python2.7/site-packages/hbase') # 引入正確的hbase模塊路徑,測試過可刪除from thrift import Thriftfrom thrift.transport import TSocketfrom thrift.transport import TTransportfrom thrift.protocol import TBinaryProtocolfrom hbase import Hbasefrom hbase.ttypes import * transport = TSocket.TSocket('101.71.51.221', 9099)transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = Hbase.Client(protocol) transport.open() tableName = 'hb_vender'rowKey = '17_bcc5f02a876b010cbcd3fb2f82ab5b8e_43_111_57_437b9e2a-257c-4115-9570-bcd61741b3dc' result = client.getRow(tableName, rowKey, None)print resultfor r in result: print 'the row is ' , r.row print 'the values is ' , r.columns.get('a:venderName').value注意這邊可能出現:ImportError: No module named six,因為需要安裝six,如果已經安裝pip,使用pip install six,如果沒有安裝用root執行安裝:easy_install six,安裝成功后執行腳本測試成功!
新聞熱點
疑難解答