ubuntu导入python的包_在ubuntu环境下怎么利用python将数据批量导入数据hbase
斯蒂芬大帝
能夠單條導入就能夠批量導入配置 thriftpython使用的包 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.gzcd thrift-0.7.0-devsudo ./configure –with-cpp=no –with-ruby=nosudo makesudo make install然后,到HBase的源碼包里,找到src/main/resources/org/apache/hadoop/hbase/thrift/執行thrift –gen py Hbase.thriftmv 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 sockettransport = TSocket.TSocket('hostname,like:localhost', port)# Buffering is critical. Raw sockets are very slowtransport = TTransport.TBufferedTransport(transport)# Wrap in a protocolprotocol = TBinaryProtocol.TBinaryProtocol(transport)# Create a client to use the protocol encoderclient = 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.timestampdata_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操作。
總結
以上是生活随笔為你收集整理的ubuntu导入python的包_在ubuntu环境下怎么利用python将数据批量导入数据hbase的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 公爵多少钱(公爵手表什么档次)
- 下一篇: pythonre正则表达式1012pyt