Hive学习(六)DML数据操作

目录

数据导入

装载数据(Load)

通过查询语句向表中插入数据(Insert)

查询语句中创建表并加载数据(As Select)

数据导出

清除表中数据


数据导入

装载数据(Load)

语法:

load data [local] inpath 'file_path' [overwrite] into table tb_name [partition(partcol1=val1,...)]
  1. load data:表示加载数据

  2. local:表示从本地加载数据到 hive 表;否则从 HDFS 加载数据到 hive 表

  3. inpath:表示加载数据的路径

  4. overwrite:表示覆盖表中已有数据,否则表示追加

  5. into table:表示加载到哪张表

  6. tb_name:表示具体的表

  7. partition:表示上传到指定分区

案例,加载本地数据到student表中:

hive> load data local inpath '/opt/module/datas/student.txt' into table student_part partition(dt='201912');
Loading data to table iceink.student_part partition (dt=201912)
Partition iceink.student_part{dt=201912} stats: [numFiles=2, numRows=0, totalSize=96, rawDataSize=0]
OK

通过查询语句向表中插入数据(Insert)

-- 基本插入数据
insert into table student_part partition(dt='201909') values(1,'posion',20);
​
-- 根据单张表查询结果
insert overwrite table student_part partition(dt='201909') select id,name,age from student_part where dt = '201911';

查询语句中创建表并加载数据(As Select)

create table if not exists student_copy2 as select id,name,age from student;

数据导出

数据导出有很多种方式,如:Insert导出、Hadoop命令导出、Hive Shell导出、Export导出、Sqoop导出

下面主要介绍Insert导出。

-- 将查询结果导出到本地
insert overwrite local directory '/opt/module/datas/student' select * from student;
​
结果数据:
1iceink18
2icydate18
​
-- 将查询结果格式化导出到本地
insert overwrite local directory '/opt/module/datas/student' row format delimited fields terminated by '\t' select * from student;
​
结果数据:
1   iceink  18
2   icydate 18
​
-- 将查询结果导出到HDFS上,没有local
insert overwrite directory '/opt/module/datas/student' select * from student;

清除表中数据

语法: truncate table tb_name;

注意:

Truncate 只能删除管理表,不能删除外部表中数据

hive> truncate table student_external; FAILED: SemanticException [Error 10146]: Cannot truncate non-managed table student_external.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值