python操作 hbase 資料的方法,pythonhbase

來源:互聯網
上載者:User

python操作 hbase 資料的方法,pythonhbase

配置 thrift

python使用的包 thrift

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

安裝伺服器端thrift。

參考官網,同時也可以在本機上安裝以終端使用。

thrift Getting Started

也可以參考安裝方法 python 調用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/

執行

thrift –gen py Hbase.thrift
mv gen-py/hbase/ /usr/lib/python2.4/site-packages/ (根據python版本可能有不同)

擷取資料樣本 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"

會基礎的python應該知道result是個list,result[0].columns.items()是一個dict 的索引值對。可以查詢相關資料。或者通過輸出變數,觀察變數的值與類型。

說明:上面程式中 transport.open()進行連結,在執行完後,還需要斷開transport.close()

目前只涉及到讀資料,之後還會繼續更新其他dbase操作。

以上這篇python操作 hbase 資料的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.