python利用thrift服務讀取hbase數據的方法
因工作需要用python通過hbase的thrift服務讀取Hbase表數據,發(fā)現公司的測試環(huán)境還不支持,于是自己動手準備環(huán)境,在此我將在安裝步驟盡可能描述清楚,旨在給第一次動手安裝的朋友,此過程親測成功!
安裝過程如下:
1、首先確保hbase安裝測試成功,再者確認下hbase的thrift服務是否啟動,注意目前的Hbase(本文基于版本0.98.17)有兩套thrift接口thrift和thrift2,本文使用thrift,啟動命令:hbase thrift -p 9090 start,確保該端口沒有被占用,使用lsof -i:9090查看),本公司測試環(huán)境該端口被占用,如果被占用換一個沒有被占用的端口即可;
2、安裝thrift,去官網下載thrift:http://thrift.apache.org/download,本人使用 thrift-0.10.0.tar.gz ,下載好后編譯安裝,解壓后進入安裝目錄/home/hadoop/thrift-0.10.0,分別執(zhí)行./configure make,make,sudo make install,注意這邊可能因各環(huán)境不同可能遇到問題,具體安裝所需環(huán)境請參考官網: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后執(zhí)行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/python import sys sys.path.append('/usr/lib/python2.7/site-packages/hbase') # 引入正確的hbase模塊路徑,測試過可刪除 from thrift import Thrift from thrift.transport import TSocket from thrift.transport import TTransport from thrift.protocol import TBinaryProtocol from hbase import Hbase from 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 result for 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執(zhí)行安裝:easy_install six,安裝成功后執(zhí)行腳本測試成功!
以上這篇python利用thrift服務讀取hbase數據的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Pytorch中torch.nn.Softmax的dim參數用法說明
這篇文章主要介紹了Pytorch中torch.nn.Softmax的dim參數用法說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06