配置 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# 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 數據的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。