天天看点

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