04-Hive数据导出

大家好!砸门又见面了。今天来玩一下Hive数据导出。
导出的方式有以下几种
1)hadoop命令的方式
get
text
2)通过insert…directory方式

insert overwrite[local] directory '/tmp/ca employees'
[row format delimited fields terminated by '\t']
select name,salary,address
from employees
样本代码

3)shell命令加管道:hive -f/e | sed/grep/awk>file
4)第三方工具,如sqoop

好,砸门开始实验吧!
1)hadoop命令的方式

hive> select * from testtext;
OK
wer	46
wer	89
weree	78
rr	89
Time taken: 0.212 seconds
hive> 
[root@hadoop1 host]# hadoop fs -get /user/hive/warehouse/testtext /usr/host/data2/ 
[root@hadoop1 host]# cd data2
[root@hadoop1 data2]# ll
total 4
drwxr-xr-x. 2 root root 4096 Jun  2 02:23 testtext
[root@hadoop1 data2]# 
[root@hadoop1 data2]# hadoop fs -text /user/hive/warehouse/testtext/*
wer	46
wer	89
weree	78
rr	89
[root@hadoop1 data2]# 

注意:我的hive保存在hdfs上的路径是/user/hive/warehouse,这是在配置hive-site.xml文件时指定的。
当然还可以重定向:

[root@hadoop1 data2]# hadoop fs -text /user/hive/warehouse/testtext/* > newdata2
[root@hadoop1 data2]# ll
total 8
-rw-r--r--. 1 root root   29 Jun  2 02:26 newdata2
drwxr-xr-x. 2 root root 4096 Jun  2 02:23 testtext
[root@hadoop1 data2]# cat newdata2 
wer	46
wer	89
weree	78
rr	89
[root@hadoop1 data2]#

(两个大于号是追加,一个大于号是重写覆盖)
2)通过insert…directory方式

hive> insert overwrite local directory '/usr/host/data3'
    > select name,addr
    > from testtext;
Total MapReduce jobs = 1
Launching Job 1 out of 1
Number of reduce tasks is set to 0 since there's no reduce operator
Job running in-process (local Hadoop)
Hadoop job information for null: number of mappers: 1; number of reducers: 0
2016-06-02 02:32:44,084 null map = 0%,  reduce = 0%
2016-06-02 02:32:56,469 null map = 100%,  reduce = 0%, Cumulative CPU 0.83 sec
2016-06-02 02:32:57,543 null map = 100%,  reduce = 0%, Cumulative CPU 0.83 sec
2016-06-02 02:32:58,658 null map = 100%,  reduce = 0%, Cumulative CPU 0.83 sec
MapReduce Total cumulative CPU time: 830 msec
Ended Job = job_1464828076391_0014
Execution completed successfully
Mapred Local Task Succeeded . Convert the Join into MapJoin
Copying data to local directory /usr/host/data3
Copying data to local directory /usr/host/data3
OK
Time taken: 29.525 seconds
hive> 

查一下data3文件夹

[root@hadoop1 host]# cd data3
[root@hadoop1 data3]# ll
total 4
-rw-r--r--. 1 root root 29 Jun  2 02:32 000000_0
[root@hadoop1 data3]# cat 000000_0 
wer46
wer89
weree78
rr89
[root@hadoop1 data3]# 

下载到hdfs上,则去掉local,不需要格式row…

hive> insert overwrite directory '/data3' 
    > select name,addr
    > from testtext;
Total MapReduce jobs = 1
Launching Job 1 out of 1
Number of reduce tasks is set to 0 since there's no reduce operator
Job running in-process (local Hadoop)
Hadoop job information for null: number of mappers: 1; number of reducers: 0
2016-06-02 02:37:27,137 null map = 0%,  reduce = 0%
2016-06-02 02:37:34,847 null map = 100%,  reduce = 0%
Ended Job = job_1464828076391_0015
Execution completed successfully
Mapred Local Task Succeeded . Convert the Join into MapJoin
Moving data to: /data3
OK
Time taken: 27.902 seconds
hive> 

真是百发百中,屡试不爽呀!

3)shell命令加管道:
hive -f/e | sed/grep/awk>file

[root@hadoop1 data3]# hive -e "select * from testtext"
OK
wer	46
wer	89
weree	78
rr	89
Time taken: 5.879 seconds
[root@hadoop1 data3]# hive -S -e "select * from testtext" | grep wer
wer	89
weree	78
[root@hadoop1 data3]# 

加-S的好处是控制台上可以少很多信息。

有些累了,休息一会。如果你看到此文,想进一步学习或者和我沟通,加我微信公众号:名字:谢华东 。

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

当法律与事业相遇

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值