您当前的位置: 首页 >  彭世瑜 Python

Python编程:happybase读写HBase数据库

彭世瑜 发布时间:2019-01-08 16:54:05 ,浏览量:2

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

关注
打赏
1688896170
查看更多评论

彭世瑜

暂无认证

  • 2浏览

    0关注

    2727博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0811s