Hive學習之修改表、分區、列

修改表的語句容許改變現有表的結構,經過該語句能夠增長列/分區,修改SerDe,增長表和SerDe的屬性或者重命名錶。與之相似,修改分區的語句能夠改變指定分區的屬性。apache

重命名錶

重命名錶的語句以下:oop

ALTER TABLE table_name RENAME TO new_table_name

修改表屬性

修改表屬性的語句以下:佈局

ALTER TABLE table_name SET TBLPROPERTIES (property_name = property_value, property_name = property_value,... )

使用該語句能夠增長表的元數據,last_modified_by, last_modified_time屬性自動被添加和管理,可使用DESCRIBE EXTENDED table_name查詢新增的表屬性。spa

修改表註釋

要修改表的註釋,只須要使用上面介紹的修改表屬性語句,將property_name指定爲'comment' 屬性便可:code

ALTER TABLE table_name SET TBLPROPERTIES('comment' = new_comment);

增長SerDe屬性

增長SerDe屬性的語句以下:orm

ALTER TABLE table_name SET SERDE serde_class_name [WITH SERDEPROPERTIES serde_properties]
ALTER TABLE table_name SET SERDEPROPERTIES
(property_name = property_value,property_name = property_value, ... )

該語句容許向SerDe對象增長自定義的元數據。SerDe屬性在SerDe被Hive初始化時傳遞給表的SerDe。對象

修改表的存儲屬性

ALTER TABLE table_name CLUSTEREDBY (col_name, col_name, ...) [SORTED BY (col_name, ...)] INTO num_buckets BUCKETS

該語句改變表的物理存儲屬性。hadoop

須要注意的時,上述修改表的語句僅修改表的Hive的元數據,不會從新組織或者從新格式化現存數據,用戶須要肯定實際的數據佈局符合元數據的定義。input

新增分區

新增分區的語句爲:string

ALTER TABLEtable_name ADD [IF NOT EXISTS] PARTITION partition_spec[LOCATION 'location1'] partition_spec [LOCATION 'location2'] ...
 
partition_spec:
  :(partition_col = partition_col_value, partition_col = partiton_col_value, ...)

特別地,下面的例子將失敗且不會報錯,不管指定哪一個分區,全部的查詢都將在分區dt='2008-08-08'上執行:

ALTER TABLE page_view ADD PARTITION (dt='2008-08-08', country='us')
location '/path/to/us/part080808' PARTITION (dt='2008-08-09',country='us') location '/path/to/us/part080809';

假設表不能存在分區而執行新增分區的操做將會提示錯誤信息:

hive> alter tabletable_properties add partition (address='china');
FAILED:SemanticException table is not partitioned but partition spec exists:{address=china}

這是因爲在新建表的時候,並無建立分區列address,因此只有在存在分區列的表上執行增長分區的操做,纔會成功。

重命名分區

重命名分區的語句以下:

ALTER TABLE table_name PARTITION partition_spec RENAME TO PARTITION partition_spec;
partition_spec:
  :(partition_col = partition_col_value, partition_col = partiton_col_value, ...)


示例代碼以下:

hive> showpartitions people;
OK
department=1/sex=0/howold=23
Time taken:0.349 seconds, Fetched: 1 row(s)
hive> altertable people partition(department='1',sex='0',howold=23) rename to partition(department='2',sex='1',howold=24);         
OK
Time taken:2.005 seconds
hive> showpartitions people;
OK
department=2/sex=1/howold=24
Time taken:0.271 seconds, Fetched: 1 row(s)

交換分區

交換分區的語句以下:

ALTER TABLEtable_name_1 EXCHANGE PARTITION (partition_spec) WITH TABLE table_name_2;

該語句容許將一個分區中的數據移動另外一個擁有相同schema但沒有那個分區的表中。

恢復分區(MSCKREPAIR TABLE)

Hive在元存儲中爲每一個表存儲了一個分區列表,然而若是新分區直接添加到HDFS中(使用hadoop fs –put),Hive不會知道這些分區,除非在每一個新添加的分區上執行ALTER TABLEtable_name ADD PARTITION命令。爲了不重複執行上述命令,可使用以下的命令:

MSCK REPAIR TABLE table_name;

該語句將把存在於HDFS上但不在元存儲上的分區添加到元存儲中,示例以下:

hive> dfs -mkdir /user/hive/warehouse/learning.db/people/department=1/sex=0/howold=23;
hive> show partitions people;
OK
department=2/sex=1/howold=24
Time taken:0.192 seconds, Fetched: 1 row(s)
hive> msck repair table people;
OK
Partitions notin metastore:     people:department=1/sex=0/howold=23
Repair: Addedpartition to metastore people:department=1/sex=0/howold=23
Time taken:0.943 seconds, Fetched: 2 row(s)
hive> show partitions people;
OK
department=1/sex=0/howold=23
department=2/sex=1/howold=24
Time taken:0.397 seconds, Fetched: 2 row(s)

刪除分區

刪除分區的語句爲:

ALTER TABLE table_name DROP [IF EXISTS] PARTITION partition_spec,PARTITION partition_spec,...

可使用上述語句刪除表的分區,該語句將會刪除指定分區的數據和元數據。對於受NO DROP CASCADE的表,可使用IGNORE PROTECTION刪除指定的分區或一組分區,該語句以下:

ALTER TABLE table_name DROP [IF EXISTS] PARTITION partition_spec IGNORE PROTECTION;

刪除分區的例子以下,從該例子能夠發現,當刪除並不存在的分區時不會提示錯誤信息。

