简体   繁体   中英

Add partition to hive table with no data

I am trying to create a hive table which has the same columns as another table (partitioned). I use the following query for the same

CREATE TABLE destTable STORED AS PARQUET AS select * from srcTable where 1=2;

Apparently I cannot use 'PARTITIONED BY(col_name)' because destTable must not be partitioned. But I want to mention that destTable should be partitioned by a column (same as srcTable) before I add data to it.

Is there a way to do that?

As you mentioned, destTable can not be a partitioned table so there is no way to do this directly. Also, destTable can not be an external table.

In this situation you will need to create a temporary "staging_table" (un-partitioned and a Hive-managed table) to hold the data.

Step 1: Transfer everything from srcTable to the staging_table

Step 2: Create a partitioned destTable and do:

INSERT OVERWRITE TABLE destTable PARTITION(xxxx)
SELECT * FROM staging_table;

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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