hive學習筆記

九天高遠發表於2013-06-19

前言

推薦文章

目的文章連結備註
測試1
Hive分割槽表新增欄位+重刷歷史方法(避免舊分割槽新增欄位為NULL)https://blog.csdn.net/hjw199089/article/details/79056612沒怎麼看

庫方面

建立庫

create database kegx_schema;
create database if not exists kegx_schema;

-- 修改資料庫預設位置
create database kegx_schema location 'my/myhive.db';//要指定資料庫名
CREATE DATABASE db_test LOCATION '/test/hello';
-- db_name,comment,location,owner_name,owner_type,parameters
-- db_test,"",hdfs://localhost:9000/test/hello,root,USER

-- 為資料庫增加描述資訊
create database kegx_schema comment 'hive by kegx';

-- 增加一些和其相關的鍵-值對屬性資訊
create database kegx_schema with dbproperties('name'='ke','data'='2021-01-02');

-- 檢視
desc database extended kegx_schema;

-- 【注意】設定一個屬性顯示當前所在的資料庫:hive>set hive.cli.print.current.db=true;hive會為每個資料庫建立一個目錄。資料庫中的表以資料庫的子目錄形式儲存;但default沒有目錄;資料庫所在目錄位於:${hive.metastore.warehouse.dir}

-- 使用資料庫
use default

修改庫

-- 修改資料庫,DBPROPERTIES通過鍵-值對屬性值,來描述資料庫資訊。資料庫其他後設資料都是不可更改的,例如資料庫名,資料庫所在目錄是不能修改的。
ALTER DATABASE db_test SET DBPROPERTIES('CREATETIME'='20200101');//在parameters列中顯示{CREATETIME=20200101}
ALTER DATABASE db_test SET DBPROPERTIES('comment'='測試用的資料庫');//在parameters列中顯示{CREATETIME=20200101, comment=???????}

-- 【注意】資料庫所在的目錄位置是可以修改的,但是其底下的資料可不會自動跟著變。
-- 修改方式如下
alter (database|schema) database_name set location hdfs_path

alter database db_test set location 'hdfs://localhost:9000/test/hello';
alter schema db_test set location 'hdfs://localhost:9000/test/hello1';

實驗如下

CREATE DATABASE db_test LOCATION '/test/hello';

create table db_test.employee (eud int,name String,salary String,destination String) COMMENT 'Employee table' ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n' STORED AS TEXTFILE;

dfs -ls /test/hello;
-- drwxr-xr-x   - root supergroup          0 2021-01-03 14:26 /test/hello/employee

alter (database|schema) db_test set location 'hdfs://localhost:9000/test/hello1';

-- drop掉,然後dfs -ls /test/; 發現/test/hello1沒了,但是/test/hello和底下的檔案都還在
DROP DATABASE IF EXISTS db_test CASCADE;

dfs -ls /;
dfs -ls /test/;
dfs -ls /test/hello;
dfs -ls /test/hello1;
-- 結束這次實驗
hadoop fs -rmr /test

刪除庫

-- 刪除空資料庫
DROP DATABASE IF EXISTS kegx_schema;
-- Error while processing statement: FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask.InvalidOperationException(message:Database kegx_schema is not empty. One or more tables exist.)

-- 避免資料庫不存在而丟擲警告資訊
drop database if exists kegx_schema;

-- 刪除非空資料庫,cascade 強制刪除。restrict(預設)不允許刪除。
DROP DATABASE IF EXISTS kegx_schema CASCADE;                  

描述庫

-- 查詢資料庫和表
show databases;
show tables;
-- 使用正規表示式:
show databases like '*關鍵字*';

DESC DATABASE kegx_schema;
--extended顯示資料庫詳細資訊
DESC DATABASE EXTENDED kegx_schema;
-- db_name,comment,location,owner_name,owner_type,parameters
-- kegx_schema,"",hdfs://localhost:9000/user/hive/warehouse/kegx_schema.db,root,USER,""

hadoop fs -ls /user/hive/warehouse
-- Found 1 items
-- drwxr-xr-x   - root supergroup          0 2020-12-26 23:39 /user/hive/warehouse/student

表方面

前言

HIVE: 支援插入, 不支援刪除和更新。

概念解釋
外部表、內部表一般都使用外部表,因為外部表的刪除不是真正的刪除。
內部表(管理表)刪除即真的刪除了。尤其是這張表多個下游在用,只能是建立為外部表。
首先,未被external修飾的是內部表(managed table),被external修飾的為外部表(external table);
區別:
內部表資料由Hive自身管理,外部表資料由HDFS管理;
內部表資料儲存的位置是hive.metastore.warehouse.dir(預設:/user/hive/warehouse),外部表資料的儲存位置由自己制定;
刪除內部表會直接刪除後設資料(metadata)及儲存資料;
刪除外部表僅僅會刪除後設資料,HDFS上的檔案並不會被刪除;
對內部表的修改會將修改直接同步給後設資料,而對外部表的表結構和分割槽進行修改,則需要修復(MSCK REPAIR TABLE table_name;)

建立表

•CREATE TABLE 建立一個指定名字的表。如果相同名字的表已經存在,則丟擲異常;使用者可以用 IF NOT EXIST 選項來忽略這個異常
•EXTERNAL 關鍵字可以讓使用者建立一個外部表,在建表的同時指定一個指向實際資料的路徑(LOCATION)
•LIKE 允許使用者複製現有的表結構,但是不復制資料
•COMMENT可以為表與欄位增加描述
•PARTITIONED BY 指定分割槽
•ROW FORMAT 
  DELIMITED [FIELDS TERMINATED BY char] [COLLECTION ITEMS TERMINATED BY char] 
    MAP KEYS TERMINATED BY char] [LINES TERMINATED BY char] 
    | SERDE serde_name [WITH SERDEPROPERTIES 
    (property_name=property_value, property_name=property_value, ...)] 
  使用者在建表的時候可以自定義 SerDe 或者使用自帶的 SerDe。如果沒有指定 ROW FORMAT 或者 ROW FORMAT DELIMITED,將會使用自帶的 SerDe。在建表的時候,
使用者還需要為表指定列,使用者在指定表的列的同時也會指定自定義的 SerDe,Hive 通過 SerDe 確定表的具體的列的資料。 
•STORED AS 
  SEQUENCEFILE //序列化檔案
  | TEXTFILE //普通的文字檔案格式
  | RCFILE  //行列儲存相結合的檔案
  | INPUTFORMAT input_format_classname OUTPUTFORMAT output_format_classname //自定義檔案格式
  如果檔案資料是純文字,可以使用 STORED AS TEXTFILE。如果資料需要壓縮,使用 STORED AS SEQUENCE 。
