hive导入导出数据的几种方式

一,Hive数据导入的几种方式

首先列出讲述下面几种导入方式的数据和hive表。

Hive表:

创建testA:

[java]  view plain  copy
  1. CREATE TABLE testA (  
  2.     id INT,  
  3.     name string,  
  4.     area string  
  5. ) PARTITIONED BY (create_time string) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' STORED AS TEXTFILE;  

创建testB:

[java]  view plain  copy
  1. CREATE TABLE testB (  
  2.     id INT,  
  3.     name string,  
  4.     area string,  
  5.     code string  
  6. ) PARTITIONED BY (create_time string) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' STORED AS TEXTFILE;  

数据文件(sourceA.txt):

[java]  view plain  copy
  1. 1,fish1,SZ  
  2. 2,fish2,SH  
  3. 3,fish3,HZ  
  4. 4,fish4,QD  
  5. 5,fish5,SR  
数据文件(sourceB.txt):

[java]  view plain  copy
  1. 1,zy1,SZ,1001  
  2. 2,zy2,SH,1002  
  3. 3,zy3,HZ,1003  
  4. 4,zy4,QD,1004  
  5. 5,zy5,SR,1005  

(1)本地文件导入到Hive表

[java]  view plain  copy
  1. hive> LOAD DATA LOCAL INPATH '/home/hadoop/sourceA.txt' INTO TABLE testA PARTITION(create_time='2015-07-08');  
  2. Copying data from file:/home/hadoop/sourceA.txt  
  3. Copying file: file:/home/hadoop/sourceA.txt  
  4. Loading data to table default.testa partition (create_time=2015-07-08)  
  5. Partition default.testa{create_time=2015-07-08} stats: [numFiles=1, numRows=0, totalSize=58, rawDataSize=0]  
  6. OK  
  7. Time taken: 0.237 seconds  
  8. hive> LOAD DATA LOCAL INPATH '/home/hadoop/sourceB.txt' INTO TABLE testB PARTITION(create_time='2015-07-09');  
  9. Copying data from file:/home/hadoop/sourceB.txt  
  10. Copying file: file:/home/hadoop/sourceB.txt  
  11. Loading data to table default.testb partition (create_time=2015-07-09)  
  12. Partition default.testb{create_time=2015-07-09} stats: [numFiles=1, numRows=0, totalSize=73, rawDataSize=0]  
  13. OK  
  14. Time taken: 0.212 seconds  
  15. hive> select * from testA;  
  16. OK  
  17. 1   fish1   SZ  2015-07-08  
  18. 2   fish2   SH  2015-07-08  
  19. 3   fish3   HZ  2015-07-08  
  20. 4   fish4   QD  2015-07-08  
  21. 5   fish5   SR  2015-07-08  
  22. Time taken: 0.029 seconds, Fetched: 5 row(s)  
  23. hive> select * from testB;  
  24. OK  
  25. 1   zy1 SZ  1001    2015-07-09  
  26. 2   zy2 SH  1002    2015-07-09  
  27. 3   zy3 HZ  1003    2015-07-09  
  28. 4   zy4 QD  1004    2015-07-09  
  29. 5   zy5 SR  1005    2015-07-09  
  30. Time taken: 0.047 seconds, Fetched: 5 row(s)  


(2)Hive表导入到Hive表

将testB的数据导入到testA表

[java]  view plain  copy
  1. hive> INSERT INTO TABLE testA PARTITION(create_time='2015-07-11') select id, name, area from testB where id = 1;  
  2. ...(省略)  
  3. OK  
  4. Time taken: 14.744 seconds  
  5. hive> INSERT INTO TABLE testA PARTITION(create_time) select id, name, area, code from testB where id = 2;  
  6. <pre name="code" class="java">...(省略)  
OKTime taken: 19.852 secondshive> select * from testA;OK2 zy2 SH 10021 fish1 SZ 2015-07-082 fish2 SH 2015-07-083 fish3 HZ 2015-07-084 fish4 QD 2015-07-085 fish5 SR 2015-07-081 zy1 SZ 2015-07-11Time taken: 0.032 seconds, Fetched: 7 row(s)

 
说明: 

1,将testB中id=1的行,导入到testA,分区为2015-07-11

2,将testB中id=2的行,导入到testA,分区create_time为id=2行的code值。

(3)HDFS文件导入到Hive表

将sourceA.txt和sourceB.txt传到HDFS中,路径分别是/home/hadoop/sourceA.txt和/home/hadoop/sourceB.txt中

