编程语言
首页 > 编程语言> > python3连接hbase 操作

python3连接hbase 操作

作者:互联网

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

# 再运行python脚本连接服务器
import happybase
S_TABLE_NAME_DEMO = 'lrx_test1'
# 创建连接,通过参数size来设置连接池中连接的个数
connection = happybase.Connection(host='172.2.15.1', 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('lrx_test1', 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()

  运行结果如下:

------------------------------------------------------

[b'student']
表对象为:
<happybase.table.Table name=b'lrx_test1'>
表实例为:
<happybase.table.Table name='lrx_test1'>
[b'1']
0
b'row2' {b'info:score': b'\x00\x00\x00\x00\x00\x00\x00\x00'}
b'rowkey0' {b'info:score': b'0'}
b'rowkey1' {b'info:score': b'1'}
b'rowkey2' {b'info:score': b'2'}
b'rowkey3' {b'info:score': b'3'}
b'rowkey4' {b'info:score': b'4'}
b'rowkey5' {b'info:score': b'value5'}
b'row2' {b'info:score': b'\x00\x00\x00\x00\x00\x00\x00\x00'}
b'rowkey0' {b'info:score': b'0'}
b'rowkey1' {b'info:score': b'1'}
b'rowkey2' {b'info:score': b'2'}
b'rowkey3' {b'info:score': b'3'}
b'rowkey4' {b'info:score': b'updated_value4'}
{b'rowkey1': {b'info:score': b'1'}, b'rowkey2': {b'info:score': b'2'}}
<generator object Table.scan at 0x0000000003530040>
info1:
{'info:score': '2'}
[b'student']

标签:info,connection,score,print,table,hbase,x00,连接,python3
来源: https://www.cnblogs.com/tochw/p/15374153.html