编程语言
首页 > 编程语言> > python 使用Hbase

python 使用Hbase

作者:互联网

# coding=utf-8
"""
依赖:thrift,happybase
注意×××:启动hbase thrift start-port:9090
"""
import happybase

# 创建连接 默认端口9090  table_prefix命名空间,不同命名空间互不影响
connection = happybase.Connection("10.198.192.76",table_prefix="lyj")
# connection = happybase.Connection("10.198.192.76")
# 获取存在的表
table_name_list = connection.tables()
print(table_name_list)

# 禁用表(在做一些删除操作之前必须先禁用表)
# connection.disable_table("tablename")

# 启用表
# connection.enable_table("tablename")

# 列族
# colfamilies = {
#     "cf":{},
#     "df":{}
# }
# 创建表
# connection.create_table("lyjtable",colfamilies)

# 获取表实例
table = happybase.Table("lyjtable",connection)

# 修改,插入数据  行键,{";列族:列":"值"}
# table.put("row1",{"cf:1":"2"})
# table.put("row2",{"cf:col1":"row2-col1"})
table.put("row2",{"cf:col2":"row2-col2"})

# 查一行
# row = table.row(b"row1")
# print(row)
# print(row[b"cf:1"])
# 查多行
# rows = table.rows[b"row1",b"row2"]

# 查同一单元的时间版本
# values = table.cells(b"row1",b"cf:1",include_timestamp=True)
# print(values)


# 扫描
"""
条件扫描
1.从某一行开始扫描:scan(row_start=b"aaa")
2.扫描到某一行停止:scan(row_stop=b"xyz")
3.扫描行之间:scan(row_start=b"aaa",row_stop=b"xyz")
4.扫描行健指定开头:scan(row_prefix=b"abc")
"""
# for k,v in table.scan():
#     print(k,v)


# 删除数据
"""
删整行
table.delete("rowkey")
删除列
table.delete("rowkey",columns=[b"列族:列名",...])
"""
# table.delete(b"row2",columns=[b"cf:col1"])
# print(table.row(b"row2"))


# 批量执行

happybaseAPI:https://happybase.readthedocs.io/en/latest/user.html?highlight=cells#retrieving-rows

标签:row2,python,cf,connection,使用,happybase,table,Hbase,row
来源: https://blog.csdn.net/pyml_kdl/article/details/114710318