convert normal column as partition column in hive

北慕城南 提交于 2019-12-24 10:34:09

问题


I have a table with 3 columns. now i need to modify one of the column as a partition column. Is there any possibility? If not, how can we add partition to existing table. I used the below syntax: create table t1 (eno int, ename string ) row format delimited fields terminated by '\t'; load data local '/....path/' into table t1; alter table t1 add partition (p1='india');

i am getting errors.........

Any one know how to add partition to existing table......?

Thanks in advance.


回答1:


I don't think this is directly possible. Hive would have to completely rearrange and split the files in HDFS because adding the partition would impose a new directory structure.

What I suggest you do is simply create a new table with the desired schema and partition, and insert everything from the first into the second.




回答2:


I think there is no way to convert an existing column of a table to partition. If you want to add a partition in a table use ALTER command as you have already done. If you are dealing with the external table then specify the location field as well. I am not sure whether a partition can be added using ALTER command for managed tables.




回答3:


You can't add a partition to a created table. But you can do something like these steps. Create a new table and insert data from old table to the new one.

/*Original table structure*/
CREATE  TABLE original_table(
    c1 string,
    c2 string,
    c3 string)
STORED AS ORC;



/*Partitioned table structure*/
CREATE  TABLE partitioned_table(
    c1 string,
    c2 string)
partitioned by (c3 string)  
STORED AS ORC;


/*load data from original_table to partitioned_table*/
insert into table partitioned_table partition(c3)     select c1,c2,c3 from  original_table;


/*remae original_table to old_table. You can just drop it if you want it*/
ALTER TABLE original_table RENAME TO old_table;


/*rename partitioned_table to original_table*/
ALTER TABLE partitioned_table RENAME TO original_table; 


来源:https://stackoverflow.com/questions/12584143/convert-normal-column-as-partition-column-in-hive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!