•LOCATION指定表在HDFS的儲存路徑
•CLUSTERED表示的是按照某列聚類,例如在插入資料中有兩項“張三,數學”和“張三,英語”
          若是CLUSTERED BY name,則只會有一項,“張三,(數學,英語)”,這個機制也是為了加快查詢的操作。
          
 -- 生成建表資訊
show create table table_name;

實戰

-- 建立客戶資訊表
create external table if not exists `kegx_schema.CUSTOMER_INFORMATION`(`ID` int comment '主鍵',`BATCH_DATETIME` TIMESTAMP comment '批次日期',`ECIF_NUMBER` STRING comment '企業客戶資訊整合號', `CUST_NUMBER` STRING comment '客戶號',`CUST_ORGANIZATION_NUMBER` int comment '客戶所屬機構號',`CUST_ID_CARD_NUMBER` STRING comment '客戶身份證號',`CUST_MAX_ARREARS` int comment '客戶最大欠款額度',`CUST_NAME` string comment '客戶名稱',`CUST_ADDRESS` array<string> comment '客戶常用聯絡住址',`CUST_MAP` map<String,string> comment '客戶關係MAP<K,V>',constraint CUSTOMER_information_pk primary key (ID) disable novalidate) comment '客戶資訊表'
    partitioned by (BATCH_DATE string)
    row format delimited fields terminated by ',' collection items terminated by '-' map keys terminated by ':'
    TBLPROPERTIES ('creator'='kgx','crate_time'='2021-01-01');

-- 複製客戶資訊表
CREATE TABLE IF NOT EXISTS kegx_schema.test AS SELECT * FROM `kegx_schema.CUSTOMER_INFORMATION` ; //-- 沒有分隔符啥的,需要自己加row format,也不是非分割槽表
CREATE TABLE kegx_schema.test LIKE kegx_schema.CUSTOMER_INFORMATION;

-- 建立賬戶資訊表
create external table if not exists `kegx_schema.ACCOUNT_INFORMATION`(
    `ID` int comment '主鍵',`BATCH_DATETIME` TIMESTAMP comment '批次日期',`ECIF_NUMBER` STRING comment '企業客戶資訊整合號',
    `ACCOUNT_NUMBER` STRING comment '賬戶號',`ACCOUNT_ORGANIZATION_NUMBER` int comment '賬戶所屬機構號',ACCOUNT_TYPE STRING comment '賬戶型別',
    `ACCOUNT_BALANCE` int comment '賬戶額度',`CUST_ADDRESS` array<int> comment '客戶當年各月的餘額,如2020;1;2;3;4;5;6;7;8;9;10;11;12',
    `OVERDUE_IN_THE_LAST_MONTH` string comment '最近一個月逾期情況',`OVERDUE_MAP` map<int,string> comment '逾期情況MAP<近X月,欠款數>,如<-3,1000>近三個月欠款1000',
    constraint account_information_pk primary key (ID) disable novalidate) comment '賬戶資訊表'
    partitioned by (BATCH_DATE string)
    row format delimited fields terminated by ',' collection items terminated by ';' map keys terminated by ':'
    TBLPROPERTIES ('creator'='kgx','crate_time'='2021-01-01');

-- 建立客戶逾期資訊表
create external table if not exists `kegx_schema.CUST_OVERDUE_INFO`(
    `ID` int comment '主鍵',
    `BATCH_DATETIME` TIMESTAMP comment '批次日期',
    `ECIF_NUMBER` STRING comment '企業客戶資訊整合號',
    `CUST_NUMBER` STRING comment '客戶號',
    `CUST_ORGANIZATION_NUMBER` INT comment '客戶所屬機構號',
    `OVERDUE_IN_THE_LAST_MONTH` STRING comment '最近一個月逾期情況',
    `OVERDUE_IN_THE_LAST_THREE_MONTH` STRING comment '最近三個月逾期情況',
    `OVERDUE_IN_THE_LAST_SIX_MONTH` STRING comment '最近六個月逾期情況',
    `OVERDUE_IN_FUTURE` DECIMAL(22,2) comment '未來一個月預測的逾期情況,=最近一個月逾期情況X70%+最近三個月逾期情況X20%+最近六個月逾期情況X10%',
    constraint cust_overdue_info_pk primary key (ID) disable novalidate) comment '客戶逾期資訊表'
    partitioned by (BATCH_DATE string)
    row format delimited fields terminated by ',' collection items terminated by ';' map keys terminated by ':'
    TBLPROPERTIES ('creator'='kgx','crate_time'='2021-01-01');

插入表

建立資料

-- 建立資料
[root@localhost kegx_workspace]# vim data_CUSTOMER_INFORMATION.txt
1,2021-01-02 21:16:15,ecif20210010000001,2333000000199603010919,156,440883199603010919,20000,柯小鑫,廣東省廣州市-廣東省佛山市-天津市,father:柯爸爸-mother:柯媽媽
2,2021-01-02 21:16:15,ecif20210010000002,2333000000199603010919,156,440883199603010919,20000,野比大雄,廣東省廣州市-廣東省佛山市-天津市,father:野比二柱子-mother:野比媽媽
[root@localhost kegx_workspace]# cat data_CUSTOMER_INFORMATION.txt

[root@localhost kegx_workspace]# touch data_ACCOUNT_INFORMATION.txt
[root@localhost kegx_workspace]# chmod 777 data_ACCOUNT_INFORMATION.txt
[root@localhost kegx_workspace]# vim data_ACCOUNT_INFORMATION.txt
1,2021-01-03 15:35:15,ecif20210010000001,233300000019960301091901,156,1,10000,2020;0;0;0;0;0;0;0;0;0;0;100000,0,-1:10;-3:0;-6:0;-12:0;-18:0;-24:0
2,2021-01-03 15:35:15,ecif20210010000001,233300000019960301091902,156,2,20000,2020;0;0;0;1000000;100000;0;0;0;0;0,0,-1:0;-3:0;-6:0;-12:0;-18:0;-24:0
3,2021-01-03 15:35:15,ecif20210010000002,233300000019960301091901,156,1,10000,2020;0;0;0;0;0;0;0;0;0;0;0;10000,0,-1:0;-3:0;-6:0;-12:0;-18:0;-24:0
4,2021-01-03 15:35:15,ecif20210010000002,233300000019960301091902,156,2,30000,2020;0;10000;0;10000;0;0;0;0;0;0;0;10000,0,-1:5;-3:10;-6:0;-12:0;-18:0;-24:0
5,2021-01-03 15:35:15,ecif20210010000002,233300000019960301091903,156,3,30000,2020;0;10000;0;0;0;0;10000;0;0;0;0;10000,0,-1:0;-3:0;-6:0;-12:0;-18:0;-24:0
[root@localhost kegx_workspace]# cat data_ACCOUNT_INFORMATION.txt