[java]  view plain  copy
  1. hive> LOAD DATA INPATH '/home/hadoop/sourceA.txt' INTO TABLE testA PARTITION(create_time='2015-07-08');  
  2. ...(省略)  
  3. OK  
  4. Time taken: 0.237 seconds  
  5. hive> LOAD DATA INPATH '/home/hadoop/sourceB.txt' INTO TABLE testB PARTITION(create_time='2015-07-09');  
  6. <pre name="code" class="java">...(省略)  
  7. OK  
  8. Time taken: 0.212 seconds  
  9. hive> select * from testA;  
  10. OK  
  11. 1   fish1   SZ  2015-07-08  
  12. 2   fish2   SH  2015-07-08  
  13. 3   fish3   HZ  2015-07-08  
  14. 4   fish4   QD  2015-07-08  
  15. 5   fish5   SR  2015-07-08  
  16. Time taken: 0.029 seconds, Fetched: 5 row(s)  
  17. hive> select * from testB;  
  18. OK  
  19. 1   zy1 SZ  1001    2015-07-09  
  20. 2   zy2 SH  1002    2015-07-09  
  21. 3   zy3 HZ  1003    2015-07-09  
  22. 4   zy4 QD  1004    2015-07-09  
  23. 5   zy5 SR  1005    2015-07-09  
  24. Time taken: 0.047 seconds, Fetched: 5 row(s)  

 
/home/hadoop/sourceA.txt'导入到testA表 

/home/hadoop/sourceB.txt'导入到testB表

(4)创建表的过程中从其他表导入

[java]  view plain  copy
  1. hive> create table testC as select name, code from testB;  
  2. Total jobs = 3  
  3. Launching Job 1 out of 3  
  4. Number of reduce tasks is set to 0 since there's no reduce operator  
  5. Starting Job = job_1449746265797_0106, Tracking URL = http://hadoopcluster79:8088/proxy/application_1449746265797_0106/  
  6. Kill Command = /home/hadoop/apache/hadoop-2.4.1/bin/hadoop job  -kill job_1449746265797_0106  
  7. Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 0  
  8. 2015-12-24 16:40:17,981 Stage-1 map = 0%,  reduce = 0%  
  9. 2015-12-24 16:40:23,115 Stage-1 map = 100%,  reduce = 0%, Cumulative CPU 1.11 sec  
  10. MapReduce Total cumulative CPU time: 1 seconds 110 msec  
  11. Ended Job = job_1449746265797_0106  
  12. Stage-4 is selected by condition resolver.  
  13. Stage-3 is filtered out by condition resolver.  
  14. Stage-5 is filtered out by condition resolver.  
  15. Moving data to: hdfs://hadoop2cluster/tmp/hive-root/hive_2015-12-24_16-40-09_983_6048680148773453194-1/-ext-10001  
  16. Moving data to: hdfs://hadoop2cluster/home/hadoop/hivedata/warehouse/testc  
  17. Table default.testc stats: [numFiles=1, numRows=0, totalSize=45, rawDataSize=0]  
  18. MapReduce Jobs Launched:   
  19. Job 0: Map: 1   Cumulative CPU: 1.11 sec   HDFS Read: 297 HDFS Write: 45 SUCCESS  
  20. Total MapReduce CPU Time Spent: 1 seconds 110 msec  
  21. OK  
  22. Time taken: 14.292 seconds  
  23. hive> desc testC;  
  24. OK  
  25. name                    string                                        
  26. code                    string                                        
  27. Time taken: 0.032 seconds, Fetched: 2 row(s)  

二,Hive数据导出的几种方式

(1)导出到本地文件系统

[java]  view plain  copy
  1. hive> INSERT OVERWRITE LOCAL DIRECTORY '/home/hadoop/output' ROW FORMAT DELIMITED FIELDS TERMINATED by ',' select * from testA;  
  2. Total jobs = 1  
  3. Launching Job 1 out of 1  
  4. Number of reduce tasks is set to 0 since there's no reduce operator  
  5. Starting Job = job_1451024007879_0001, Tracking URL = http://hadoopcluster79:8088/proxy/application_1451024007879_0001/  
  6. Kill Command = /home/hadoop/apache/hadoop-2.4.1/bin/hadoop job  -kill job_1451024007879_0001  
  7. Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 0  
  8. 2015-12-25 17:04:30,447 Stage-1 map = 0%,  reduce = 0%  
  9. 2015-12-25 17:04:35,616 Stage-1 map = 100%,  reduce = 0%, Cumulative CPU 1.16 sec  
  10. MapReduce Total cumulative CPU time: 1 seconds 160 msec  
  11. Ended Job = job_1451024007879_0001  
  12. Copying data to local directory /home/hadoop/output  
  13. Copying data to local directory /home/hadoop/output  
  14. MapReduce Jobs Launched:   
  15. Job 0: Map: 1   Cumulative CPU: 1.16 sec   HDFS Read: 305 HDFS Write: 110 SUCCESS  
  16. Total MapReduce CPU Time Spent: 1 seconds 160 msec  
  17. OK  
  18. Time taken: 16.701 seconds  

查看数据结果:

[java]  view plain  copy
  1. [hadoop@hadoopcluster78 output]$ cat /home/hadoop/output/000000_0   
  2. 1,fish1,SZ,2015-07-08  
  3. 2,fish2,SH,2015-07-08  
  4. 3,fish3,HZ,2015-07-08  
  5. 4,fish4,QD,2015-07-08  
  6. 5,fish5,SR,2015-07-08  
