Python编程:happybase读写HBase数据库
【摘要】 happybase文档: https://happybase.readthedocs.io/en/latest/
安装
pip install happybase
1
表操作
import happybase
# 连接数据库
connection = happybase.Connection(host='hostname', port=9090)
# 查询所...
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
文章来源: pengshiyu.blog.csdn.net,作者:彭世瑜,版权归原作者所有,如需转载,请联系作者。
原文链接:pengshiyu.blog.csdn.net/article/details/86084206
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)