插入資料

-- hive中載入資料:local模式,載入資料
hive> load data local inpath '/app/kegx_workspace/data_CUSTOMER_INFORMATION.txt' overwrite into table kegx_schema.CUSTOMER_INFORMATION partition (BATCH_DATE='20210102');
-- 非local模式,載入資料
hadoop fs -put /app/kegx_workspace/data_CUSTOMER_INFORMATION.txt /user/hive/warehouse/kegx_schema.db/CUSTOMER_information

描述表

describe kegx_schema.CUSTOMER_INFORMATION;
describe extended kegx_schema.CUSTOMER_INFORMATION;

desc kegx_schema.CUSTOMER_INFORMATION;
desc formatted kegx_schema.CUSTOMER_INFORMATION;

# col_name              data_type               comment             
id                      int                     主鍵                  
batch_datetime          timestamp               批次日期                
cust_number             int                     客戶號                 
cust_organization_number        int                     客戶所屬機構號             
cust_id_card_number     int                     客戶身份證號              
cust_max_arrears        int                     客戶最大欠款額度            
cust_name               string                  客戶名稱                
cust_hobby              array<string>           客戶最大欠款額度            
cust_map                map<string,string>      客戶MAP<K,V>          
                 
# Partition Information          
# col_name              data_type               comment             
batch_date              string                                      
                 
