Insert data into selected columns in HIVE from another table

12,024

Please be careful when using Insert overwrite as it will replace any existing data in your table. As you said, one of the tables consists of a lot of data.

Coming to your question, you can do the following:

Insert into table fruitandveg(pears,kiwi) select pears,kiwi from fruit;

Be careful with the case(preferred lowercase) of pears and kiwi in the insert into clause as Hive is case sensitive.

Share:
12,024

Related videos on Youtube

user2985249
Author by

user2985249

Updated on June 04, 2022

Comments

  • user2985249
    user2985249 almost 2 years

    I have 2 hive tables, one with lots of columns and data the other with some matching columns some non-matching.

    I want to be able to insert data from the first table and specify the columns to insert it into for example:

    Table 1(fruit): Apples string, Oranges string, Pears string, Grapes string, Kiwi string;

    Table 2 (fruitAndVeg): Pears string, Carrot string, Kiwi string;

    I want an insert statement close to the following:

    insert overwrite table fruitAndVeg x (x.Pears, x.kiwi) select y.Pears, y.kiwi from fruit y;
    

    I know that the code below is possible but I have a lot more data in my tables so trying to specify the columns rather than insert nulls or empty strings in manually:

    insert overwrite table fruitAndVeg select y.Pears, emptystring, y.kiwi from fruit y;
    

    Any help is appreciated.

    NB. Can also use Impala if that is easier.

    • Amos
      Amos about 9 years
      I can't see why your first SQL is not enough.
  • Alexei Osipov
    Alexei Osipov over 8 years
    This will not work in Hive because it does not allow to specify columns to insert to.
  • Alexei Osipov
    Alexei Osipov over 8 years
    I've spent quite a lot of time on Hive language DSL pages and there is no possibility to list column names for "insert overwrite". The syntax you mentioned is for ACID tables only and it's not compatible with "insert overwrite".
  • Utsav Jha
    Utsav Jha over 8 years
    thats strange. because I have queries running in that format in hive. I have a table with x columns, and the ''insert into table'' statement has x columns specified. I have changed the columns' ordering a few times, yet my query executes perfectly and the output is in the format I expect it to be.
  • Utsav Jha
    Utsav Jha over 8 years
    @AlexeiOsipov please refer to this:issues.apache.org/jira/browse/HIVE-9481
  • Alexei Osipov
    Alexei Osipov over 8 years
    I suspect that you have have "ACID" tables in Hive. HIVE-9481 describes changes for "insert" statement. Not "insert overwrite".
  • Utsav Jha
    Utsav Jha over 8 years
    I have written the Insert statement only.