python3连接hbase 操作

# pip3 install thrift
# pip3 install happybase
# 先在Linux上启动HBASE server
# 在Linux上启动HBASE server的thrift服务

# 再运行python脚本连接服务器
import happybase
S_TABLE_NAME_DEMO = 'lrx_college_student'
# 创建连接,通过参数size来设置连接池中连接的个数
connection = happybase.Connection(host='192.168.1.250', port=9090, timeout=None, 
    autoconnect=True, table_prefix=None, table_prefix_separator=b'_',
    compat='0.98', transport='buffered', protocol='binary'
    )
# 打开传输,无返回值
connection.open()
#获取所有表名,返回一个list
l_tables = connection.tables()
print(l_tables)

bytes_tablename = bytes(S_TABLE_NAME_DEMO, encoding = 'utf-8')
# 创建表,无返回值
if bytes_tablename not in l_tables:
    connection.create_table(S_TABLE_NAME_DEMO,{'info':dict()})
# 获取一个表对象,返回一个happybase.table.Table对象(返回二进制表名)
table0 = connection.table(S_TABLE_NAME_DEMO)
print('表对象为:')
print(table0) #<happybase.table.Table name=b'lrx_test1'>
# 获取表实例,返回一个happybase.table.Table对象(返回表名)
table = happybase.Table(S_TABLE_NAME_DEMO,connection)
print('表实例为:')
print(table) #<happybase.table.Table name='lrx_test1'>

for i in range(5):
    table.put('rowkey%s' %i,{'info:score':'%s' %i} )
table.put('rowkey5',{'info:score':'value5'})



# 获取单元格数据,返回一个list
l_content = table.cells('rowkey1','info:score')
print (l_content) #[b'value1', b'value1']
# 获取计数器列的值,返回当前单元格的值
content2 = table.counter_get('row2','info:score')
print(content2)
# 获取一个扫描器,返回一个generator
scanner = table.scan()
for k,v in scanner:
    print(k,v)
###########################################
# #结果为:
# #b'rowkey0' {b'info:0': b'0'}
# b'rowkey1' {b'info:1': b'value1'}
# b'rowkey2' {b'info:2': b'2'}
# b'rowkey3' {b'info:3': b'3'}
# b'rowkey4' {b'info:4': b'4'}
# b'rowkey5' {b'info:5': b'5'}
table.put('rowkey4',{'info:score':'updated_value4'})
table.delete('rowkey5')

scanner_new = table.scan()
for k,v in scanner_new:
    print(k,v)
###########################################
# #结果为:
# #b'rowkey0' {b'data:score': b'0'}
# b'rowkey1' {b'data:score': b'value1'}
# b'rowkey2' {b'data:score': b'2'}
# b'rowkey3' {b'data:score': b'3'}
# b'rowkey4' {b'data:score': b'updated_value4'}
# 检索多行数据,返回字典
rows_dict = dict(table.rows(['rowkey1', 'rowkey2']))
print(rows_dict)
 
print(scanner_new) #<generator object Table.scan at 0x000001E17CCDAF10>
# 获取一行数据,返回一个dict
info = table.row('rowkey2')
info1={}
for k,v in info.items():
    info1[k.decode()]=v.decode()
print("info1:")
print(info1)

# 删除表
connection.delete_table(S_TABLE_NAME_DEMO,disable=True)
l_tables = connection.tables()
print(l_tables)
#[b'lrx_test', b'student']
# 关闭传输,无返回值
connection.close()

  

posted @ 2021-10-06 20:47  tochenwei  阅读(952)  评论(0编辑  收藏  举报