Python編程:happybase讀寫HBase數據庫
happybase文檔:
https://happybase.readthedocs.io/en/latest/
安裝
pip install happybase
1
表操作
import happybase # 連接數據庫 connection = happybase.Connection(host='hostname', port=9090) # 查詢所有表 table_name_list = connection.tables() # 建表 families = { 'user_info': dict(), 'history': dict() } connection.create_table('table_name', families) # 刪除表 connection.delete_table(name, disable=False)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
數據操作
table = connection.table('table-name') # 獲取列族信息 info_dict = table.families() # 添加數據 data = { 'family:key1': 'value1', 'family:key2': 'value2' } table.put(b'row-key', data) # 查詢數據 row = table.row(b'row-key') # 刪除數據 row = table.delete(b'row-key')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
批量數據操作
# 批量添加 bat = table.batch() bat.put('row-key1', {'family:key1': 'value1', 'family:key2': 'value2'}) bat.put('row-key2', {'family:key1': 'value1', 'family:key2': 'value2'}) bat.send() # 批量查詢 rows_list = table.rows(['row-key1', 'row-key2']) # 返回list rows_dict = dict(table.rows(['row-key1', 'row-key2']))# 返回dict
1
2
3
4
5
6
7
8
9
10
參考
Python操作HBase之happybase
HBase Python 數據庫
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。