zoukankan      html  css  js  c++  java
  • Hive中如何快速的复制一张分区表(包括数据)

    转自:http://lxw1234.com/archives/2015/09/484.htm

    关键字: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,只有两个分区,每个分区中都有一条数据,如下:

    1. hive> show partitions t1;
    2. OK
    3. pt=2015-09-11
    4. pt=2015-09-12
    5. Time taken: 0.11 seconds, Fetched: 2 row(s)
    6. hive> desc t1;
    7. OK
    8. id string
    9. pt string
    10. # Partition Information
    11. # col_name data_type comment
    12. pt string
    13. Time taken: 0.123 seconds, Fetched: 7 row(s)
    14. hive> select * from t1;
    15. OK
    16. X 2015-09-11
    17. Y 2015-09-12
    18. Time taken: 0.095 seconds, Fetched: 2 row(s)
    19. hive>

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

    1. hive> create table t2 like t1;
    2. OK
    3. Time taken: 0.162 seconds
    4. hive> desc t2;
    5. OK
    6. id string
    7. pt string
    8. # Partition Information
    9. # col_name data_type comment
    10. pt string
    11. Time taken: 0.139 seconds, Fetched: 7 row(s)
    12. hive> show partitions t2;
    13. OK
    14. Time taken: 0.082 seconds

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

    1. [liuxiaowen@dev ~]$ hadoop fs -cp /hivedata/warehouse/liuxiaowen.db/t1/* /hivedata/warehouse/liuxiaowen.db/t2/
    2. [liuxiaowen@dev ~]$ hadoop fs -ls /hivedata/warehouse/liuxiaowen.db/t2/
    3. Found 2 items
    4. drwxr-xr-x - liuxiaowen liuxiaowen 0 2015-09-11 17:17 /hivedata/warehouse/liuxiaowen.db/t2/pt=2015-09-11
    5. 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的分区元数据;

    1. hive> show partitions t2;
    2. OK
    3. Time taken: 0.082 seconds
    4. hive> MSCK REPAIR TABLE t2;
    5. OK
    6. Partitions not in metastore: t2:pt=2015-09-11 t2:pt=2015-09-12
    7. Repair: Added partition to metastore t2:pt=2015-09-11
    8. Repair: Added partition to metastore t2:pt=2015-09-12
    9. Time taken: 0.249 seconds, Fetched: 3 row(s)
    10. hive> show partitions t2;
    11. OK
    12. pt=2015-09-11
    13. pt=2015-09-12
    14. Time taken: 0.068 seconds, Fetched: 2 row(s)
    15. hive> select * from t2;
    16. OK
    17. X 2015-09-11
    18. Y 2015-09-12
    19. Time taken: 0.123 seconds, Fetched: 2 row(s)
    20. hive>

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

  • 相关阅读:
    codeblocks 配置
    2020-7-28
    echarts markPoint在极坐标散点图中不显示value值
    Oracle cve 2020-14644 分析利用以及回显思路
    Openfire Admin Console SSRF&任意文件读取漏洞 CVE-2019-18394 CVE-2019-18393 poc
    Shiro 回显利用工具(burp
    java反序列化提取payload之Xray高级版的shiro回显poc的提取过程
    CVE-2020-3452 CISCO ASA远程任意文件读取 poc
    记事本陈列-历届数学建模大赛优秀论文(含国赛、美赛、研赛)目录
    懒人必备 |通过爬虫 筛选以及查看CSDN 满足相应积分的资源列表 简单好用
  • 原文地址:https://www.cnblogs.com/cxzdy/p/4934849.html
Copyright © 2011-2022 走看看