hive 元數據表結構
Table of Contents
1 CDS
該表內容非常簡單,只有一個字段:cd_id,這個字段不明白是什么意思, 但是有一點可以肯定的是cd_id 並不是tbls.tbl_id.
比如 在partition_keys.tbls_id ,如果與sds.cd_id 關聯,查出來 的結果,可能是錯誤的。
2 db相關
2.1 DBS
該表存儲着數據庫的基本信息。
| 字段 | 說明 |
|---|---|
| DB_ID | 數據庫的編號,作為主鍵 |
| DESC | 對於該數據庫的說明 |
| DB_LOCATION_URI | 數據庫在hdfs中的位置 |
| NAME | 庫名 |
| OWNER_NAME | 庫的所有者。 |
| OWNER_TYPE | 庫擁有者的類型 |
db_id 可以與tbls 表關聯,查詢庫里有哪些表。或者某張表屬於哪個庫。見 TBLS.
mysql> select db_id,db_location_uri,name from DBS; +-------+----------------------------------------------------+---------+ | db_id | db_location_uri | name | +-------+----------------------------------------------------+---------+ | 1 | hdfs://nameservice1/user/hive/warehouse | default | | 3132 | hdfs://nameservice1/user/hive/warehouse/bigdata.db | bigdata | +-------+----------------------------------------------------+---------+
3 SerDe相關
3.1 SERDES
| 字段 | 說明 |
|---|---|
| serde_id | 主鍵,每張表一個編號 |
| NAME | 默認為NULL |
| slib | 序列化使用的庫名 |
slib 是建表時指定的或者根據存儲格式自動指定的。
CREATE EXTERNAL TABLE IF NOT EXISTS my_table ( id string, name string,...) ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.JsonSerDe' STORED AS textfile -- hive 3.0使用Json格式讀寫textfile LOCATION '/usr/hive/text/my_table';
示例如下:
mysql> select * from SERDES limit 5; +----------+------+------------------------------------------------------+ | SERDE_ID | NAME | SLIB | +----------+------+------------------------------------------------------+ | 18005 | NULL | org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe | | 82367 | NULL | org.apache.hadoop.hive.serde2.columnar.ColumnarSerDe | +----------+------+------------------------------------------------------+
3.2 SERDES_PARAMS
| 字段 | 說明 |
|---|---|
| serde_id | 主鍵,序列化的編號 |
| param_key | 參數名 |
| param_value | 參數值 |
參數的可選值有如下幾項:
mysql> select distinct param_key from SERDE_PARAMS; +----------------------+ | param_key | +----------------------+ | field.delim | | serialization.format | | escapeChar | | quoteChar | | separatorChar | +----------------------+
4 存儲相關
4.1 SDS
該表存儲着表的存儲信息。比如inputformat,outputformat,location 等。
| 字段 | 說明 |
|---|---|
| SD_ID | 主鍵,沒什么意義 |
| CD_ID | tbls.tbl_id或者 cds.cd_id |
| INPUT_FORMAT | 數據輸入格式 |
| IS_COMPRESSED | 是否對數據進行壓縮 |
| IS_STOREDASSUBDIRECTORIES | 是否存儲在子目錄 |
| LOCATION | 數據在hdfs中的存放位置 |
| NUM_BUCKETS | 分桶的數量 |
| OUTPUT_FORMAT | 數據輸出格式 |
| SERDE_ID | SERDES.SERDE_ID |
4.2 SD_PARAMS
該表存儲Hive存儲的屬性信息,在創建表時候使用. 通過STORED BY ‘storage.handler.class.name’ [WITH SERDEPROPERTIES (…)指定
| 字段 | 說明 |
|---|---|
| SD_ID | 配置信息ID |
| param_key | 存儲屬性名 |
| param_value |
本地環境數據為空。
5 table相關
5.1 TABLE_PARAMS
這張表存儲着表相關的統計信息。比如有多少個文件,有多少行數據,當前大小,最近一次操作時間。
| 字段 | 說明 |
|---|---|
| TBL_ID | 數據的編號 |
| PARAM_KEY | 參數 |
| PARAM_VALUE | 參數的值 |
一般param_key 包含如下幾個統計項:
| 項 | 說明 |
|---|---|
| COLUMN_STATS_ACCURATE | 是否精確統計列,布爾值 |
| numFiles | 文件個數 |
| numRows | 行數 |
| rawDataSize | 原始數據大小,未壓縮前的數據大小 |
| totalSize | 占用HDFS空間大小 |
| transient_lastDdlTime | 最近一次操作的時間戳 |
| external | 是否外部表,布爾值 |
| comment | 表說明,字符串 |
5.2 TBLS
記錄數據表的信息
| 字段 | 解釋 |
|---|---|
| TBL_ID | 在hive中創建表的時候自動生成的一個id,用來表示,主鍵 |
| CREATE_TIME | 創建的數據表的時間,使用的是時間戳 |
| DBS_ID | 這個表是在那個數據庫里面 |
| LAST_ACCESS_TIME | 最后一次訪問的時間戳 |
| OWNER | 數據表的所有者 |
| RETENTION | 保留時間 |
| SD_ID | 標記物理存儲信息的id |
| TBL_NAME | 數據表的名稱 |
| TBL_TYPE | 數據表的類型,MANAGED_TABLE, EXTERNAL_TABLE, VIRTUAL_VIEW, INDEX_TABLE |
| VIEW_EXPANDED_TEXT | 展開視圖文本,非視圖為null |
| VIEW_ORIGINAL_TEXT | 原始視圖文本,非視圖為null |
TBLS的SD_ID與SDS的SD_ID進行關聯,可以查詢存儲信息,TBLS的DB_ID與DBS的DB_ID進行關聯,可以查詢庫信息。
下面語句可以查看所有表的所屬的數據庫,用戶和表類型。
select b.name as db_name,a.tbl_id,a.owner as tbl_owner,a.tbl_name,a.tbl_type from TBLS a, DBS b where a.db_id = b.db_id;
下面語句可以查看所有表的存儲信息:
select a.owner,a.tbl_name, b.input_format,b.output_format,b.location, b.is_compressed,b.IS_STOREDASSUBDIRECTORIES from TBLS a,SDS b where a.sd_id = b.sd_id;
5.3 TAB_COL_STATS
| 字段 | 說明 |
|---|---|
| CS_ID | 列統計編號 |
| AVG_COL_LEN | 數據的平均長度 |
| MAX_COL_LEN | 數據的最大長度 |
| COLUMN_NAME | 列的名字 |
| COLUMN_TYPE | 列的類型 |
| DB_NAME | 數據庫的名稱 |
| BIG_DECIMAL_HIGH_VALUE | 數據中最大的Decimal值 |
| BIG_DECIMAL_LOW_VALUE | 數據中最小的Decimal值 |
| DOUBLE_HIGH_VALUE | 數據中最大的Double值 |
| DOUBLE_LOW_VALUE | 數據中最小的Double值 |
| LAST_ANALYZED | 最新一次解析的時間戳 |
| LONG_HIGH_VALUE | 數據中最大的Long值 |
| LONG_LOW_VALUE | 數據中最小的Long值 |
| NUM_DISTINCTS | 不同記錄的數量 |
| NUM_FALSES | 為false的數量 |
| NUM_NULLS | 為null的數量 |
| NUM_TRUES | 為true的數量 |
| TBL_ID | 表的ID |
| TABLE_NAME | 數據表的名稱 |
5.4 COLUMNS_V2
| 字段 | 說明 |
|---|---|
| CD_ID | 關聯cds.cd_id,與tbls.tb_id一致 |
| comment | 字段注釋 |
| column_name | 字段名 |
| type_name | 字段類型 |
| integer_idx | 字段在表中的順序 |
6 分區
6.1 PARTITIONS
| 字段 | 說明 |
|---|---|
| PART_ID | 分區的編號 |
| CREATE_TIME | 創建分區的時間 |
| LAST_ACCESS_TIME | 最近一次訪問時間 |
| PART_NAME | 分區的名字 |
| SD_ID | 關聯SDS.SD_ID |
| TBL_ID | 數據表的id,TBLS.tbl_id |
6.2 PARTITION_PARAMS
| 字段 | 說明 |
|---|---|
| PART_ID | 分區的編號 |
| PARAM_KEY | 參數 |
| PARAM_VALUE | 參數的值 |
參數可選值:
| param_key | 說明 |
|---|---|
| COLUMN_STATS_ACCURATE | 是否精確統計,布爾值,默認TRUE |
| numFiles | 有多少個文件 |
| numRows | 有多少行數據 |
| rawDataSize | 原始文件大小,未壓縮前的數據占用空間大小 |
| totalSize | hdfs中占用空間大小 |
| transient_lastDdlTime | 最后一次執行ddl的時間,timestamp類型 |
| last_modified_by | 執行ddl的用戶 |
| last_modified_time | 最后一次執行修改的時間,timestamp類型 |
7 VERSION
這個表是記錄Hive的版本,這個表里面只能有一條記錄,這樣Hive才能啟動。在創建metadata表的時候,自動寫入的信息。
| 字段 | 說明 |
|---|---|
| VER_ID | 版本id |
| SCHEMA_VERSION | |
| VERSION_COMMENT | 一般就是簡單的說明 |
mysql> select * from VERSION; +--------+----------------+----------------------------+ | VER_ID | SCHEMA_VERSION | VERSION_COMMENT | +--------+----------------+----------------------------+ | 1 | 1.1.0 | Hive release version 1.1.0 | +--------+----------------+----------------------------+
8 根據元數據拼寫表的創建語句
下面是基本
select concat('create table ',
t.tbl_name,' (\n',c.col_string,')',
case pk.partition_string
WHEN NULL then NULL
ELSE concat('\npartition by (',pk.partition_string,')')
end,
case se.slib
when null then null
else concat('\nrow format serde\n''',se.slib,'''\n')
end,
case sep.serde_id
when null then null
else concat('WITH SERDEPROPERTIES (\n',sep.params,')\n')
end,
'stored as inputformat\n''',
s.input_format,'''\noutputformat\n''',
s.output_format,'\nlocation\n''',s.location,''';'
-- '\n stored as orc\n LOCATION ''',s.location
-- ,'''\nTBLPROPERTIES(\n ''orc.compression=''SNAPPY'');'
)
from TBLS t left join (select tbl_id,group_concat(concat_ws(' ',pkey_name,pkey_type)) as partition_string from PARTITION_KEYS group by tbl_id order by integer_idx) pk on t.tbl_id = pk.tbl_id
left join DBS d on t.db_id = d.db_id
left join SDS s on t.sd_id = s.sd_id
left join SERDES se on s.serde_id = se.serde_id
left join (select serde_id,group_concat(concat_ws('=',concat('''',param_key,''''),concat('''',param_value,'''\n'))) params from SERDE_PARAMS group by serde_id) sep on se.serde_id = sep.serde_id
left join (select cd_id, group_concat(concat_ws(' ',column_name,type_name) separator ',\n') as col_string from COLUMNS_V2 group by cd_id order by integer_idx) c on s.cd_id = c.cd_id
where t.tbl_id=33374
-- and t.owner = ''
-- and d.name = ''
group by d.name, t.owner,t.tbl_name;
下面是通過元數據,生成將表改為ORC的SQL:
select concat('create table ',
t.tbl_name,'_orc (\n',c.col_string,')',
case
WHEN pk.partition_string is NULL then ' '
ELSE concat('\npartitioned by (',pk.partition_string,')')
end,
'\n stored as orc\n LOCATION ''',s.location,'_orc',
'''\nTBLPROPERTIES(\n ''orc.compress''=''SNAPPY'');',
'\n\ninsert into ',t.tbl_name,'_orc ',
case
WHEN pk.partition_string is NULL then ' '
ELSE concat('partition (',pk.pkeys,')')
end,
'\nselect ',c.cols,
case
WHEN pk.partition_string is NULL then ' '
ELSE concat(',',pk.pkeys)
end,
'\n from ',t.tbl_name,';'
'\n\nalter table ',t.tbl_name,' rename to ',t.tbl_name,'b;\n',
'alter table ',t.tbl_name,'_orc rename to ',t.tbl_name,';\n\n') as contents
from TBLS t left join (select tbl_id,group_concat(concat_ws(' ',pkey_name,pkey_type)) as partition_string,group_concat(pkey_name) as pkeys from PARTITION_KEYS group by tbl_id order by integer_idx) pk on t.tbl_id = pk.tbl_id
left join (select * from DBS where db_id=3132) d on t.db_id = d.db_id
left join SDS s on t.sd_id = s.sd_id
left join SERDES se on s.serde_id = se.serde_id
left join (select serde_id,group_concat(concat_ws('=',concat('''',param_key,''''),concat('''',param_value,'''\n'))) params from SERDE_PARAMS group by serde_id) sep on se.serde_id = sep.serde_id
left join (select cd_id, group_concat(concat_ws(' ',column_name,type_name) order by integer_idx separator ',\n') as col_string ,group_concat(column_name order by integer_idx) as cols from COLUMNS_V2 group by cd_id order by integer_idx) c on s.cd_id = c.cd_id
where -- t.tbl_id=28883 and
-- t.owner = '' and
se.slib !='org.apache.hadoop.hive.ql.io.orc.OrcSerde' and
t.tbl_name not like 'stg%'
-- and t.tbl_name='test'
and t.tbl_name not like 'new%'
group by d.name, t.owner,t.tbl_name
into outfile '/tmp/change_table_0603.sql';
Created: 2020-06-02 Tue 23:41