# Detailed Table Information             
Database:               kegx_schema              
OwnerType:              USER                     
Owner:                  root                     
CreateTime:             Sat Jan 02 20:44:38 CST 2021     
LastAccessTime:         UNKNOWN                  
Retention:              0                        
Location:               hdfs://localhost:9000/user/hive/warehouse/kegx_schema.db/CUSTOMER_information     
Table Type:             EXTERNAL_TABLE           
Table Parameters:                
        COLUMN_STATS_ACCURATE   {\"BASIC_STATS\":\"true\"}
        EXTERNAL                TRUE                
        bucketing_version       2                   
        comment                 客戶資訊表               
        crate_time              2020-02-29          
        creator                 kgx                 
        numFiles                0                   
        numPartitions           0                   
        numRows                 0                   
        rawDataSize             0                   
        totalSize               0                   
        transient_lastDdlTime   1609591478          
                 
# Storage Information            
SerDe Library:          org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe       
InputFormat:            org.apache.hadoop.mapred.TextInputFormat         
OutputFormat:           org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat       
Compressed:             No                       
Num Buckets:            -1                       
Bucket Columns:         []                       
Sort Columns:           []                       
Storage Desc Params:             
        collection.delim        -                   
        field.delim             ,                   
        mapkey.delim            :                   
        serialization.format    ,                   
                 
# Constraints            
                 
# Primary Key            
Table:                  kegx_schema.CUSTOMER_information  
Constraint Name:        CUSTOMER_information_pk   
Column Names:           id                       
Time taken: 0.779 seconds, Fetched: 58 row(s)
#
describe extended kegx_schema.CUSTOMER_INFORMATION;


id                      int                     主鍵                  
batch_datetime          timestamp               批次日期                
cust_number             string                  客戶號                 
cust_organization_number        int                     客戶所屬機構號             
cust_id_card_number     string                  客戶身份證號              
cust_max_arrears        int                     客戶最大欠款額度            
cust_name               string                  客戶名稱                
cust_address            array<string>           客戶常用聯絡住址            
cust_map                map<string,string>      客戶關係MAP<K,V>        
batch_date              string                                      
                 
# Partition Information          
# col_name              data_type               comment             
batch_date              string                                      
                 
Detailed Table Information      
Table(tableName:CUSTOMER_information, dbName:kegx_schema, owner:root, createTime:1609639985, lastAccessTime:0, retention:0, sd:StorageDescriptor(cols:[FieldSchema(name:id, type:int, comment:主鍵), 
                                                                                                                                  FieldSchema(name:batch_datetime, type:timestamp, comment:批次日期), FieldSchema(name:cust_number, type:string, comment:客戶號), FieldSchema(name:cust_organization_number, type:int, comment:客戶所屬機構號), FieldSchema(name:cust_id_card_number, type:string, comment:客戶身份證號), FieldSchema(name:cust_max_arrears, type:int, comment:客戶最大欠款額度), FieldSchema(name:cust_name, type:string, comment:客戶名稱), FieldSchema(name:cust_address, type:array<string>, comment:客戶常用聯絡住址), FieldSchema(name:cust_map, type:map<string,string>, comment:客戶關係MAP<K,V>), FieldSchema(name:batch_date, type:string, comment:null)], 
                                                                                                                                                location:hdfs://localhost:9000/user/hive/warehouse/kegx_schema.db/CUSTOMER_information, inputFormat:org.apache.hadoop.mapred.TextInputFormat, outputFormat:org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat, compressed:false, numBuckets:-1, serdeInfo:SerDeInfo(name:null, serializationLib:org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe, parameters:{mapkey.delim=:, collection.delim=-, serialization.format=,, field.delim=,}), bucketCols:[], sortCols:[], parameters:{}, skewedInfo:SkewedInfo(skewedColNames:[], skewedColValues:[], skewedColValueLocationMaps:{}), storedAsSubDirectories:false), partitionKeys:[FieldSchema(name:batch_date, type:string, comment:null)], parameters:{creator=kgx, totalSize=166, EXTERNAL=TRUE, numRows=0, rawDataSize=0, crate_time=2021-01-01, numFiles=1, numPartitions=1, transient_lastDdlTime=1609639985, bucketing_version=2, comment=客戶資訊表}, viewOriginalText:null, viewExpandedText:null, tableType:EXTERNAL_TABLE, rewriteEnabled:false, catName:hive, ownerType:USER)
Constraints     Primary Key for kegx_schema.CUSTOMER_information:[id], Constraint Name: CUSTOMER_information_pk    
Time taken: 0.111 seconds, Fetched: 17 row(s)

刪除表

刪除外部表

-- 測試使用truncate命令刪除外部表:

truncate table mytable;
#FAILED: SemanticException [Error 10146]: Cannot truncate non-managed table mytable. (state=,code=0)

-- 分析,檢視錶結構
describe extended tablename
desc formatted tablename;

-- 原因:truncate不能刪除外部表,只能刪除內部表

-- 刪除外部表 
-- (1)刪除該表分割槽:
alter table tablename drop partition(load_date='2018-11-23',p_hou16);

-- (2)刪除hdfs中的資料
hadoop fs -ls /home/tablename

-- (3)可以將外部表變為內部表,再刪除內部表。不建議為了刪除外部表而先將其轉化為內部表,這樣會將該表關聯的HDFS路徑中的資料也一併刪除的。建立外部表的初衷並不是為了刪除它,這只是為了刪除資料的簡單方式。

ALTER TABLE tablename SET TBLPROPERTIES('EXTERNAL'='False'); 
drop table tablename;

變更表

表本身屬性層面

-- 表本身屬性層面
-- 重新命名錶
ALTER TABLE table_name RENAME TO new_table_name;
ALTER TABLE kegx_schema.CUST_OVERDUE_INFO1 RENAME TO kegx_schema.CUST_OVERDUE_INF;


ALTER TABLE table_name SET TBLPROPERTIES table_properties table_properties:
        :[property_name = property_value…..] -- 使用者可以用這個命令向表中增加metadata
         
-- 新增表註釋、修改表註釋的語句相同.【注意】comment一定要是小寫的,不能是COMMENT,且必須要【加單引號】!
-- 以下2個語句都正確:
ALTER TABLE table_name SET TBLPROPERTIES('comment' = '表的新註釋');
alter table table_name set tblproperties('comment' = '表的新註釋');
-- 語句不報錯,但並不是修改表註釋,只是在TBLPROPERTIES下新加了一個叫COMMENT的屬性,用show create table能看到。
ALTER TABLE table_name SET TBLPROPERTIES('COMMENT' = '表的新註釋');


-- 改變表檔案格式與組織,這個命令修改了表的物理儲存屬性
ALTER TABLE table_name SET FILEFORMAT file_format
ALTER TABLE table_name CLUSTERED BY(userid) SORTED BY(viewTime) INTO num_buckets BUCKETS

-- 更改表的屬性
alter table table_name set TBLPROPERTIES ('EXTERNAL'='TRUE')   -- 內部錶轉內部表
alter table table_name set TBLPROPERTIES ('EXTERNAL'='FALSE')  -- 外部錶轉內部表

-- 修改表的位元組編碼
alter table table_name set serdeproperties  ('serialization.encoding'='utf-8');
-- 建立/刪除檢視
CREATE VIEW [IF NOT EXISTS] view_name [ (column_name [COMMENT column_comment], ...) ][COMMENT view_comment][TBLPROPERTIES (property_name = property_value, ...)] AS SELECT


•增加檢視
•如果沒有提供表名,檢視列的名字將由定義的SELECT表示式自動生成
•如果修改基本表的屬性,檢視中不會體現,無效查詢將會失敗
•檢視是隻讀的,不能用LOAD/INSERT/ALTER
•刪除檢視 DROP VIEW view_name

表欄位層面

修改欄位型別有諸多限制,格外注意,可能導致寫入成功但查詢失敗。

-- 增加、刪除分割槽
-- 增加
ALTER TABLE table_name ADD [IF NOT EXISTS] partition_spec [ LOCATION 'location1' ] partition_spec [ LOCATION 'location2' ] ...
      partition_spec:
  : PARTITION (partition_col = partition_col_value, partition_col = partiton_col_value, ...)

alter table kegx_schema.ACCOUNT_INFORMATION ADD IF NOT EXISTS partition(BATCH_DATE='20210104');
-- 如果存在就顯示,如果不存在就不顯示
show partitions kegx_schema.ACCOUNT_INFORMATION partition(BATCH_DATE='20210104');
-- 顯示分割槽的詳細資訊,可以檢視欄位//Location: hdfs://localhost:9000/test
desc formatted kegx_schema.ACCOUNT_INFORMATION partition(BATCH_DATE='20210104');

-- 刪除 ALTER TABLE table_name DROP partition_spec, partition_spec,...
alter table kegx_schema.ACCOUNT_INFORMATION drop partition(BATCH_DATE='20210103');
ALTER TABLE table_name [PARTITION partition_spec] CHANGE [COLUMN] col_old_name col_new_name column_type [COMMENT col_comment] [FIRST|AFTER column_name] [CASCADE|RESTRICT];
-- 【建議】別用id來玩
ALTER TABLE kegx_schema.ACCOUNT_INFORMATION CHANGE COLUMN id sub_id String COMMENT '備用主鍵';
ALTER TABLE kegx_schema.ACCOUNT_INFORMATION partition(BATCH_DATE='20210103') CHANGE COLUMN id sub_id String COMMENT '備用主鍵';

-- 修改內部表的欄位長度
alter table kegx_schema.CUST_OVERDUE_INFO change OVERDUE_IN_FUTURE OVERDUE_IN_FUTURE DECIMAL(25,2);
alter table kegx_schema.CUST_OVERDUE_INFO change column OVERDUE_IN_FUTURE at DECIMAL(22,2);
ALTER TABLE table_name [PARTITION partition_spec] ADD|REPLACE COLUMNS (col_name data_type [COMMENT col_comment], ...) [CASCADE|RESTRICT]
-- ADD是代表新增一欄位,欄位位置在所有列後面(partition列前) REPLACE則是表示替換表中所有欄位。最後新增CASCADE傾瀉、串聯,否則新增的列在舊分割槽中不可見,查詢資料時為NULL,重新刷資料時仍為NULL。相似問題見:hive分割槽表增加欄位新增欄位值為空的bug

-- 表新增一列
ALTER TABLE table_name ADD COLUMNS (new_col INT);
-- 表新增一列並增加列欄位註釋
ALTER TABLE table_name ADD COLUMNS (new_col INT COMMENT 'a comment');
desc table_name partition(BATCH_DATE='某一分割槽');// -- 未看到增加的欄位,但是select 無論帶不帶分割槽都能看到新欄位
desc table_name;// -- 增加了欄位
ALTER TABLE table_name ADD COLUMNS (new_col1 INT COMMENT 'a comment') CASCADE;
-- 表刪除一列,刪除一個欄位
不支援
-- 刪除欄位(使用新schema表結構替換原有的)
ALTER TABLE table_name REPLACE COLUMNS(id BIGINT, name STRING);
-- 記得最後加上CASCADE,然後desc table_name可見表結構變了,但是底層的資料沒有丟失。dfs -ls 或者hadoop fs -ls

-- 更新列
alter table table_name replace columns (column_new_name new_type COMMENT col_comment);

補充【重要】

[COMMENT col_comment] [FIRST|AFTER column_name] [CASCADE|RESTRICT]; 
This command will allow users to change a column’s name, data type, comment, or position, or an arbitrary 任意 combination組合 of them. The PARTITION clause is available in Hive 0.14.0 and later; see Upgrading Pre-Hive 0.13.0 Decimal Columns for usage. A patch for Hive 0.13 is also available (see HIVE-7971).

The CASCADE|RESTRICT clause is available in Hive 1.1.0. ALTER TABLE CHANGE COLUMN with CASCADE command changes the columns of a table’s metadata, and cascades the same change to all the partition metadata. RESTRICT is the default, limiting column change only to table metadata.

ALTER TABLE CHANGE COLUMN CASCADE clause條款、分句 will override the table partition’s column metadata regardless of the table or partition’s protection mode. Use with discretion慎重.

The column change command will only modify Hive’s metadata, and will not modify data不會修改底層資料. Users should make sure the actual data layout of the table/partition conforms with the metadata definition定義.

Example:

CREATE TABLE test_change (a int, b int, c int);

// First change column a's name to a1.
ALTER TABLE test_change CHANGE a a1 INT;

// Next change column a1's name to a2, its data type to string, and put it after column b.
ALTER TABLE test_change CHANGE a1 a2 STRING AFTER b;
// The new table's structure is:  b int, a2 string, c int.

// Then change column c's name to c1, and put it as the first column.
ALTER TABLE test_change CHANGE c c1 INT FIRST;
// The new table's structure is:  c1 int, b int, a2 string.

// Add a comment to column a1
ALTER TABLE test_change CHANGE a1 a1 INT COMMENT 'this is column a1';

實驗

-- 複製客戶資訊表
CREATE TABLE IF NOT EXISTS kegx_schema.test AS SELECT * FROM `kegx_schema.CUSTOMER_INFORMATION` ; //沒有分隔符啥的,也不是非分割槽表
CREATE TABLE kegx_schema.test LIKE kegx_schema.CUSTOMER_INFORMATION;
load data local inpath '/app/kegx_workspace/data_CUSTOMER_INFORMATION.txt' overwrite into table kegx_schema.test partition (BATCH_DATE='20210103');
show partitions kegx_schema.test;
show partitions kegx_schema.test partition (BATCH_DATE='20210103');
load data local inpath '/app/kegx_workspace/data_CUSTOMER_INFORMATION.txt' overwrite into table kegx_schema.test;
select * from kegx_schema.test limit 20;
select * from kegx_schema.test where BATCH_DATE='20210103' limit 20;
truncate table kegx_schema.test;
// 建立測試表
drop table kegx_schema.test;
CREATE TABLE IF NOT EXISTS kegx_schema.test (id BIGINT, name STRING);
desc formatted kegx_schema.test;
desc formatted kegx_schema.test partition (BATCH_DATE='20210103');
dfs -ls /user/hive/warehouse/kegx_schema.db/test;
dfs -cat /user/hive/warehouse/kegx_schema.db/test/data_CUSTOMER_INFORMATION.txt;
dfs -ls /user/hive/warehouse/kegx_schema.db/test/batch_date=20210103;
dfs -cat /user/hive/warehouse/kegx_schema.db/test/batch_date=20210103/data_CUSTOMER_INFORMATION.txt;//-- 可見資料沒丟
// 新增欄位
ALTER TABLE kegx_schema.test ADD COLUMNS(t_1 STRING) CASCADE ;
// 刪除欄位(使用新schema替換原有的)
ALTER TABLE kegx_schema.test REPLACE COLUMNS(id BIGINT, name STRING) CASCADE;
select * from kegx_schema.test where BATCH_DATE='20210103' limit 20;// -- 因為新的表結構(hive稱為schema)id BIGINT, name STRING所以select 出來就是 `ID` int comment '主鍵',`BATCH_DATETIME` TIMESTAMP comment '批次日期',即第一個第二列的值

在這裡插入圖片描述

實驗

-- 讓某個分割槽表的資料到一個不合法的路徑
alter table kegx_schema.ACCOUNT_INFORMATION ADD IF NOT EXISTS partition(BATCH_DATE='20210104') LOCATION 'hdfs://localhost:9000/test/';
show partitions kegx_schema.ACCOUNT_INFORMATION partition(BATCH_DATE='20210104');//-- 如果存在就顯示,如果不存在就不顯示
desc formatted kegx_schema.ACCOUNT_INFORMATION partition(BATCH_DATE='20210104');//-- 顯示分割槽的詳細資訊,可以檢視欄位//Location: hdfs://localhost:9000/test
alter table kegx_schema.ACCOUNT_INFORMATION drop partition(BATCH_DATE='20210104');
load data local inpath '/app/kegx_workspace/data_ACCOUNT_INFORMATION.txt' overwrite into table kegx_schema.ACCOUNT_INFORMATION partition (BATCH_DATE='20210104');
dfs -ls /;
dfs -ls /test;//-- hadoop fs -ls /test//也看不到資料,但是就是能select 出來
dfs -rmr /test;
select * from `kegx_schema.ACCOUNT_INFORMATION` AS a where BATCH_DATE='20210104' limit 10;

在此特地的提一下 “desc formatted test partition(ds=20190903);” 這個SQL,為什麼呢?

可能有人會問,記這些有啥用,等你被坑的時候,你就知道了?

我來介紹一下這個坑,其實也不是坑吧,個人認為就是有一點不合理,怎麼說呢?對於OLAP來講,確實增減欄位不是很厚道,但是大部分業務會有這樣的場景,比如A表裡面有10個欄位,我需要臨時加一個欄位,怎麼辦?

加個欄位唄? alter table test add columns(age int); – 非分割槽表,正常的

BUT常用的是分割槽表,不可行,常見的一個現象就是,匯入匯出、查詢分割槽表的資料的時候,會出現某一列無資料,有時候報錯,慌不?檢視創表語句發現,確實加上了欄位了呀,但是就是無法進行匯出操作?報不存在欄位,慌不?

不要慌:其實是你操作不對,也不算是Hive的bug吧,Hive已經提供瞭解決方案,如下:

示例:alter table test_partition partition(year=2018) add columns(age int); – 往 year=2018 分割槽裡面新增欄位

這樣寫,只能針對與一個分割槽,進行新增欄位,能批量對歷史分割槽也加上欄位麼?

示例:alter table table_for_test_add_column add columns (added_column string COMMENT ‘新新增的列’) CASCADE;

那麼我們就可以繼續happy的玩耍了~ 驗證各種操作都正常~

分割槽操作

分割槽操作
1)修改分割槽名
alter table table_name partition(dt='partition_old_name') rename to partition(dt='partition_new_name')
 
2)修改分割槽屬性
alter table table_name partition column (dt partition_new_type)
 
3)修改分割槽位置
alter table table_name partition (createtime='20190301') set location "new_location"
 
