hbase使用scan查詢數(shù)據(jù)-指定時間范圍和查詢條數(shù)

1、查看hbase數(shù)據(jù),可以使用hbase shell。

2、登錄阿里云服務(wù)器:xxxxxxxxxx

  • 進(jìn)入目錄:/mnt/hbase
  • 創(chuàng)建文件:test.py

3、腳本內(nèi)容:vim test.py

import happybase    #導(dǎo)入包
import datetime     #導(dǎo)入包
 
# 連接HBase集群
connection = happybase.Connection('hb-xxxxxxxxxx.hbase.rds.aliyuncs.com', port=xxxx, timeout=120000 )
table = connection.table('xxxxxxxx')
 
# 讀取前10條數(shù)據(jù)
limit = 10    #一定要提前設(shè)置變量,如果這里不配置,在for循壞里直接寫數(shù)值不生效。
 
# 格式化時間
start_date = datetime.datetime(2024, 6, 20)
end_date = datetime.datetime(2024, 6, 30)
 
start_date_str = start_date.strftime('%Y-%m-%d %H:%M:%S')
end_date_str = end_date.strftime('%Y-%m-%d %H:%M:%S')
 
# for循壞
for key, data in table.scan(
        filter=f"SingleColumnValueFilter('xxxx', 'creationTime', >=, 'binary:{start_date_str}') AND SingleColumnValueFilter('xxxx', 'creationTime', <=, 'binary:{end_date_str}')",
        limit=limit    #這里一定要注意,必須是引用變量
        ):
    print(key, data)    #打印
 
# 關(guān)閉連接
connection.close()

4、執(zhí)行腳本:python test.py

  • 可以看到對應(yīng)時間的數(shù)據(jù)


    image.png

5、如、重新編寫時間,寫一個沒有數(shù)據(jù)的時間,提示如下:

  • 這里其實是沒有數(shù)據(jù)引起的。


    image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容