hive> show partitions people;
OK
department=1/sex=0/howold=23
department=2/sex=1/howold=24
Time taken:0.397 seconds, Fetched: 2 row(s)
hive> alter table people drop partition (department='2',sex='2',howold=24);
OK
Time taken:1.596 seconds
hive> show partitions people;
OK
department=1/sex=0/howold=23
department=2/sex=1/howold=24
Time taken:0.227 seconds, Fetched: 2 row(s)
hive> alter table people drop partition (department='2',sex='1',howold=24);
Dropped the partition department=2/sex=1/howold=24
OK
Time taken:2.267 seconds
hive> show partitions people;                                              
OK
department=1/sex=0/howold=23
Time taken:0.191 seconds, Fetched: 1 row(s)

解檔/歸檔分區

ALTER TABLE table_name ARCHIVE PARTITION partition_spec;
ALTER TABLE table_name UNARCHIVE PARTITION partition_spec;

Hive中的歸檔移動分區中的文件到Hadoop歸檔中(HAR),該語句只會減小文件的數量,但不提供壓縮。

修改表/分區的文件格式

ALTER TABLE table_name [PARTITION partitionSpec] SET FILEFORMAT file_format

可使用上述語句修改表或者分區的文件格式,Hive支持的文件格式有:SEQUENCEFILE、TEXTFILE、RCFILE 、ORC和INPUTFORMAT input_format_classnameOUTPUTFORMAT output_format_classname,默認的文件格式爲TEXTFILE,由配置參數hive.default.fileformat指定。TEXTFILE指以純文本文件存儲數據,在數據須要壓縮時使用SEQUENCEFILE,使用INPUTFORMAT和OUTPUTFORMAT指定輸入格式和輸出格式的類名,好比'org.apache.hadoop.hive.contrib.fileformat.base64.Base64TextInputFormat'。

修改表/分區的文件位置

ALTER TABLE table_name [PARTITIONpartitionSpec] SET LOCATION "newlocation"

修改表/分區的Touch

TOUCH讀元數據,而後寫回。這可以觸發前置或者後者hook的執行,假設是存在一個記錄表或者分區修改的hook和直接修改HDFS上文件的外部腳本。因爲外部腳本在Hive以外修改文件,修改不會被hook所記錄,這是外部腳本能夠調用TOUCH以觸發hook,而後標記上述表或者分區爲已修改的。修改表或者分區的TOUCH語句以下:

ALTER TABLE table_name TOUCH [PARTITION partitionSpec];

修改表/分區的保護

能夠在表級或者分區級設置數據保護。啓用NO_DROP將保護表或者分區被刪除,啓用OFFLINE將阻止表或者分區中的數據被查詢,但元數據依然能夠被訪問。若是表中的任何分區啓用了NO_DROP,該表也不能被刪除。修改表或者分區保護的語句以下:

ALTER TABLE table_name [PARTITION partition_spec] ENABLE|DISABLE NO_DROP;
ALTER TABLE table_name [PARTITION partition_spec] ENABLE|DISABLE OFFLINE;

演示代碼及結果以下:

hive> alter table iis enable no_drop;
OK
Time taken: 0.792seconds
hive> drop tableiis;
FAILED: ExecutionError, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. Table iis isprotected from being dropped
hive> alter tableiis enable offline;
OK
Time taken: 0.439seconds
hive> select *from iis;
FAILED:SemanticException [Error 10113]: Query against an offline table or partitionTable iis
hive> alter tablepeople partition (department='1', sex='0', howold=23) enable no_drop;
OK
Time taken: 1.23 seconds
hive> drop table people;
FAILED: ExecutionError, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. Table peoplePartitiondepartment=1/sex=0/howold=23 is protected from being dropped

修改列名/類型/位置/註釋

下面的語句容許修改列名稱、列類型、列註釋、列位置。該語句僅修改Hive元數據,不會觸動表中的數據,用戶須要肯定實際的數據佈局符合元數據的定義。

ALTER TABLE table_name CHANGE [COLUMN] col_old_name col_new_name column_type [COMMENTcol_comment] [FIRST|(AFTER column_name)]

示例代碼及結果以下:

hive> alter table people change telephone mobile string comment 'change column name' first;
OK
Time taken: 0.66seconds
hive>describe people;                                                                    
OK
mobile                string                   changecolumn name 
name                string                                       
age                   int                                         
birthday                date                                       
address                string                                       
department             string                                       
sex                   string                                       
howold              int                                         
# PartitionInformation          
# col_name                  data_type               comment            
            department              string                                       
sex                   string                                       
howold              int

 增長/替換列

增長或者替換列的語句以下,其中ADD COLUMNS在現有列以後但在分區列以前增長新列,REPLACE COLUMNS先刪除現存列,而後再增長新列。替換列只能在表使用自帶SerDe(DynamicSerDe,MetadataTypedColumnsetSerDe, LazySimpleSerDe and ColumnarSerDe)時使用。

ALTER TABLE table_name ADD|REPLACE COLUMNS (col_name data_type[COMMENT col_comment], ...)

REPLACE COLUMNS也能夠用於刪除列,例如:

hive> alter table test add columns (e int comment 'Add new column e');
OK
Time taken: 0.395seconds
hive> describetest;
OK
a                   int                                         
b                   int                                         
c                   int                                         
e                   int                     Add new column e   
d                   int                                         
             
# PartitionInformation          
# col_name                  data_type               comment            
             
d                   int                                         
Time taken: 0.209seconds, Fetched: 10 row(s)
hive> alter table test replace columns (a1 string, b1 string, c1 string);
OK
Time taken: 0.994seconds
hive> describe test;
OK
a1                  string                                       
b1                  string                                       
c1                  string                                       
d                   int                                         
             
# PartitionInformation          
# col_name                  data_type               comment            
             
d                   int                                         
Time taken: 0.232seconds, Fetched: 9 row(s)

須要注意的是該刪除僅修改表的schema,但不刪除數據。

相關文章
相關標籤/搜索