4)新增分割槽
alter table table_name add partition (partition_name = 'value') location '***'
--示例
alter table table_name add IF NOT EXISTS partition (createtime='20190301') location '/user/hive/warehouse/testdw/js_nk_wn'
 
--還可以同時新增多個分割槽,只需要在後面繼續追加就行
alter table table_name add partition (createtime='20190301') location '/user/hive/warehouse/dept_part' partition (createtime='20190228') location '/user/hive/warehouse/dept_part' 
 
5)刪除分割槽
--刪除一級分割槽
alter table table_name drop if exists partition(createtime='20190301')
--刪除二級分割槽
alter table table_name drop if exists partition (month='02',day='12')
 

查詢表

-- 
show partitions kegx_schema.CUSTOMER_INFORMATION;

-- 
select * from `kegx_schema.CUSTOMER_INFORMATION` AS a where BATCH_DATE='20210103' limit 10;


報錯彙總

問題排查的思路:檢視hive.log.本人用ROOT使用者,所以地址是/tmp/root/hive.log

其中我用的是root使用者,建議參考我的onenote筆記。

Mysql方面

#hive後設資料
CREATE DATABASE db_hive;

drop table user_test;
create table user_test
(
    id       int auto_increment comment 'id' primary key,
    username varchar(50)  null comment '使用者名稱稱',
    sex      varchar(5)   null,
    address  varchar(100) null,
    birthday datetime     not null
)
    charset = utf8;

