天天看點

Python程式設計:happybase讀寫HBase資料庫安裝表操作資料操作批量資料操作

happybase文檔: https://happybase.readthedocs.io/en/latest/

安裝

pip install happybase      

表操作

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)
      

資料操作

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')      

批量資料操作

# 批量添加
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      

參考

Python操作HBase之happybase