Hive中数据的导入与导出的几种方式

数据的导入:
1.向已经建立好的表中加载数据:
语法: load data [local] inpath ‘/opt/module/datas/student.txt’ [overwrite] | into table tablename [partition (partcol1=val1,…)];
中括号代表可以省略。

参数意义:
local: 表示从本地加载数据到hive表;否则从HDFS加载数据到hive表。
overwrite:表示覆盖表中已有数据,否则表示追加。
partition:表示上传到指定分区。

2.直接插入数据或者是通过查询语句向表中插入数据:
1)直接插入数据:
insert into table tab1 values(101,‘tom’);
2)通过查询语句向表中插入数据:
insert into table tab1 select * from tab2;
2)通过查询语句向表中追加数据:
insert overwrite table tab1 select * from tab2;
(这时从表2查询的字段数必须和表1的字段数相同)
带有分区的表:
insert overwrite table student partition(month=‘201708’)
select id, name from student where month=‘201709’;

3.查询语句中创建表并加载数据(As Select
根据查询结果创建表(查询的结果会添加到新创建的表中)
create table if not exists test3 as select id,name from test1;

4.创建表时通过Location指定加载数据路径
1)创建表,并指定在hdfs上的位置
create table if not exists test3(id int ) row format delimited fields terminated by ‘\t’ location ‘/mo/hive/test3’;(注意这里还要在hive的里面再加一个目录才行,不加的话建表以后看不到表)
2).上传数据到hdfs上
dfs -put /demo/b.txt /mo/hive/test7;(注意这里必须是dfs ,前面加一个hdfs反而会报错)
3).查询数据

5.Import数据到指定Hive表中。

数据的导出:

1.Insert导出
1).将查询的结果导出到本地
insert overwrite local directory ‘/opt/module/datas/export/student’
select * from student; (不加local的话导出到hdfs文件系统中去)

在这里插入图片描述
2)将查询的结果格式化导出到本地(对于1来说,导出的数据不会按照我们想要的格式给我们整理好,需要我们手动指明如何格式化数据)

在这里插入图片描述
其实Insert导出数据有一个小坑,链接地址。

2.通过Hadoop命令导出到本地。
dfs -get /user/hive/warehouse/test3/000000_0
/demo/hive/aaa.txt;

3.使用 Hive Shell 命令导出。
基本语法:(hive -f/-e 执行语句或者脚本 > file)

bin/hive -e ‘select * from default.student;’ >
/opt/module/datas/export/student4.txt;

4.Export导出到HDFS上
export table default.student to
‘/user/hive/warehouse/export/student’;

5.Sqoop导出

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值