1. 程式人生 > >Hive orc表 刪除欄位

Hive orc表 刪除欄位

說明

Unfortunately, you can’t!
The only way you can delete column from existing table is by using REPLACE COLUMNS keyword.
But this can be done only for tables with a native SerDe (DynamicSerDe, MetadataTypedColumnsetSerDe, LazySimpleSerDe and ColumnarSerDe).

Your best bet is recreating the schema. Follows the steps.

  1. Check if the table is external. If it isn’t, use the following statement to make it external.

  2. alter table alpha001 set tblproperties(‘EXTERNAL’=’TRUE’);
    Drop the table. Since the table is an external table, you can drop it without dropping the actual table.

  3. Recreate the table with the new schema. You should be able to access the table with new schema.

例子

原始表結構
create external table if not exists ods_mysql.ods_db_o2m__tb_time_period_dict ( 
    `id` bigint comment "自增ID",
    `service_type` string comment "型別",
    `begin_time` string comment "時間段開始時間",
    `end_time` string comment "時間段結束時間",
    `status` string comment "狀態: ENABLE 可用,DISABLE 不可用"
, `version` bigint comment "版本號", `create_time` bigint comment "建立時間", `update_time` bigint comment "更新時間", `code` string COMMENT "code碼,相容原來TimeBucket中的列舉值" ) comment "時間段字典" partitioned by(p_day string) stored as orc ;
刪除欄位 code
hive> drop table ods_mysql.ods_db_o2m__tb_time_period_dict;
hive> 
create external table if not exists ods_mysql.ods_db_o2m__tb_time_period_dict ( 
    `id` bigint comment "自增ID",
    `service_type` string comment "型別",
    `begin_time` string comment "時間段開始時間",
    `end_time` string comment "時間段結束時間",
    `status` string comment "狀態: ENABLE 可用,DISABLE 不可用",
    `version` bigint comment "版本號",
    `create_time` bigint comment "建立時間",
    `update_time` bigint comment "更新時間"
)
comment "時間段字典"
partitioned by(p_day string)
stored as orc ;
重新對映
hive> alter table ods_mysql.ods_db_o2m__tb_time_period_dict add PARTITION (p_day='2018-07-27') location '/user/hive/warehouse/ods_mysql.db/ods_db_o2m__tb_time_period_dict/p_day=2018-07-27';