国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python操作 hbase 數(shù)據(jù)的方法

2020-02-23 04:12:43
字體:
供稿:網(wǎng)友

配置 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-8from thrift import Thriftfrom thrift.transport import TSocketfrom thrift.transport import TTransportfrom thrift.protocol import TBinaryProtocolfrom hbase import Hbase# from hbase.ttypes import ColumnDescriptor, Mutation, BatchMutationfrom hbase.ttypes import *import csvdef 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 clientif __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()進(jìn)行鏈接,在執(zhí)行完后,還需要斷開transport.close()

目前只涉及到讀數(shù)據(jù),之后還會繼續(xù)更新其他dbase操作。

以上這篇python操作 hbase 數(shù)據(jù)的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持武林站長站。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 安陆市| 乌审旗| 凤山市| 安泽县| 汕头市| 静乐县| 哈巴河县| 马山县| 安远县| 临海市| 衡水市| 双流县| 图木舒克市| 中方县| 新和县| 安陆市| 顺平县| 景洪市| 荥阳市| 阿拉善左旗| 图木舒克市| 梅州市| 疏勒县| 山阳县| 香河县| 化隆| 宕昌县| 吴桥县| 驻马店市| 漠河县| 民勤县| 兴安县| 镇平县| 焦作市| 博客| 云梦县| 肥城市| 武冈市| 石台县| 商水县| 永顺县|