為什么要查詢表數據量
在做數據倉庫管理時,數據導入hive或向表生成數據形成的數據資產,表里的數據量和占用存儲空間是重要的元數據屬性。為方便數據使用時計算資源的分配,對數據要有基本的了解,所以需要對表的數據量做統計。
使用 analyze table 主動生成元數據信息
analyze table tableName [partition(partCol[='value'])] compute statistics;
分區表必須添加partition字段才能執行,執行
使用desc extended tableName 方法
desc extended databaseName.tableName;
使用可以查看非分區表
parameters:{totalSize=126532790,
rawDataSize=125773613,
numRows=759177,
COLUMN_STATS_ACCURATE={"BASIC_STATS":"true"},
numFiles=6,
transient_lastDdlTime=1590736183,
comment=單位基本信息},
使用 show create table tableName 查看
show create table tableName;
......
TBLPROPERTIES (
'COLUMN_STATS_ACCURATE'='{\"BASIC_STATS\":\"true\"}',
'numFiles'='6',
'numRows'='759177',
'rawDataSize'='125773613',
'totalSize'='126532790',
'transient_lastDdlTime'='1590736183')
使用 show create table 也只能查看非分區表.
使用 show tblproperties tableName
可以查看單個屬性
show tblproperties tableName("rawDataSize")
使用 explain select * from tableName
hive> explain select * from tableName;
OK
STAGE DEPENDENCIES:
Stage-0 is a root stage
STAGE PLANS:
Stage: Stage-0
Fetch Operator
limit: -1
Processor Tree:
TableScan
alias: ab01
Statistics: Num rows: 759177 Data size: 125773613 Basic stats: COMPLETE Column stats: NONE
Select Operator
使用explain可以查看分區表和非分區表,但后面帶上where條件后結果將不准確或錯誤。
查hive元數據庫
查詢元數據需要連接postgres hive庫。
通過databaseName和tableName查詢表tbl_id
DBS -- 存儲Hive中所有數據庫的基本信息
TBLS -- 存儲Hive表、視圖、索引表的基本信息
使用tbl_id 查詢table_params 數據
使用shell 命令查看表存儲文件大小
-- 單位b
hadoop fs -ls /user/hive/warehouse/table_name | awk -F ' ' '{print $5}'|awk '{a+=$1}END {print a}'
--#查看分區表的容量 單位GB
$ hadoop fs -ls /user/hive/warehouse/table_name/yyyymm=202005 | awk -F ' ' '{print $5}'|awk '{a+=$1}END {print a/(1024*1024*1024)}'
-- 自動轉換單位
hadoop fs -du /user/hive/warehouse/table_name/ | awk '{ sum=$1 ;dir2=$2 ; hum[1024**3]="Gb";hum[1024**2]="Mb";hum[1024]="Kb"; for (x=1024**3; x>=1024; x/=1024){ if (sum>=x) { printf "%.2f %s \t %s\n",sum/x,hum[x],dir2;break } }}'