微信搜索lxw1234bigdata | 邀请体验:数阅–数据管理、OLAP分析与可视化平台 | 赞助作者:赞助作者

Hive中如何快速的复制一张分区表(包括数据)

Hive lxw1234@qq.com 36138℃ 1评论

关键字:Hive 复制表

Hive中有时候会遇到复制表的需求,复制表指的是复制表结构和数据。

如果是针对非分区表,那很简单,可以使用CREATE TABLE new_table AS SELECT * FROM old_table;

那么如果是分区表呢?

首先想到的办法可能是:

先创建一张和old_table结构相同的new_table,包括分区;可以使用CREATE TABLE new_table LIKE old_table;

接下来使用动态分区,把old_table的数据INSERT到new_table中。

这个方法当然可以,但可能不是最快的。

其实可以这样做:

1. CREATE TABLE new_table LIKE old_table;

2. 使用hadoop fs -cp 命令,把old_table对应的HDFS目录的文件夹全部拷贝到new_table对应的HDFS目录下;

3. 使用MSCK REPAIR TABLE new_table;修复新表的分区元数据;

看例子:

有一张分区表t1,只有两个分区,每个分区中都有一条数据,如下:

hive> show partitions t1;
OK
pt=2015-09-11
pt=2015-09-12
Time taken: 0.11 seconds, Fetched: 2 row(s)
hive> desc t1;
OK
id                      string                                      
pt                      string                                      
                 
# Partition Information          
# col_name              data_type               comment             
                 
pt                      string                                      
Time taken: 0.123 seconds, Fetched: 7 row(s)
hive> select * from t1;
OK
X       2015-09-11
Y       2015-09-12
Time taken: 0.095 seconds, Fetched: 2 row(s)
hive> 

创建一张相同表结构的新表t2;

hive> create table t2 like t1;
OK
Time taken: 0.162 seconds
hive> desc t2;
OK
id                      string                                      
pt                      string                                      
                 
# Partition Information          
# col_name              data_type               comment             
                 
pt                      string                                      
Time taken: 0.139 seconds, Fetched: 7 row(s)
hive> show partitions t2;
OK
Time taken: 0.082 seconds

使用hadoop fs -cp命令把t1对应HDFS目录的所有文件夹复制到t2对应的HDFS目录下:

[liuxiaowen@dev ~]$ hadoop fs -cp /hivedata/warehouse/liuxiaowen.db/t1/* /hivedata/warehouse/liuxiaowen.db/t2/
[liuxiaowen@dev ~]$ hadoop fs -ls /hivedata/warehouse/liuxiaowen.db/t2/
Found 2 items
drwxr-xr-x   - liuxiaowen liuxiaowen          0 2015-09-11 17:17 /hivedata/warehouse/liuxiaowen.db/t2/pt=2015-09-11
drwxr-xr-x   - liuxiaowen liuxiaowen          0 2015-09-11 17:17 /hivedata/warehouse/liuxiaowen.db/t2/pt=2015-09-12

在Hive用使用MSCK REPAIR TABLE t2;修复新表t2的分区元数据;

hive> show partitions t2;
OK
Time taken: 0.082 seconds
hive> MSCK REPAIR TABLE t2;
OK
Partitions not in metastore:    t2:pt=2015-09-11        t2:pt=2015-09-12
Repair: Added partition to metastore t2:pt=2015-09-11
Repair: Added partition to metastore t2:pt=2015-09-12
Time taken: 0.249 seconds, Fetched: 3 row(s)
hive> show partitions t2;
OK
pt=2015-09-11
pt=2015-09-12
Time taken: 0.068 seconds, Fetched: 2 row(s)
hive> select * from t2;
OK
X       2015-09-11
Y       2015-09-12
Time taken: 0.123 seconds, Fetched: 2 row(s)
hive> 

OK,新表t2已经复制好了,它和t1有着相同的表结构,分区结构,分区以及数据。

 

如果觉得本博客对您有帮助,请 赞助作者

转载请注明:lxw的大数据田地 » Hive中如何快速的复制一张分区表(包括数据)

喜欢 (47)
分享 (0)
发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
(1)个小伙伴在吐槽
  1. insert overwrite table t2 partition(pt) select * from t1; 这样动态分区的形式也还行~
    weishuxiao2015-12-30 11:48 回复