Add PARTITION after creating TABLE in hive

41.8k views Asked by At

i have created a non partitioned table and load data into the table,now i want to add a PARTITION on the basis of department into that table,can I do this? If I do:

ALTER TABLE Student ADD PARTITION (dept='CSE') location '/test';

It gives me error:

FAILED: SemanticException table is not partitioned but partition spec exists: {dept=CSE}

please help. Thanks

2

There are 2 answers

3
Naresh On BEST ANSWER

First create a table in such a way so that you don't have partition column in the table.

create external table Student(col1 string, col2 string) partitioned by (dept string) location 'ANY_RANDOM_LOCATION';

Once you are done with the creation of the table then alter the table to add the partition department wise like this :

alter table Student add partition(dept ='cse') location '/test';

I hope this will help.

0
Abhijit Kumar On

You can't alter table partition if you didn't define partition while creation of table.

If, when altering a un-partitioned table to add partition, you get this error: "Semantic Exception table is not partitioned but partition spec exists: {dept=CSE}," it means you are trying to include the partitioned in the table itself.

You are not getting syntax error because the syntax of the command is correct and used to alter the partition column.