How to copy all hive table from one Database to other Database

自古美人都是妖i 提交于 2019-12-02 18:13:50
Venkat Ankam

I can think of couple of options.

  1. Use CTAS.

    CREATE TABLE NEWDB.NEW_TABLE1 AS select * from OLDDB.OLD_TABLE1;
    CREATE TABLE NEWDB.NEW_TABLE2 AS select * from OLDDB.OLD_TABLE2;
    ...
    
  2. Use IMPORT feature of Hive https://cwiki.apache.org/confluence/display/Hive/LanguageManual+ImportExport

Hope this helps.

create external table new_db.table like old_db.table location '(path of file in hdfs file)';

if you have partition in table then you have to add partition in new_db.table.

You can approach one of the following option :

The syntax looks something like this: EXPORT TABLE table_or_partition TO hdfs_path; IMPORT [[EXTERNAL] TABLE table_or_partition] FROM hdfs_path [LOCATION [table_location]];

Some sample statements would look like: EXPORT TABLE TO 'location in hdfs';

Use test_db; IMPORT FROM 'location in hdfs';

Export Import can be appled on a partition basis as well: EXPORT TABLE PARTITION (loc="USA") to 'location in hdfs';

The below import commands imports to an external table instead of a managed one IMPORT EXTERNAL TABLE FROM 'location in hdfs' LOCATION ‘/location/of/external/table’;

pratpor

These are probably the fastest and simplest way to copy / move tables from one db to other.

To move table source

Since 0.14, you can use following statement to move table from one database to another in the same metastore:

alter table old_database.table_a rename to new_database.table_a;

The above statements will also move the table data on hdfs if table_a is a managed table.

To copy table

You can always use CREATE TABLE <new_db>.<new_table> AS SELECT * FROM <old_db>.<old_table>; statements. But I believe this alternate method of copying database using hdfs dfs -cp and then creating tables with LIKE can be a little faster if your tables are huge:

hdfs dfs -cp /user/hive/warehouse/<old_database>.db /user/hive/warehouse/<new_database>.db

And then in Hive:

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