从别的表中查询出相应的数据并导入到Hive表中
假设Hive中有test表,其建表语句如下所示:

  1. hive> create table test(
  2.     > id int, name string
  3.     > ,tel string)
  4.     > partitioned by
  5.     > (age int)
  6.     > ROW FORMAT DELIMITED
  7.     > FIELDS TERMINATED BY '\t'
  8.     > STORED AS TEXTFILE;
  9. OK
  10. Time taken: 0.261 seconds


大体和wyp表的建表语句类似,只不过test表里面用age作为了分区字段。对于分区,这里在做解释一下:

分区:在Hive中,表的每一个分区对应表下的相应目录,所有分区的数据都是存储在对应的目录中。比如wyp表有dt和city两个分区,则对应dt=20131218,city=BJ对应表的目录为/user/hive/warehouse/dt=20131218/city=BJ,所有属于这个分区的数据都存放在这个目录中。


下面语句就是将wyp表中的查询结果并插入到test表中:

  1. hive> insert into table test
  2.     > partition (age='25')
  3.     > select id, name, tel
  4.     > from wyp;
  5. #####################################################################
  6.            这里输出了一堆Mapreduce任务信息,这里省略
  7. #####################################################################
  8. Total MapReduce CPU Time Spent: 1 seconds 310 msec
  9. OK
  10. Time taken: 19.125 seconds
  11. hive> select * from test;
  12. OK
  13. 5       wyp1    131212121212    25
  14. 6       wyp2    134535353535    25
  15. 7       wyp3    132453535353    25
  16. 8       wyp4    154243434355    25
  17. 1       wyp     13188888888888  25
  18. 2       test    13888888888888  25
  19. 3       zs      899314121       25
  20. Time taken: 0.126 seconds, Fetched: 7 row(s)

这里做一下说明:
我们知道我们传统数据块的形式insert into table values(字段1,字段2),这种形式hive是不支持的。


通过上面的输出,我们可以看到从wyp表中查询出来的东西已经成功插入到test表中去了!如果目标表(test)中不存在分区字段,可以去掉partition (age=’25′)语句。当然,我们也可以在select语句里面通过使用分区值来动态指明分区:

  1. hive> set hive.exec.dynamic.partition.mode=nonstrict;
  2. hive> insert into table test
  3.     > partition (age)
  4.     > select id, name,
  5.     > tel, age
  6.     > from wyp;
  7. #####################################################################
  8.            这里输出了一堆Mapreduce任务信息,这里省略
  9. #####################################################################
  10. Total MapReduce CPU Time Spent: 1 seconds 510 msec
  11. OK
  12. Time taken: 17.712 seconds
  13. hive> select * from test;
  14. OK
  15. 5       wyp1    131212121212    23
  16. 6       wyp2    134535353535    24
  17. 7       wyp3    132453535353    25
  18. 1       wyp     13188888888888  25
  19. 8       wyp4    154243434355    26
  20. 2       test    13888888888888  30
  21. 3       zs      899314121       34
  22. Time taken: 0.399 seconds, Fetched: 7 row(s)


这种方法叫做动态分区插入,但是Hive中默认是关闭的,所以在使用前需要先把hive.exec.dynamic.partition.mode设置为nonstrict。当然,Hive也支持insert overwrite方式来插入数据,从字面我们就可以看出,overwrite是覆盖的意思,是的,执行完这条语句的时候,相应数据目录下的数据将会被覆盖!而insert into则不会,注意两者之间的区别。例子如下:

  1. hive> insert overwrite table test
  2.     > PARTITION (age)
  3.     > select id, name, tel, age
  4.     > from wyp;

更可喜的是,Hive还支持多表插入,什么意思呢?在Hive中,我们可以把insert语句倒过来,把from放在最前面,它的执行效果和放在后面是一样的,如下:

  1. hive> show create table test3;
  2. OK
  3. CREATE  TABLE test3(
  4.   id int,
  5.   name string)
  6. Time taken: 0.277 seconds, Fetched: 18 row(s)
  7. hive> from wyp
  8.     > insert into table test
  9.     > partition(age)
  10.     > select id, name, tel, age
  11.     > insert into table test3
  12.     > select id, name
  13.     > where age>25;
  14. hive> select * from test3;
  15. OK
  16. 8       wyp4
  17. 2       test
  18. 3       zs
  19. Time taken: 4.308 seconds, Fetched: 3 row(s)


可以在同一个查询中使用多个insert子句,这样的好处是我们只需要扫描一遍源表就可以生成多个不相交的输出。这个很酷吧!

四、在创建表的时候通过从别的表中查询出相应的记录并插入到所创建的表中
在实际情况中,表的输出结果可能太多,不适于显示在控制台上,这时候,将Hive的查询输出结果直接存在一个新的表中是非常方便的,我们称这种情况为CTAS(create table .. as select)如下:

  1. hive> create table test4
  2.     > as
  3.     > select id, name, tel
  4.     > from wyp;
  5. hive> select * from test4;
  6. OK
  7. 5       wyp1    131212121212
  8. 6       wyp2    134535353535
  9. 7       wyp3    132453535353
  10. 8       wyp4    154243434355
  11. 1       wyp     13188888888888
  12. 2       test    13888888888888
  13. 3       zs      899314121
  14. Time taken: 0.089 seconds, Fetched: 7 row(s)

 

posted on 2021-11-24 17:02  sean1246  阅读(46)  评论(0编辑  收藏  举报