20

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

Chen Kinnrot
  • 20,609
  • 17
  • 79
  • 141
rinku buragohain
  • 329
  • 2
  • 7
  • 15

2 Answers2

15

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.

Naresh
  • 5,073
  • 12
  • 67
  • 124
  • Ya when i created table its a non partioned table, so can i create partition later on,thats my question – rinku buragohain Jun 18 '15 at 06:32
  • 1
    Answer is no. But why at the first place you are changing a non partitioned table to partitioned table. Just create a table with partition of any name. and later you can add as many location you want to add. – Naresh Jun 18 '15 at 06:41
  • OK so it should be pre decide during creation of table which column we want to be partitioned and later on we can add on more columns on it right . – rinku buragohain Jun 18 '15 at 06:45
11

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.

blackgreen
  • 34,072
  • 23
  • 111
  • 129
Abhijit Kumar
  • 151
  • 1
  • 6