简体   繁体   English

从配置单元外部表中的分区删除列

[英]drop column from a partition in hive external table

I have a hive external table with 3 partition columns (A,B,C) and now I want to drop B and C columns from the partition.Is it possible to do so? 我有一个配置单元外部表,其中包含3个分区列(A,B,C),现在我想从分区中删除B和C列,是否可以这样做? I have tried with Alter table tab_name drop column col_name; 我已经尝试过使用Alter table tab_name删除列col_name; --- but it throws an error stating partitioned columns cannot be dropped. ---但是它抛出一个错误,指出不能删除分区的列。

To drop partition columns the table should be recreated . 要删除分区列, 应重新创建表。 The steps are: 这些步骤是:

  1. Drop table, dropping external table will not drop data files. 删除表,删除外部表不会删除数据文件。
  2. Reorganize data folders to reflect new partition structure. 重新组织数据文件夹以反映新的分区结构。 Partitions are folders on physical level, hierarchically organized. 分区是物理级别的文件夹,按层次进行组织。 If you delete upper level partition, then all sub-folders should be moved to the upper level and so on. 如果删除上层分区,则所有子文件夹都应移至上层,依此类推。 if you are deleting two upper partition columns and only one is left then it should be only one level subfolders under the table location. 如果要删除两个上分区列,而只剩下一列,则它应该是表位置下的仅一级子文件夹。
  3. Create table with new partitioning schema on top of old location. 在旧位置的顶部创建具有新分区架构的表。
  4. Run MSCK repair table . 运行MSCK repair table It will create partition metadata for all found partitions folders. 它将为所有找到的分区文件夹创建分区元数据。

If all of these steps seem too complex or too difficult to do, then simply create new table and load data : 如果所有这些步骤看起来太复杂或太难了,那么只需创建新表并加载数据:

  1. Create new table with new partitioning schema. 使用新的分区架构创建新表。
  2. Load data into new table. 将数据加载到新表中。
  3. drop old table and rename new one 删除旧表并重命名新表

Like this: 像这样:

set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nonstrict;
insert overwrite table new_table partition(C)
select --list columns without deleted 
from old_table;

And finally, after dropping old table, you can rename new one using ALTER TABLE table_name RENAME TO new_table_name . 最后,删除旧表后,可以使用ALTER TABLE table_name RENAME TO new_table_name重命名新ALTER TABLE table_name RENAME TO new_table_name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM