python3.7通過(guò)thrift操作hbase的示例代碼
HBase是一個(gè)分布式的、面向列的開(kāi)源數(shù)據(jù)庫(kù),其是Apache的Hadoop項(xiàng)目的子項(xiàng)目。HBase不同于一般的關(guān)系數(shù)據(jù)庫(kù),它是一個(gè)適合于非結(jié)構(gòu)化數(shù)據(jù)存儲(chǔ)的數(shù)據(jù)庫(kù)。另一個(gè)不同的是HBase基于列的而不是基于行的模式。其數(shù)據(jù)結(jié)構(gòu)類似與Redis的key-value模式。
python3.7 通過(guò) thrift , rpc 接口操作 hbase ,指定依賴庫(kù)為: thrift 和 hbase-thrift 。 然而我們 在 python3.7 環(huán)境中發(fā)現(xiàn) hbase-thrift-0.20.4 無(wú)法被支持, hbase-thrift 官方僅推薦用于 python2.x 。 于是有了下邊的 patch 版本 和 patch 版本寫(xiě)法的客戶端。
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
檢測(cè)安裝是否成功
# 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) //不使用這個(gè)協(xié)議 protocol = TCompactProtocol.TCompactProtocol(transport) client = Hbase.Client(protocol) socket.open() table = client.getTableNames() print(table)
總結(jié)
以上所述是小編給大家介紹的python3.7通過(guò)thrift操作hbase的示例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
- python thrift搭建服務(wù)端和客戶端測(cè)試程序
- python thrift 實(shí)現(xiàn) 單端口多服務(wù)的過(guò)程
- python使用thrift教程的方法示例
- python利用thrift服務(wù)讀取hbase數(shù)據(jù)的方法
- python 如何用urllib與服務(wù)端交互(發(fā)送和接收數(shù)據(jù))
- Python連接Java Socket服務(wù)端的實(shí)現(xiàn)方法
- python 實(shí)現(xiàn)客戶端與服務(wù)端的通信
- python網(wǎng)絡(luò)編程socket實(shí)現(xiàn)服務(wù)端、客戶端操作詳解
- Python Websocket服務(wù)端通信的使用示例
- python實(shí)現(xiàn)Thrift服務(wù)端的方法
相關(guān)文章
為了順利買到演唱會(huì)的票用Python制作了自動(dòng)搶票的腳本
大麥網(wǎng),是中國(guó)綜合類現(xiàn)場(chǎng)娛樂(lè)票務(wù)營(yíng)銷平臺(tái),業(yè)務(wù)覆蓋演唱會(huì)、 話劇、音樂(lè)劇、體育賽事等領(lǐng)域。但是因?yàn)槠睌?shù)有限,還有黃牛們不能丟了飯碗,所以導(dǎo)致了,很多人都搶不到票,那么,今天帶大家用Python來(lái)制作一個(gè)自動(dòng)搶票的腳本小程序2021-10-104種方法教你利用Python發(fā)現(xiàn)數(shù)據(jù)的規(guī)律
發(fā)現(xiàn)數(shù)據(jù)的規(guī)律是數(shù)據(jù)分析和數(shù)據(jù)科學(xué)中非常重要的一個(gè)步驟。這篇文章主要給大家整理了4個(gè)可以發(fā)現(xiàn)數(shù)據(jù)規(guī)律的方法,希望對(duì)大家有所幫助2023-03-03用Python復(fù)現(xiàn)二戰(zhàn)德軍enigma密碼機(jī)
大家好,本篇文章主要講的是用Python復(fù)現(xiàn)二戰(zhàn)德軍enigma密碼機(jī),感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2022-01-01PyTorch線性回歸和邏輯回歸實(shí)戰(zhàn)示例
這篇文章主要介紹了PyTorch線性回歸和邏輯回歸實(shí)戰(zhàn)示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05Python基于SciPy庫(kù)實(shí)現(xiàn)統(tǒng)計(jì)分析與建模
SciPy是一個(gè)強(qiáng)大的Python庫(kù),提供了豐富的科學(xué)計(jì)算和數(shù)據(jù)分析工具,本文我們將探討如何使用Python和SciPy庫(kù)進(jìn)行統(tǒng)計(jì)分析和建模,感興趣的可以學(xué)習(xí)一下2023-06-06詳解如何使用python創(chuàng)建和結(jié)束線程
線程的創(chuàng)建和結(jié)束是多線程編程中的核心概念之一,在本文中,我們將學(xué)習(xí)如何使用 Python 創(chuàng)建線程,并探討如何優(yōu)雅地結(jié)束線程,需要的朋友可以參考下2024-04-04python sklearn包——混淆矩陣、分類報(bào)告等自動(dòng)生成方式
今天小編就為大家分享一篇python sklearn包——混淆矩陣、分類報(bào)告等自動(dòng)生成方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02利用Python實(shí)現(xiàn)網(wǎng)絡(luò)測(cè)試的腳本分享
這篇文章主要給大家介紹了關(guān)于利用Python實(shí)現(xiàn)網(wǎng)絡(luò)測(cè)試的方法,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-05-05Python使用conda如何安裝requirement.txt的擴(kuò)展包
這篇文章主要介紹了Python使用conda如何安裝requirement.txt的擴(kuò)展包問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02