HBase Note6:Python向HBase写入图片

HBase Note6:Python向HBase写入图片

图片存储HBase

  • 图片以二进制写入HBase
  • 其中,rowkey = 'test' # 数据的唯一标识,类似sql中的主键
import happybase import requests IMAGE_URL = "https://pic3.zhimg.com/v2-58d652598269710fa67ec8d1c88d8f03_r.jpg?source=1940ef5c" r = requests.get(IMAGE_URL) connection = happybase.Connection( "192.168.92.145", 9090) # 其中的b表示二进制数据,整个表都是二进制存储 connection.open() table = connection.table("tablename") attribs = {} attribs['info:pictures'] = r.content attribs['info:modelid'] = '001' attribs['info:modelname'] = 'picname' rowkey = 'test' # 数据的唯一标识,类似sql中的主键 table.put(row=bytes(rowkey, encoding="utf8"), data=attribs) # 添加一条数据
在habse shell中查看
notion image

读取HBase中图片

#从hbase数据库中读取数据都本地 import happybase connection = happybase.Connection( "192.168.92.145", 9090) # 其中的b表示二进制数据,整个表都是二进制存储 connection.open() table = connection.table("tablename") r = table.row(b'test') #参数就是row key rs=table.scan()#扫描表中的数据 也可以加参数限制 for key,v in rs: print(key,v) with open('ttt2022831.jpg','wb') as f: f.write(r[b'info:pictures'])#和列名对应

存文件

# r = open("test2022831.txt",'rb') r = open(r"D:\Cumtb_Code\data\bgm.mp3",'rb') # print(r) # print(r.read()) file_bin = r.read() # # 将 图片的二进制内容 转成 真实图片 # with open("test2021831.txt","wb") as f: # f.write(file_bin) print(file_bin.__sizeof__()) import happybase connection = happybase.Connection( "192.168.92.145", 9090) # 其中的b表示二进制数据,整个表都是二进制存储 connection.open() table = connection.table("testTxt") attribs = {} attribs['info:value'] = file_bin attribs['info:fileid'] = '002' attribs['info:filename'] = 'txtname' rowkey = 'test' # 数据的唯一标识,类似sql中的主键 table.put(row=bytes(rowkey, encoding="utf8"), data=attribs) # 添加一条数据

读文件

import happybase connection = happybase.Connection( "192.168.92.145", 9090) # 其中的b表示二进制数据,整个表都是二进制存储 connection.open() table = connection.table("testTxt") r = table.row(b'test') #参数就是row key with open('bgm2022931.mp3','wb') as f: f.write(r[b'info:value'])#和列名对应