python之hbase数据库扩展

1、根据时间戳查询

#scan '表名',{COLUMNS => 'a',TIMERANGE => [1661821769833,1661821769835]}
scan 'files1',{COLUMNS => 'a',TIMERANGE => [1661821769833,1661821769835]}

2、清空表

truncate 'files1'

3、查询表中所有数据

scan 'files1'

4、python连接hbase数据库执行建表、插数据、查数据、删除操作

import os
import re
import happybase
import time
connection = happybase.Connection(host='192.168.19.129',port=9090)

# 创建表
connection.create_table(
    'files',
    {
        'a':dict()
    }
)


print(connection.tables())
# 循环插入数据
table = happybase.Table('files',connection)
PicData = open('%s/%s' % (r'/ssdraid/tdc_test/file/','testData1m.txt'), 'rb').read()
print(os.getcwd(),"111111111111")
for i in range(1,10):
    table.put(f'row{i}',{'a:a1':PicData})
    time.sleep(1)

 # 获取表实例  #单条添加表数据
 table = happybase.Table('chenghl',connection)
 table.put('row1',{'a:a1':'a11'})
 table.put('row2',{'b:b1':'b11'})

#查数据
for row_index, col_families in table.scan():  # row_key是行index, col_families是列族
         for col_key, col_value in col_families.items():
             col_key_str = col_key.decode('utf-8')
             col_value_str = col_value.decode('utf-8')
             print("行:{} 列:{} 值:{}".format(row_index, col_key_str, col_value_str))
 print("=================")

# 删除表
 connection.delete_table('files',disable=True)

print(connection.tables())
connection.close()

你可能感兴趣的:(python之hbase数据库扩展)