python利用thrift服務(wù)讀取hbase數(shù)據(jù)的方法-創(chuàng)新互聯(lián)

因工作需要用python通過hbase的thrift服務(wù)讀取Hbase表數(shù)據(jù),發(fā)現(xiàn)公司的測試環(huán)境還不支持,于是自己動手準(zhǔn)備環(huán)境,在此我將在安裝步驟盡可能描述清楚,旨在給第一次動手安裝的朋友,此過程親測成功!

創(chuàng)新互聯(lián)公司從2013年創(chuàng)立,先為天寧等服務(wù)建站,天寧等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為天寧企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

安裝過程如下:

1、首先確保hbase安裝測試成功,再者確認(rèn)下hbase的thrift服務(wù)是否啟動,注意目前的Hbase(本文基于版本0.98.17)有兩套thrift接口thrift和thrift2,本文使用thrift,啟動命令:hbase thrift -p 9090 start,確保該端口沒有被占用,使用lsof -i:9090查看),本公司測試環(huán)境該端口被占用,如果被占用換一個(gè)沒有被占用的端口即可;

2、安裝thrift,去官網(wǎng)下載thrift:http://thrift.apache.org/download,本人使用 thrift-0.10.0.tar.gz ,下載好后編譯安裝,解壓后進(jìn)入安裝目錄/home/hadoop/thrift-0.10.0,分別執(zhí)行./configure make,make,sudo make install,注意這邊可能因各環(huán)境不同可能遇到問題,具體安裝所需環(huán)境請參考官網(wǎng):http://thrift.apache.org/docs/install/centos,安裝之后可以用thrift -version命令測試是否安裝成功,安裝成功后會顯示安裝的版本;

3、確保1和2沒有問題,接下來需要生成python腳本需要導(dǎo)入的hbase相關(guān)模塊,首先去官網(wǎng)下載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,該目錄下有兩個(gè)thrift服務(wù),進(jìn)入thrift后執(zhí)行thrift --gen pyHbase.thrift,不出現(xiàn)問題會在該thrift目錄下生成目錄 gen-py,里面具體是hbase模塊,將該目錄名稱改為hbase,并拷貝進(jìn)python模塊包:cp -r hbase /usr/lib/python2.7/site-packages/,至此python需要使用的hbase模塊已經(jīng)準(zhǔn)備好;

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

網(wǎng)站題目:python利用thrift服務(wù)讀取hbase數(shù)據(jù)的方法-創(chuàng)新互聯(lián)
路徑分享:http://muchs.cn/article40/cdggeo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、移動網(wǎng)站建設(shè)App設(shè)計(jì)、自適應(yīng)網(wǎng)站定制網(wǎng)站、商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)