怎么在python3.7中利用thrift對hbase進(jìn)行操作-創(chuàng)新互聯(lián)

怎么在python3.7中利用thrift對hbase進(jìn)行操作?針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

成都創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都做網(wǎng)站、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的坡頭網(wǎng)站設(shè)計(jì)、移動媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

HBase是一個分布式的、面向列的開源數(shù)據(jù)庫,其是Apache的Hadoop項(xiàng)目的子項(xiàng)目。HBase不同于一般的關(guān)系數(shù)據(jù)庫,它是一個適合于非結(jié)構(gòu)化數(shù)據(jù)存儲的數(shù)據(jù)庫。另一個不同的是HBase基于列的而不是基于行的模式。其數(shù)據(jù)結(jié)構(gòu)類似與Redis的key-value模式。

python3.7 通過 thrift , rpc 接口操作 hbase ,指定依賴庫為: thrift 和 hbase-thrift 。 然而我們 在 python3.7 環(huán)境中發(fā)現(xiàn) hbase-thrift-0.20.4 無法被支持, hbase-thrift 官方僅推薦用于 python2.x 。 于是有了下邊的 patch 版本 和 patch 版本寫法的客戶端。

patch 版本下載,適用于 python 3.x : http://dl.cpp.la/Archive/hbase-thrift-0.20.4.patch.tgz

卸載 hbase-thrift-0.20.4 版本

# pip3 list | grep hbase-thrift
>> hbase-thrift    0.20.4
# pip3 uninstall hbase-thrift -y
>> Successfully uninstalled hbase-thrift-0.20.4

安裝 hbase-thrift-0.20.4.patch 版本(支持 python3.x )

wget http://dl.cpp.la/Archive/hbase-thrift-0.20.4.patch.tgz
tar -zxvf hbase-thrift-0.20.4.patch.tgz
cd hbase-thrift-0.20.4.patch
python3 setup.py install

檢測安裝是否成功

# pip3 list | grep hbase-thrift
>> hbase-thrift    0.20.4.patch
Python3.7 操作 hbase-thrift-patch 客戶端代碼示例
from thrift.transport import TSocket
from thrift.transport.TTransport import TBufferedTransport
from thrift.protocol import TBinaryProtocol
 
from hbase import Hbase
from hbase.ttypes import ColumnDescriptor
from hbase.ttypes import Mutation
 
class HBaseClient(object):
 
  def __init__(self):
    self.__ip = HBASE_URI.get("HOST")
    self.__port = HBASE_URI.get("PORT")
    self.__transport = self.createSocket
    protocol = TBinaryProtocol.TBinaryProtocol(self.__transport)
    self.__client = Hbase.Client(protocol)
    self.__transport.open()
 
  @property
  def createSocket(self):
    CS = TSocket.TSocket(self.__ip, self.__port)
    CS.setTimeout(60*1000)
    return TBufferedTransport(CS)
 
  def __del__(self):
    self.__transport.close()
 
  def get_tables(self):
    """
    get all table name
    :return: table name list
    """
    return self.__client.getTableNames()
 
  def create_table(self, table, *columns):
    """
    create table
    :param table: table name
    :param columns: columns name , variable parameter
    """
    func = lambda col: ColumnDescriptor(col)
    column_families = list(map(func, columns))
    self.__client.createTable(table, column_families)
 
  def delete_table(self, table):
    '''
    delete table in hbase
    :param table: tableName
    :return:
    '''
    if self.__client.isTableEnabled(table):
      self.__client.disableTable(table)
    self.__client.deleteTable(table)
 
  def put(self, table, row, columns):
    """
    add record
    :param table: table name
    :param row:
    :param columns:
    :return:
    """
    self.__client.mutateRow(table, row, [Mutation(column=k, value=v) for k, v in columns.items()])
 
  def delete(self, table, row, column):
    """
    delete record
    :param table: table name
    :param row:
    """
    self.__client.deleteAll(table, row, column)
 
  def scan(self, table, start_row="", columns=None):
    """
    get record
    :param table: table name
    :param start_row:
    :param columns:
    """
    scanner = self.__client.scannerOpen(table, start_row, columns)
    while True:
      r = self.__client.scannerGet(scanner)
      if not r:
        break
      yield dict([(k, v.value) for k, v in r[0].columns.items()])
if __name__ == "__main__":
  client = HBaseClient()
  for v in client.scan('studentd', columns={"cpp.la":"https://cpp.la"}):
    print(v)
by:cpp.la

ps:python3.7連接hbase

pip安裝thrift 和hbase 包

from thrift.transport import TSocket,TTransport
from thrift.protocol import TBinaryProtocol,TCompactProtocol
from hbase import Hbase
socket = TSocket.TSocket('10.1.21.35',port=9090)
socket.setTimeout(5000)
transport = TTransport.TFramedTransport(socket)
protocol = TBinaryProtocol.TBinaryProtocol(transport) //不使用這個協(xié)議
protocol = TCompactProtocol.TCompactProtocol(transport)
client = Hbase.Client(protocol)
socket.open()
table = client.getTableNames()
print(table)

關(guān)于怎么在python3.7中利用thrift對hbase進(jìn)行操作問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道了解更多相關(guān)知識。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

網(wǎng)站欄目:怎么在python3.7中利用thrift對hbase進(jìn)行操作-創(chuàng)新互聯(lián)
文章出自:http://bm7419.com/article42/ddjhhc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計(jì)公司、營銷型網(wǎng)站建設(shè)App設(shè)計(jì)、網(wǎng)站內(nèi)鏈定制開發(fā)、網(wǎng)站收錄

廣告

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

手機(jī)網(wǎng)站建設(shè)