select * from kgxdb.user_test limit 20;

# mysql 尋找hive後設資料的資訊,例如為啥中文亂碼
# 但是在這裡要非常嚴重強調的一點:hive的後設資料metastore在mysql的資料庫,不管是資料庫本身,還是裡面的表編碼都必須是latin1(CHARACTER SET latin1 COLLATE latin1_bin)!!!!!
# 驗證方式:(可以通過客戶端軟體在資料庫上右鍵屬性檢視,也可以通過命令檢視)
show create database db_hive;
# db_hive,CREATE DATABASE `db_hive` /*!40100 DEFAULT CHARACTER SET utf8 */ /*!80016 DEFAULT ENCRYPTION='N' */



hive的後設資料metastore

#hive系統表(hive的後設資料metastore)
-- ①修改表欄位註解和表註解
alter table db_hive.COLUMNS_V2 modify column COMMENT varchar(256) character set utf8;
alter table db_hive.TABLE_PARAMS modify column PARAM_VALUE varchar(4000) character set utf8;
-- ②修改分割槽欄位註解:
alter table db_hive.PARTITION_PARAMS modify column PARAM_VALUE varchar(4000) character set utf8;
alter table db_hive.PARTITION_KEYS modify column PKEY_COMMENT varchar(4000) character set utf8;
-- ③修改索引註解:
alter table db_hive.INDEX_PARAMS modify column PARAM_VALUE varchar(4000) character set utf8;

select * from db_hive.COLUMNS_V2;

函式方面

show functions '';
show functions like '*crypt*';
desc function aes_encrypt;
desc function extended aes_encrypt;
-- y6Ss+zCYObpCbgfWfyNWTw==
SELECT base64(aes_encrypt('ABC', '1234567890123456'));

-- 模糊查詢函式名
show functions "a.*";
show functions "a.";// -- SHOW FUNCTIONS is deprecated不贊成, please use SHOW FUNCTIONS LIKE instead.
show functions "ab.";
show functions ".s";
show functions "*.s";
show functions ".bs";
-- 模糊查詢表名
show tables "*.test.*"

-- 該語句輸出匹配正規表示式的自定義和內建的函式,使用’.*’輸出所有函式。需要注意的是正規表示式中必須要有點號,否則不會匹配成功

UDF

--extended顯示資料庫詳細資訊
DESC DATABASE EXTENDED db_test;

default,Default Hive database,hdfs://localhost:9000/user/hive/warehouse,public,ROLE,""

DESC DATABASE EXTENDED default;

create table student(id int,name string,sex string);

Shell命令

dfs

hive (default)> dfs -ls /;

![-cat [-ignoreCrc] …]

等價於沒有進入hive控制檯的命令

hive> !pwd;
/app/apache-hive-3.1.2-bin/bin

hive> !ls -lrt;
total 52
-rwxr-xr-x. 1 root root   884 Aug 23  2019 schematool
-rwxr-xr-x. 1 root root   832 Aug 23  2019 metatool
-rwxr-xr-x. 1 root root  3064 Aug 23  2019 init-hive-dfs.sh
-rwxr-xr-x. 1 root root   880 Aug 23  2019 hplsql
-rwxr-xr-x. 1 root root   885 Aug 23  2019 hiveserver2
-rwxr-xr-x. 1 root root  1900 Aug 23  2019 hive-config.sh
-rwxr-xr-x. 1 root root   881 Aug 23  2019 beeline
-rwxr-xr-x. 1 root root 10158 Aug 23  2019 hive
drwxr-xr-x. 3 root root  4096 Dec 26 19:02 ext
-rw-------. 1 root root  6797 Dec 27 00:56 nohup.out

hive> !tail -5f nohup.out;
OK
OK
OK
OK
OK

引文

後記

解決hive中文顯示亂碼

參考文章https://www.cnblogs.com/DreamDrive/p/7469476.html

