配置 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-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"
會基礎的python應該知道result是個list,result[0].columns.items()是一個dict 的鍵值對。可以查詢相關資料。或者通過輸出變量,觀察變量的值與類型。
說明:上面程序中 transport.open()進行鏈接,在執行完后,還需要斷開transport.close()
目前只涉及到讀數據,之后還會繼續更新其他dbase操作。
以上就是詳解python操作hbase數據的方法介紹的詳細內容,更多請關注Gxl網其它相關文章!
本條技術文章來源于互聯網,如果無意侵犯您的權益請點擊此處反饋版權投訴
本文系統來源:php中文網
TAG標簽:python