日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

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

系統(tǒng) 2348 0

配置 thrift

python使用的包 thrift

個(gè)人使用的python 編譯器是pycharm community edition. 在工程中設(shè)置中,找到project interpreter, 在相應(yīng)的工程下,找到package,然后選擇 “+” 添加, 搜索 hbase-thrift (Python client for HBase Thrift interface),然后安裝包。

安裝服務(wù)器端thrift。

參考官網(wǎng),同時(shí)也可以在本機(jī)上安裝以終端使用。

thrift Getting Started

也可以參考安裝方法 python 調(diào)用HBase 范例

首先,安裝thrift

下載thrift,這里,我用的是thrift-0.7.0-dev.tar.gz 這個(gè)版本

tar xzf thrift-0.7.0-dev.tar.gz
cd thrift-0.7.0-dev
sudo ./configure ?Cwith-cpp=no ?Cwith-ruby=no
sudo make
sudo make install

然后,到HBase的源碼包里,找到

src/main/resources/org/apache/hadoop/hbase/thrift/

執(zhí)行

thrift ?Cgen 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"
          

會(huì)基礎(chǔ)的python應(yīng)該知道result是個(gè)list,result[0].columns.items()是一個(gè)dict 的鍵值對(duì)??梢圆樵?xún)相關(guān)資料?;蛘咄ㄟ^(guò)輸出變量,觀察變量的值與類(lèi)型。

說(shuō)明: 上面程序中 transport.open()進(jìn)行鏈接,在執(zhí)行完后,還需要斷開(kāi)transport.close()

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

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


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦?。?!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 息烽县| 景德镇市| 德江县| 耿马| 富顺县| 务川| 河北区| 灵丘县| 威宁| 闽侯县| 竹山县| 分宜县| 沽源县| 通渭县| 望城县| 韶山市| 武威市| 吉安县| 天祝| 松阳县| 东明县| 元阳县| 伊川县| 台前县| 宁都县| 双桥区| 久治县| 石棉县| 满城县| 海城市| 平遥县| 府谷县| 和平区| 香河县| 科技| 贺兰县| 娄底市| 罗源县| 井陉县| 长葛市| 平定县|