因為我們知道 metastore 支援資料庫級別,表級別的字符集是 latin1,那麼我們只需要把相應註釋的地方的字符集由 latin1 改成 utf-8,就可以了。用到註釋的就三個地方,表、分割槽、檢視。如下修改分為兩個步驟:

(1)、進入資料庫 Metastore 中執行以下 5 條 SQL 語句

-- ①修改表欄位註解和表註解
alter table COLUMNS_V2 modify column COMMENT varchar(256) character set utf8;
alter table TABLE_PARAMS modify column PARAM_VALUE varchar(4000) character set utf8;
-- ② 修改分割槽欄位註解:
alter table PARTITION_PARAMS modify column PARAM_VALUE varchar(4000) character set utf8;
alter table PARTITION_KEYS modify column PKEY_COMMENT varchar(4000) character set utf8;
-- ③修改索引註解:
alter table INDEX_PARAMS modify column PARAM_VALUE varchar(4000) character set utf8;

(2)、修改 metastore 的連線 URL,即hive-site.xml。

cd /app/apache-hive-3.1.2-bin/conf
vim hive-site.xml

<property>
    <name>javax.jdo.option.ConnectionURL</name>
    <value>jdbc:mysql://IP:3306/db_name?createDatabaseIfNotExist=true&amp;useUnicode=true&amp;characterEncoding=UTF-8</value>
    <description>JDBC connect string for a JDBC metastore</description>
</property>


其中遇到報錯,Xml檔案中不能使用&,要使用他的轉義&來代替。

Caused by: com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '=' (code 61); expected a semi-colon after the reference for entity 'characterEncoding'

Xml檔案中不能使用&,要使用他的轉義&amp;來代替。

測試結果:
在這裡插入圖片描述
在這裡插入圖片描述

以上就能完美的解決這個問題.

未消化,還需要閱讀

建立/刪除函式
建立暫時函式
   以下的語句建立由class_name實現的暫時函式,該函式被建立後僅僅能夠在當前會話中使用。會話結束後函式失效。

實現函式的類能夠是Hive類路徑中的隨意類。能夠使用Add Jar語句向Hive類路徑加入類。

CREATE  TEMPORARY  FUNCTION  function_name  AS  class_name
刪除暫時函式
   使用以下的語句能夠刪除當前會話中的暫時函式:

DROP  TEMPORARY  FUNCTION  [IF  EXISTS] function_name
建立永久函式
   在Hive-0.13版本號及之後的版本號中,自己定義函式能夠被註冊到元儲存中,這樣使用者能夠在每次會話中都引用函式而不必每次都建立函式。以下是建立永久函式的語句:

CREATE FUNCTION[db_name.]function_name AS class_name [USINGJAR|FILE|ARCHIVE 'file_uri' [, JAR|FILE|ARCHIVE 'file_uri'] ]
   在該語句中能夠使用USING從句加入Jar檔案、普通檔案或者歸檔檔案,當函式第一次使用時,這些檔案將會加入到Hive環境中。

若Hive不是以本地模式執行,file_uri是非本地URI,比方能夠使HDFS路徑。

能夠使用引數hive.exec.mode.local.auto設定本地模式,將該引數的值設定為true則為本地模式,默覺得false。假設指定了db_name,則函式將加入到指定的資料庫,否則加入到當前資料庫。引用當前資料庫中的函式僅僅需指定函式名,引用非當前資料庫中的函式則須要使用全限定函式名db_name. function_name。

刪除永久函式
刪除永久函式的語句為:

DROP FUNCTION[IF EXISTS] function_name
Show語句
   在前面學習各種DDL語句時,已經或多或少的使用了一些show語句。比方show databases、show tables等。以下將會系統全面地學習各種show語句。

Show Databases
   完整的show databases語句例如以下,還能夠使用LIKE從句利用正規表示式對資料庫進行過濾,只是萬用字元僅僅能是*(隨意字元)或者|(其他選擇),萬用字元須要使用單引號。

SHOW (DATABASES|SCHEMAS) [LIKE identifier_with_wildcards];
   演示程式碼例如以下:

hive> show databases like 'lea*';
OK
learning
Time taken:0.304 seconds, Fetched: 1 row(s)
hive> show schemas like 'lea*';
OK
learning
Time taken:0.223 seconds, Fetched: 1 row(s)
Show Tables
Show Tables
SHOW TABLES [IN database_name] [identifier_with_wildcards];
   該語句列出當前資料庫中全部的表和檢視。若使用IN從句則列出指定資料庫中的全部表和檢視,還能夠使用正規表示式進行過濾,萬用字元和show databases中的萬用字元使用同樣的規則,即僅僅能使用*和|。表和檢視依照字母順序列出。

show Partitions
SHOW PARTITIONS table_name  [PARTITION(partition_desc)]
   該語句以字母順序列出指定表中的全部分割槽。還能夠通過指定部分分割槽來過濾結果集:

hive> show partitions people;
OK
department=1/sex=0/howold=23
Time taken: 4.75seconds, Fetched: 1 row(s)
hive> show partitions people partition(department='1');
OK
department=1/sex=0/howold=23
Time taken:0.716 seconds, Fetched: 1 row(s)
hive> show partitions people partition(department='2');
OK
Time taken:0.376 seconds

   從Hive-0.13版本號開始還能夠指定資料庫:

hive> show partitions learning.people;
OK
department=1/sex=0/howold=23
Time taken: 0.25seconds, Fetched: 1 row(s)
Show Table/Partition Extended
SHOW TABLE EXTENDED[IN|FROM database_name] LIKE identifier_with_wildcards [PARTITION (partition_desc)]
   該語句列出匹配正規表示式的全部表的資訊。假設指定了PARTITION從句則不能使用正規表示式。該語句的輸出包含基礎表資訊和檔案系統資訊,如totalNumberFiles,totalFileSize, maxFileSize, minFileSize,lastAccessTime和 lastUpdateTime。假設使用了PARTITION則輸出指定分割槽的檔案系統資訊。

Show Table Properties
SHOW TBLPROPERTIES table_name;
SHOW TBLPROPERTIES table_name (‘屬性名’);
   上面的第一個語句以每行一個的格式列出表table_name全部屬性,屬性和屬性值之間以tab分隔。第二個語句輸出指定屬性的值。

