Hive复制分区表结构以及表数据
发布日期:2021-06-29 01:25:02 浏览次数:2 分类:技术文章

本文共 4267 字,大约阅读时间需要 14 分钟。

目录

 


 1、创建一张表,即将要被复制的表old_table:

hive (default)> create table old_table(age bigint,height string,weight string) partitioned by(p_month int,p_day int,p_hour int)row format delimited fields terminated by ','  STORED AS INPUTFORMAT 'org.apache.hadoop.mapred.TextInputFormat' OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat' ;OKTime taken: 0.429 secondshive (default)> desc old_table;OKcol_name        data_type       commentage                     bigint                                      height                  string                                      weight                  string                                      p_month                 int                                         p_day                   int                                         p_hour                  int                                                          # Partition Information          # col_name              data_type               comment                              p_month                 int                                         p_day                   int                                         p_hour                  int                                         Time taken: 0.112 seconds, Fetched: 13 row(s)

2、向old_table中添加数据,新建文本文件data.txt,向其中添加三行数据,建表时的分隔符为逗号,所以我们用逗号分隔:

25,170,7024,175,6527,180,80

3、使用load命令向表中添加数据:

hive (default)> LOAD DATA LOCAL INPATH '/home/hadoop/temp/data.txt' OVERWRITE INTO TABLE old_table PARTITION (p_month='201609',p_day='20160908',p_hour='2016090800');Loading data to table default.old_table partition (p_month=201609, p_day=20160908, p_hour=2016090800)OKTime taken: 3.424 secondshive (default)> show partitions old_table;OKpartitionp_month=201609/p_day=20160908/p_hour=2016090800Time taken: 0.202 seconds, Fetched: 1 row(s)hive (default)> select * from old_table;OKold_table.age   old_table.height        old_table.weight        old_table.p_month       old_table.p_day old_table.p_hour25      170     70      201609  20160908        201609080024      175     65      201609  20160908        201609080027      180     80      201609  20160908        2016090800Time taken: 0.099 seconds, Fetched: 3 row(s)

4、复制该表结构到新表,即new_table:

hive (default)> create table new_table like old_table;OKTime taken: 0.099 secondshive (default)> desc new_table;OKcol_name        data_type       commentage                     bigint                                      height                  string                                      weight                  string                                      p_month                 int                                         p_day                   int                                         p_hour                  int                                                          # Partition Information          # col_name              data_type               comment                              p_month                 int                                         p_day                   int                                         p_hour                  int                                         Time taken: 0.093 seconds, Fetched: 13 row(s)hive (default)> select * from new_table limit 10;OKnew_table.age   new_table.height        new_table.weight        new_table.p_month       new_table.p_day new_table.p_hourTime taken: 0.566 secondshive (default)> show partitions new_table;OKpartitionTime taken: 0.063 seconds

5、然后将使用命令hadoop fs -cp旧表数据复制到新表的hdfs目录下:

[hadoop@node1 ~]$ hadoop fs -cp /user/hive/warehouse/old_table/*  /user/hive/warehouse/new_table/

6、使用命令 MSCK REPAIR TABLE new_table刷新原数据信息:

hive (default)> MSCK REPAIR TABLE new_table;OKPartitions not in metastore:    new_table:p_month=201609/p_day=20160908/p_hour=2016090800Repair: Added partition to metastore new_table:p_month=201609/p_day=20160908/p_hour=2016090800Time taken: 0.447 seconds, Fetched: 2 row(s)

7、然后查询可看到new_table的数据:

hive (default)> select * from new_table;OKnew_table.age   new_table.height        new_table.weight        new_table.p_month       new_table.p_day new_table.p_hour25      170     70      201609  20160908        201609080024      175     65      201609  20160908        201609080027      180     80      201609  20160908        2016090800Time taken: 0.5 seconds, Fetched: 3 row(s)

 

转载地址:https://blog.csdn.net/ytp552200ytp/article/details/90697122 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Apache Flink 详解
下一篇:大数据----60款顶级开源工具

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年04月28日 17时34分02秒