问题
I have default db in hive table which contains 80 tables .
I have created one more database and I want to copy all the tables from default DB to new Databases.
Is there any way I can copy from One DB to Other DB, without creating individual table.
Please let me know if any solution.. Thanks in advance
回答1:
I can think of couple of options.
Use CTAS.
CREATE TABLE NEWDB.NEW_TABLE1 AS select * from OLDDB.OLD_TABLE1; CREATE TABLE NEWDB.NEW_TABLE2 AS select * from OLDDB.OLD_TABLE2; ...
Use IMPORT feature of Hive https://cwiki.apache.org/confluence/display/Hive/LanguageManual+ImportExport
Hope this helps.
回答2:
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.
回答3:
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’;
回答4:
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>;
来源:https://stackoverflow.com/questions/26636839/how-to-copy-all-hive-table-from-one-database-to-other-database