hive> show tblproperties table_properties;
OK
numFiles      0
last_modified_by      hadoop
last_modified_time   1402456050
COLUMN_STATS_ACCURATE false
transient_lastDdlTime      1402456050
comment     learning alter properties
numRows    -1
totalSize      0
telephone     1234567
rawDataSize       -1
Time taken:0.247 seconds, Fetched: 10 row(s)
hive> show tblproperties table_properties('telephone');
OK
1234567     
Time taken:0.355 seconds, Fetched: 1 row(s)
Show Create Table
SHOW CREATETABLE ([db_name.]table_name|view_name)
   該語句輸出建立指定表或者檢視的語句。例如以下:

hive> show create table people;
OK
CREATE  TABLE `people`(
  `name` string,
  `age` int,
  `mobile` string COMMENT 'change column name',
  `birthday` date,
  `address` string)
PARTITIONED BY (
  `department` string,
  `sex` string,
  `howold` int)
ROW FORMAT SERDE
 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe'
STORED ASINPUTFORMAT
 'org.apache.hadoop.mapred.SequenceFileInputFormat'
OUTPUTFORMAT
 'org.apache.hadoop.mapred.SequenceFileOutputFormat'
LOCATION
 'hdfs://hadoop:9000/user/hive/warehouse/learning.db/people'
TBLPROPERTIES (
  'last_modified_by'='hadoop',
  'last_modified_time'='1402539133',
  'transient_lastDdlTime'='1402539133')
Time taken:0.477 seconds, Fetched: 22 row(s)
Show Indexes
SHOW [FORMATTED](INDEX|INDEXES) ON table_with_index [(FROM|IN) db_name]
   該語句輸出特定表上的全部索引資訊,包含索引名稱、表名、被索引的列名、儲存索引的表名、索引型別和凝視。假設使用FORMATTED。會為上述資訊加入列標題,例如以下圖所看到的:



Show Columns
SHOW COLUMNS(FROM|IN) table_name [(FROM|IN) db_name]
   該語句輸出給定表中包括分割槽列的全部列:

hive> show columns in people;
OK
name               
age                
mobile             
birthday           
address            
department         
sex                
howold             
Time taken:0.493 seconds, Fetched: 8 row(s)
Show Functions
SHOW FUNCTIONS"a.*"
   該語句輸出匹配正規表示式的自己定義和內建的函式,使用’.*’輸出全部函式。

需要注意的是正規表示式中必需要有點號,否則不會匹配成功,見以下的樣例:

hive>show functions 'av*';
OK
Timetaken: 0.022 seconds
hive>show functions 'av.*';
OK
avg
Timetaken: 0.039 seconds, Fetched: 1 row(s)
Show Locks
SHOW LOCKS<table_name>;
SHOW LOCKS<table_name> EXTENDED;
SHOW LOCKS<table_name> PARTITION (<partition_desc>);
SHOW LOCKS<table_name> PARTITION (<partition_desc>) EXTENDED;
   上述語句顯示錶或者分割槽上的鎖,當使用Hive事務時,上述語句返回以下的資訊:

資料庫名稱
表名稱
分割槽名稱(若表存在分割槽)
鎖的狀態,能夠是以下的一種:
 獲得:請求者擁有鎖
 等待:請求者等待鎖
 終止:鎖已經超時但還未被清理
鎖的型別。能夠是以下的一種:
獨佔鎖:其他不論什麼使用者不可在同一時間擁有該鎖(大多數DDL語句使用該鎖。如刪除表)。
 共享讀鎖:隨意數量的共享讀鎖能夠同一時候鎖定同樣的資源(讀操作取得該鎖,令人困惑的是,插入操作也取得共享讀鎖)。
共享寫鎖:隨意數量的共享讀鎖能夠同一時候鎖定同樣的資源。但其他共享寫鎖不同意鎖定已經被共享寫鎖定的資源(更新和刪除使用共享寫鎖)。
若存在與鎖關聯的事務,則顯示其ID
鎖的持有者最後一次傳送心跳(表明其還存活)的時間
假設鎖被獲得,則顯示獲得鎖的時間
請求鎖的使用者
使用者執行的主機
Show Transactions
SHOW TRANSACTIONS
   事務是在Hive-0.13版本號中引進的。管理員使用該語句查詢當前開啟或者終止的事務。包含例如以下資訊:

事務ID
事務狀態
啟動事務的使用者
事務啟動時所在的主機
Show Compactions
SHOW COMPACTIONS
   該語句顯示當Hive事務被使用時,全部正在被壓縮或者預定壓縮的表和分割槽,包含以下的資訊:

資料庫名稱
表名
 分割槽名稱(假設存在分割槽)
主壓縮還是次要壓縮
壓縮的狀態:
初始化:在佇列中等待壓縮
工作:正在被壓縮
準備清除:壓縮已經結束。舊檔案被安排清除
假設處於工作狀態,顯示壓縮執行緒的執行緒ID
 假設處於工作狀態或者準備清除狀態。顯示壓縮開始的時間
Describe語句
Describe Database
DESCRIBE DATABASE db_name
   該語句顯示給定資料庫的凝視(假設設定的話),在HDFS上的路徑和資料庫的擁有者。例如以下所看到的:

hive>describe database learning;
OK
learning       hdfs://hadoop:9000/user/hive/warehouse/learning.db hadoop
Time taken:0.088 seconds, Fetched: 1 row(s)
hive>describe database default;
OK
default  Default Hive database      hdfs://hadoop:9000/user/hive/warehouse      public
Time taken:0.216 seconds, Fetched: 1 row(s)
Describe Table/View/Column
DESCRIBE [EXTENDED|FORMATTED] [db_name.]table_name[DOT col_name ( [DOT field_name] |[DOT '$elem$'] | [DOT '$key$'] | [DOT '$value$'] )* ]
   該語句顯示給定表包含分割槽列在內的全部列,假設使用了extendedkeyword,則以Thrift序列化形式顯示錶的後設資料,假設使用formattedkeyword,則以表格形式顯示後設資料。

假設表擁有複合型別的列,能夠通過使用表名.複合列名('$elem$'用於陣列,'$key$'用於map的鍵,'$value$'用於map的鍵值)檢視該列的屬性。對於檢視DESCRIBE EXTENDED or FORMATTED能夠用來獲取檢視的定義。兩個相關的屬性被提供:由使用者指定的原始檢視定義和由Hive內部使用的擴充套件定義。

Describe Partition
DESCRIBE [EXTENDED|FORMATTED] [db_name.]table_name PARTITION partition_spec
   該語句顯示指定分割槽列的後設資料。

  1. https://blog.csdn.net/qq_34226628/article/details/111737188 ↩︎

相關文章