hive 数据导入

Hive的几种常见的数据导入方式
这里介绍四种:
(1)、从本地文件系统中导入数据到Hive表;
(2)、从HDFS上导入数据到Hive表;
(3)、从别的表中查询出相应的数据并导入到Hive表中;
(4)、在建立表的时候经过从别的表中查询出相应的记录并插入到所建立的表中。


1、从本地文件系统中导入数据到Hive表oop

 

先在Hive里面建立好表,以下:
  1. hive> create table wyp
  2.     > (id int, name string,
  3.     > age int, tel string)
  4.     > ROW FORMAT DELIMITED
  5.     > FIELDS TERMINATED BY '\t'
  6.     > STORED AS TEXTFILE;
  7. OK
  8. Time taken: 2.832 seconds
本地文件系统里面有个/home/wyp/wyp.txt文件,内容以下:
  1. [wyp@master ~]$ cat wyp.txt
  2. 1       wyp     25      13188888888888
  3. 2       test    30      13888888888888
  4. 3       zs      34      899314121

wyp.txt文件中的数据列之间是使用\t分割的,能够经过下面的语句将这个文件里面的数据导入到wyp表里面,操做以下:
  1. hive> load data local inpath 'wyp.txt' into table wyp;
  2. Copying data from file:/home/wyp/wyp.txt
  3. Copying file: file:/home/wyp/wyp.txt
  4. Loading data to table default.wyp
  5. Table default.wyp stats:
  6. [num_partitions: 0, num_files: 1, num_rows: 0, total_size: 67]
  7. OK
  8. Time taken: 5.967 seconds
能够到wyp表的数据目录下查看,以下命令:

  1. hive> dfs -ls /user/hive/warehouse/wyp ;
  2. Found 1 items
  3. -rw-r--r--3 wyp supergroup 67 2014-02-19 18:23 /hive/warehouse/wyp/wyp.txt

须要注意的是: Hive并不支持INSERT INTO …. VALUES形式的语句

2、HDFS上导入数据到Hive表post

 

  从本地文件系统中将数据导入到Hive表的过程当中,实际上是先将数据临时复制到HDFS的一个目录下(典型的状况是复制到上传用户的HDFS home目录下,好比/home/wyp/),而后再将数据从那个临时目录下移动(注意,这里说的是移动,不是复制!)到对应的Hive表的数据目录里面。既然如此,那么Hive确定支持将数据直接从HDFS上的一个目录移动到相应Hive表的数据目录下,假设有下面这个文件/home/wyp/add.txt,具体的操做以下:
  1. [wyp@master /home/q/hadoop-2.2.0]$ bin/hadoop fs -cat /home/wyp/add.txt
  2. 5       wyp1    23      131212121212
  3. 6       wyp2    24      134535353535
  4. 7       wyp3    25      132453535353
  5. 8       wyp4    26      154243434355

这个文件是存放在HDFS上/home/wyp目录(和一中提到的不一样,一中提到的文件是存放在本地文件系统上)里面,咱们能够经过下面的命令将这个文件里面的内容导入到Hive表中,具体操做以下:

  1. hive> load data inpath '/home/wyp/add.txt' into table wyp;
  2. Loading data to table default.wyp
  3. Table default.wyp stats:
  4. [num_partitions: 0, num_files: 2, num_rows: 0, total_size: 215]
  5. OK
  6. Time taken: 0.47 seconds
  7. hive> select * from wyp;
  8. OK
  9. 5       wyp1    23      131212121212
  10. 6       wyp2    24      134535353535
  11. 7       wyp3    25      132453535353
  12. 8       wyp4    26      154243434355
  13. 1       wyp     25      13188888888888
  14. 2       test    30      13888888888888
  15. 3       zs      34      899314121
  16. Time taken: 0.096 seconds, Fetched: 7 row(s)

从上面的执行结果咱们能够看到,数据的确导入到wyp表中了!请注意load data inpath ‘/home/wyp/add.txt’ into table wyp; 里面是没有local这个单词的,这个是和一中的区别。

3、从别的表中查询出相应的数据并导入到Hive表中spa

 

假设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;

经过上面的输出,咱们能够看到从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;

固然,Hive也支持insert overwrite方式来插入数据,执行完这条语句的时候,相应数据目录下的数据将会被覆盖!而insert into则不会,注意二者之间的区别。例子以下:

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

Hive还支持多表插入
  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;
能够在同一个查询中使用多个insert子句,这样的好处是咱们 只须要扫描一遍源表就能够生成多个不相交的输出

4、在建立表的时候经过从别的表中查询出相应的记录并插入到所建立的表中code

 

在实际状况中,表的输出结果可能太多,不适于显示在控制台上,这时候,将Hive的查询输出结果直接存在一个新的表中是很是方便的,咱们称这种状况为CTAS(create table .. as select)以下:

  1. hive> create table test4
  2.     > as
  3.     > select id, name, tel
  4.     > from wyp;

数据就插入到test4表中去了,CTAS操做是原子的,所以若是select查询因为某种缘由而失败,新表是不会建立的!hadoop