hbase儲存和讀取

2021-09-28 22:54:52 字數 2061 閱讀 7855

# 連線資料庫

bd_hb_connpara =

try:

# 連線hbase表

fami =

tbname = b"命名空間"

conn = hpb.connection(host=bd_hb_connpara['host'],port=bd_hb_connpara['port'])

dbname = bytes("命名空間", 'utf-8')

# 獲取rowkey

rowkey = str(123456789) # 非重複

# 建立表

if dbname + b":" + tbname not in "已有的所有表中":

conn.create_table(dbname + b":" + tbname, fami)

tb = conn.table(dbname + b":" + tbname)

batch = tb.batch()

batch.put(rowkey, )

# 批量提交

batch.send()

print("儲存完成...")

except exception as e:

print(e)

import hbase as hb

bd_hb_connpara =

try:

tbname = '表名'

conn = hb.connectionpool(host=bd_hb_connpara['host'],

port=bd_hb_connpara['rest_port']).connect()

tb = conn['命名空間'][tbname]

rowkey = str(123456789)

tb.batch_put(hb.row(rowkey,

))# 傳送

tb.flush()

print("%s 儲存完成..." % rowkey)

except exception as e:

print(e)

try:

tbname = b"表名"

conn = hpb.connection(host=bd_hb_connpara['host'],

port=bd_hb_connpara['port'])

t = conn.table(b"命名空間" + b":" + tbname) # 讀取該錶

# 讀取24小時內的資訊

rs = time.strftime('%y%m%d%h%m%s', time.localtime(time.time() - 24 * 3600))

re = time.strftime('%y%m%d%h%m%s', time.localtime(time.time()))

tm1 = list(t.scan(row_start=rs, row_stop=re))

for tm in tm1:

tdate = tm[1][b'fami:cdate'].decode() + ".jpg" # 取時間

# 取標題

ctitle = tm[1][b'fami:ctitle'].decode()

# 取內容

ccontent = tm[1][b'fami:piccontent'].decode()

date_info = tm[0].decode() # 取rowkey

b_pic = tm[1][b'fami:cpicture']

s = base64.b64decode(b_pic)

try:

file = open(ctitle + '#' + ccontent + '#' + tdate, 'wb')

file.write(s)

file.close()

except exception as e:

print(e)

conn.close()

except exception as e:

print(e)

檔案儲存和讀取

通過檔案的處理,可以把記憶體中的資料儲存到外存中,實現資料的持久化。一 檔案的儲存 file fp 處理檔案流程 1 開啟檔案 建立乙個從記憶體到磁碟的緩衝區,把內容寫到記憶體中。fopen 2 存資料 把記憶體中的資料對映到磁碟中,實現資料寫入。fprint 3 關閉檔案 fclose 如下 in...

HBASE 讀取資料 優化

1 設定scan快取 scan.setcaching 1000 定義一次互動從服務端傳輸到客戶端的行數 2 顯示的指定列 scan.addcolumn cf,column 只獲取需要的列,減少傳輸的資料量,減少io的消耗 3 使用完resultscanner後關閉,否則可能出現一段時間內服務端一致儲...

hbase資料讀取優化 HBase效能優化 總結篇

1 hbase.hregion.max.filesize應該設定多少合適 預設值 256m 說明 maximum hstorefile size.if any one of a column families hstorefiles has?grown to exceed this value,th...