python 調(diào)用HBase的簡單實(shí)例
新來的一個工程師不懂HBase,java不熟,python還行,我建議他那可以考慮用HBase的thrift調(diào)用,完成目前的工作。
首先,安裝thrift
下載thrift,這里,我用的是thrift-0.7.0-dev.tar.gz 這個版本
tar xzf thrift-0.7.0-dev.tar.gz cd thrift-0.7.0-dev sudo ./configure --with-cpp=no --with-ruby=no sudo make sudo make install
然后,到HBase的源碼包里,找到
src/main/resources/org/apache/hadoop/hbase/thrift/
執(zhí)行
thrift --gen py Hbase.thrift
mv gen-py/hbase/ /usr/lib/python2.4/site-packages/ (根據(jù)python版本可能有不同)
我這里寫了些調(diào)用的腳本,供大家參考
from unittest import TestCase, main 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 ColumnDescriptor, Mutation, BatchMutation class HBaseTester: def __init__(self, netloc, port, table="staftesttable"): self.tableName = table self.transport = TTransport.TBufferedTransport( TSocket.TSocket(netloc, port)) self.protocol = TBinaryProtocol.TBinaryProtocol(self.transport) self.client = Hbase.Client(self.protocol) self.transport.open() tables = self.client.getTableNames() if self.tableName not in tables: self.__createTable() def __del__(self): self.transport.close() def __createTable(self): name = ColumnDescriptor(name='name') foo = ColumnDescriptor(name='foo') self.client.createTable(self.tableName, [name,foo]) def put(self,key,name,foo): name = Mutation(column="name:v", value=name) foo = Mutation(column="foo:v",value=foo) self.client.mutateRow(self.tablename,key,[name,foo]) def scanner(self,column): scanner = client.scannerOpen(self.tablename,"",[column]) r = client.scannerGet(scanner) result= [] while r: print r[0] result.append(r[0]) r = client.scannerGet(scanner) print "Scanner finished" return result class TestHBaseTester(TestCase): def setUp(self): self.writer = HBaseTester("localhost", 9090) def tearDown(self): name = self.writer.tableName client = self.writer.client client.disableTable(name) client.deleteTable(name) def testCreate(self): tableName = self.writer.tableName client = self.writer.client self.assertTrue(self.writer.tableName in client.getTableNames()) columns =['name:','foo:'] for i in client.getColumnDescriptors(tableName): self.assertTrue(i in columns) def testPut(self): self.writer.put("r1","n1","f1") self.writer.put("r2","n2","f2") self.writer.put("r3","n3","") self.writer.scanner("name:") if __name__ == "__main__": main()
以上這篇python 調(diào)用HBase的簡單實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- python利用thrift服務(wù)讀取hbase數(shù)據(jù)的方法
- python hbase讀取數(shù)據(jù)發(fā)送kafka的方法
- 通用MapReduce程序復(fù)制HBase表數(shù)據(jù)
- 在php的yii2框架中整合hbase庫的方法
- HBASE 常用shell命令,增刪改查方法
- hbase-shell批量命令執(zhí)行腳本的方法
- Hbase、elasticsearch整合中jar包沖突的問題解決
- 詳解spring封裝hbase的代碼實(shí)現(xiàn)
- 詳解VMware12使用三臺虛擬機(jī)Ubuntu16.04系統(tǒng)搭建hadoop-2.7.1+hbase-1.2.4(完全分布式)
- Hbase入門詳解
相關(guān)文章
基于matplotlib中ion()和ioff()的使用詳解
這篇文章主要介紹了基于matplotlib中ion()和ioff()的使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06python 實(shí)現(xiàn)客戶端與服務(wù)端的通信
這篇文章主要介紹了python 實(shí)現(xiàn)客戶端與服務(wù)端的通信的方法,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-12-12Python集合魔法解鎖數(shù)據(jù)去重技巧應(yīng)用實(shí)例
這篇文章主要為大家介紹了Python集合魔法解鎖數(shù)據(jù)去重技巧應(yīng)用實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11Python-re中search()函數(shù)的用法詳解(查找ip)
這篇文章主要介紹了Python-re中search()函數(shù)的用法-----查找ip,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03OpenCV-Python實(shí)現(xiàn)凸包的獲取
凸包是一個計(jì)算幾何中的概念,在圖像處理過程中,我們常常需要尋找圖像中包圍某個物體的凸包,本文就使用OpenCV實(shí)現(xiàn),感興趣的可以了解一下2021-06-06Pandas?Matplotlib保存圖形時坐標(biāo)軸標(biāo)簽太長導(dǎo)致顯示不全問題的解決
在使用matplotlib作圖的時候,有的時候會遇到畫圖時顯示不全和圖片保存時不完整的問題,這篇文章主要給大家介紹了關(guān)于Pandas?Matplotlib保存圖形時坐標(biāo)軸標(biāo)簽太長導(dǎo)致顯示不全問題的解決方法,需要的朋友可以參考下2022-06-06pytorch 同步機(jī)制的實(shí)現(xiàn)
在PyTorch中,當(dāng)多個算子和內(nèi)核被并行執(zhí)行時,PyTorch 通過 CUDA 的流和事件機(jī)制來管理并發(fā)和同步,本文就來介紹一下pytorch 同步機(jī)制,具有一定的參考價值,感興趣的可以了解一下2024-09-09