python操作 hbase 數(shù)據(jù)的方法
配置 thrift
python使用的包 thrift
個人使用的python 編譯器是pycharm community edition. 在工程中設(shè)置中,找到project interpreter, 在相應(yīng)的工程下,找到package,然后選擇 “+” 添加, 搜索 hbase-thrift (Python client for HBase Thrift interface),然后安裝包。
安裝服務(wù)器端thrift。
參考官網(wǎng),同時也可以在本機上安裝以終端使用。
thrift Getting Started
也可以參考安裝方法 python 調(diào)用HBase 范例
首先,安裝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版本可能有不同)
獲取數(shù)據(jù)示例 1
# coding:utf-8 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 from hbase.ttypes import * import csv def client_conn(): # Make socket transport = TSocket.TSocket('hostname,like:localhost', port) # Buffering is critical. Raw sockets are very slow transport = TTransport.TBufferedTransport(transport) # Wrap in a protocol protocol = TBinaryProtocol.TBinaryProtocol(transport) # Create a client to use the protocol encoder client = Hbase.Client(protocol) # Connect! transport.open() return client if __name__ == "__main__": client = client_conn() # r = client.getRowWithColumns('table name', 'row name', ['column name']) # print(r[0].columns.get('column name')), type((r[0].columns.get('column name'))) result = client.getRow("table name","row name") data_simple =[] # print result[0].columns.items() for k, v in result[0].columns.items(): #.keys() #data.append((k,v)) # print type(k),type(v),v.value,,v.timestamp data_simple.append((v.timestamp, v.value)) writer.writerows(data) csvfile.close() csvfile_simple = open("data_xy_simple.csv", "wb") writer_simple = csv.writer(csvfile_simple) writer_simple.writerow(["timestamp", "value"]) writer_simple.writerows(data_simple) csvfile_simple.close() print "finished"
會基礎(chǔ)的python應(yīng)該知道result是個list,result[0].columns.items()是一個dict 的鍵值對??梢圆樵兿嚓P(guān)資料?;蛘咄ㄟ^輸出變量,觀察變量的值與類型。
說明:上面程序中 transport.open()進行鏈接,在執(zhí)行完后,還需要斷開transport.close()
目前只涉及到讀數(shù)據(jù),之后還會繼續(xù)更新其他dbase操作。
以上這篇python操作 hbase 數(shù)據(jù)的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python3爬蟲里關(guān)于識別微博宮格驗證碼的知識點詳解
在本篇文章里小編給大家分享了關(guān)于Python3爬蟲里關(guān)于識別微博宮格驗證碼的知識點,有興趣的朋友們可以參考下。2020-07-07pytorch DistributedDataParallel 多卡訓(xùn)練結(jié)果變差的解決方案
這篇文章主要介紹了pytorch DistributedDataParallel 多卡訓(xùn)練結(jié)果變差的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06使用python的turtle函數(shù)繪制一個滑稽表情
Turtle庫是Python語言中一個很流行的繪制圖像的函數(shù)庫,今天通過實例代碼給大家分享使用python的turtle函數(shù)繪制一個滑稽表情,一起看看吧2020-02-02Python爬取用戶觀影數(shù)據(jù)并分析用戶與電影之間的隱藏信息!
看電影前很多人都喜歡去 『豆瓣』 看影評,所以我爬取44130條 『豆瓣』 的用戶觀影數(shù)據(jù),分析用戶之間的關(guān)系,電影之間的聯(lián)系,以及用戶和電影之間的隱藏關(guān)系,需要的朋友可以參考下2021-06-06