通过INSERT OVERWRITE LOCAL DIRECTORY将hive表testA数据导入到/home/hadoop目录,众所周知,HQL会启动Mapreduce完成,其实/home/hadoop就是Mapreduce输出路径,产生的结果存放在文件名为:000000_0。


(2)导出到HDFS

导入到HDFS和导入本地文件类似,去掉HQL语句的LOCAL就可以了

[java]  view plain  copy
  1. hive> INSERT OVERWRITE DIRECTORY '/home/hadoop/output' select * from testA;   
  2. Total jobs = 3  
  3. Launching Job 1 out of 3  
  4. Number of reduce tasks is set to 0 since there's no reduce operator  
  5. Starting Job = job_1451024007879_0002, Tracking URL = http://hadoopcluster79:8088/proxy/application_1451024007879_0002/  
  6. Kill Command = /home/hadoop/apache/hadoop-2.4.1/bin/hadoop job  -kill job_1451024007879_0002  
  7. Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 0  
  8. 2015-12-25 17:08:51,034 Stage-1 map = 0%,  reduce = 0%  
  9. 2015-12-25 17:08:59,313 Stage-1 map = 100%,  reduce = 0%, Cumulative CPU 1.4 sec  
  10. MapReduce Total cumulative CPU time: 1 seconds 400 msec  
  11. Ended Job = job_1451024007879_0002  
  12. Stage-3 is selected by condition resolver.  
  13. Stage-2 is filtered out by condition resolver.  
  14. Stage-4 is filtered out by condition resolver.  
  15. Moving data to: hdfs://hadoop2cluster/home/hadoop/hivedata/hive-hadoop/hive_2015-12-25_17-08-43_733_1768532778392261937-1/-ext-10000  
  16. Moving data to: /home/hadoop/output  
  17. MapReduce Jobs Launched:   
  18. Job 0: Map: 1   Cumulative CPU: 1.4 sec   HDFS Read: 305 HDFS Write: 110 SUCCESS  
  19. Total MapReduce CPU Time Spent: 1 seconds 400 msec  
  20. OK  
  21. Time taken: 16.667 seconds  

查看hfds输出文件:

[java]  view plain  copy
  1. [hadoop@hadoopcluster78 bin]$ ./hadoop fs -cat /home/hadoop/output/000000_0  
  2. 1fish1SZ2015-07-08  
  3. 2fish2SH2015-07-08  
  4. 3fish3HZ2015-07-08  
  5. 4fish4QD2015-07-08  
  6. 5fish5SR2015-07-08  

其他

采用hive的-e和-f参数来导出数据。

参数为: -e 的使用方式,后面接SQL语句。>>后面为输出文件路径

[java]  view plain  copy
  1. [hadoop@hadoopcluster78 bin]$ ./hive -e "select * from testA" >> /home/hadoop/output/testA.txt  
  2. 15/12/25 17:15:07 WARN conf.HiveConf: DEPRECATED: hive.metastore.ds.retry.* no longer has any effect.  Use hive.hmshandler.retry.* instead  
  3.   
  4. Logging initialized using configuration in file:/home/hadoop/apache/hive-0.13.1/conf/hive-log4j.properties  
  5. OK  
  6. Time taken: 1.128 seconds, Fetched: 5 row(s)  
  7. [hadoop@hadoopcluster78 bin]$ cat /home/hadoop/output/testA.txt   
  8. 1   fish1   SZ  2015-07-08  
  9. 2   fish2   SH  2015-07-08  
  10. 3   fish3   HZ  2015-07-08  
  11. 4   fish4   QD  2015-07-08  
  12. 5   fish5   SR  2015-07-08  

参数为:  -f  的使用方式,后面接存放sql语句的文件。 >> 后面为输出文件路径

SQL语句文件:

[java]  view plain  copy
  1. [hadoop@hadoopcluster78 bin]$ cat /home/hadoop/output/sql.sql   
  2. select * from testA  

使用-f参数执行:

[java]  view plain  copy
  1. [hadoop@hadoopcluster78 bin]$ ./hive -f /home/hadoop/output/sql.sql >> /home/hadoop/output/testB.txt  
  2. 15/12/25 17:20:52 WARN conf.HiveConf: DEPRECATED: hive.metastore.ds.retry.* no longer has any effect.  Use hive.hmshandler.retry.* instead  
  3.   
  4. Logging initialized using configuration in file:/home/hadoop/apache/hive-0.13.1/conf/hive-log4j.properties  
  5. OK  
  6. Time taken: 1.1 seconds, Fetched: 5 row(s)  

参看结果:

[java]  view plain  copy
  1. [hadoop@hadoopcluster78 bin]$ cat /home/hadoop/output/testB.txt   
  2. 1   fish1   SZ  2015-07-08  
  3. 2   fish2   SH  2015-07-08  
  4. 3   fish3   HZ  2015-07-08  
  5. 4   fish4   QD  2015-07-08  
  6. 5   fish5   SR  2015-07-08  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值