我需要将一个Hive表从一个数据库移动到另一个数据库,该怎么做?
从0.14版本开始,您可以使用以下语句将表从一个数据库移动到同一metastore中的另一个数据库:
use old_database;
alter table table_a rename to new_database.table_a
如果table_a
是托管表,上述语句还将移动HDFS上的表数据。
USE NEW_DB;
CREATE TABLE table
AS
SELECT * FROM OLD_DB.table;
DROP TABLE OLD_DB.table;
创建外部表 new_db.table, 类似于 old_db.table 的位置为 '(hdfs文件的路径)';
如果表中有分区,则必须在 new_db.table 中添加分区。
这可能对您有所帮助。
EXPORT TABLE table_or_partition TO hdfs_path;
IMPORT [[EXTERNAL] TABLE table_or_partition] FROM hdfs_path [LOCATION[table_location]];
一些示例语句可能如下:
EXPORT TABLE <table name> 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 <table name> PARTITION (loc="USA") to 'location in hdfs';
IMPORT EXTERNAL TABLE FROM 'location in hdfs' LOCATION ‘/location/of/external/table’;
use new_db;
Create new table in new database:
Create Table table_name;
Insert data in new tables from old tables using the command:
insert into new_table_name partition (partition_column='value')
select col1, col2, col3, col4 from old_db.old_table_name
where partition_column='value';
虽然有很多方法在答案中提供,但没有指导何时使用哪种方法。
如果您的目标是托管表,请使用CTAS:
CREATE NEW_DB.TABLE table AS SELECT * FROM OLD_DB.TABLE;
DROP TABLE OLD_DB.TABLE;
如果您只想更改表名,但表的位置不变:
alter table old_database.table_a rename to new_database.table_a;
使用CREATE LIKE和INSERT:
CREATE NEW_DB.TABLE table LIKE OLD_DB.TABLE;
INSERT INTO TABLE new_table_name partition (partition_column) select col1, col2, col3, col4 from old_db.old_table_name where partition_column='value';
DROP TABLE old_db.old_table_name;
您想要迁移的数据库应该已经提供了一个数据库连接器。借助sqoop和数据库连接器,您可以将其迁移。如果您能更具体地说明要将数据迁移到哪种类型的数据库中,那就更好了。
https://issues.apache.org/jira/browse/HIVE-2496
这个改进仍然是开放的。我知道在Impala中可能实现,但目前在Hive中不行。
开发似乎停滞了。您可以在该页面上投票支持该问题以引起一些关注。