Hive中的DML操作


Hive中的DML操作

DML是Data Manipulation Language的缩写,意思是数据操纵语言,是指在SQL语言中,负责对数据库对象运行数据访问工作的指令集,以INSERT、UPDATE、DELETE三种指令为核心,分别代表插入、更新与删除,是开发以数据为中心的应用程序必定会使用到的指令。

由于hive它主要用来进行海量数据的提取、转化、加载,,所以在Hive中的UPDATE、DELETE相关操作使用的场景比较少。

一、Load

Load语句可将文件导入到Hive表中。

1)语法

LOAD DATA [LOCAL] INPATH 'filepath' [OVERWRITE] INTO TABLE tablename [PARTITION (partcol1=val1, partcol2=val2 ...)];

关键字说明:

(1)local:表示从本地加载数据到Hive表;否则从HDFS加载数据到Hive表。

(2)overwrite:表示覆盖表中已有数据,否则表示追加。

(3)partition:表示上传到指定分区,若目标是分区表,需指定分区。

2)实操案例

(0)创建一张表
drop table if exists student;
create table student(
    id int, 
    name string
) 
row format delimited fields terminated by '\t';

image-20230608114014691

(1)加载本地文件到hive
load data local inpath '/opt/module/datas/student.txt' into table student;

查看数据:

image-20230608114405194

(2)加载HDFS文件到hive中

①上传文件到HDFS

hadoop fs -put /opt/module/datas/student.txt /user/root

②加载HDFS上数据,导入完成后去HDFS上查看文件是否还存在

load data inpath '/user/root/student.txt' into table student;

image-20230608114759458

查看数据文件/user/root/student.txt,发现文件不在了。

数据追加写入:

select * from student;

image-20230608115013155

(3)加载数据覆盖表中已有的数据

①上传文件到HDFS

hadoop fs -put /opt/module/datas/student.txt /user/root

②加载数据覆盖表中已有的数据

load data inpath '/user/root/student.txt' overwrite into table student;

image-20230608115213690

二、Insert

1. 将查询结果插入表中

1)语法
INSERT (INTO | OVERWRITE) TABLE tablename [PARTITION (partcol1=val1, partcol2=val2 ...)] select_statement;

关键字说明:

(1)INTO:将结果追加到目标表

(2)OVERWRITE:用结果覆盖原有数据

(3)PARTITION:分区

2)案例

(1)新建一张表

create table student1(
    id int,
    name string
)
row format delimited fields terminated by '\t';

image-20230608120006781

(2)根据查询结果插入数据

insert overwrite table student1
select id,
       name
from student;

image-20230608141800546

查看数据:

image-20230608141844463

2. 将给定Values插入表中

1)语法

INSERT (INTO | OVERWRITE) TABLE tablename [PARTITION (partcol1[=val1], partcol2[=val2] ...)] VALUES values_row [, values_row ...]

2)案例

insert into table  student1 values(1,'wangwu'),(2,'zhaoliu');

image-20230608141914043

查看数据:

image-20230608141936100

3. 将查询结果写入目标路径

1)语法
INSERT OVERWRITE [LOCAL] DIRECTORY directory
  [ROW FORMAT row_format] [STORED AS file_format] select_statement;
2)案例
insert overwrite local directory '/opt/module/datas/student' ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.JsonSerDe'
select id,name from student;

image-20230608142050092

查看结果:

image-20230608142124890

三、Export&Import

Export导出语句可将表的数据和元数据信息一并导处的HDFS路径,Import可将Export导出的内容导入Hive,表的数据和元数据信息都会恢复。Export和Import可用于两个Hive实例之间的数据迁移。

1)语法

--导出
EXPORT TABLE tablename TO 'export_target_path'

--导入
IMPORT [EXTERNAL] TABLE new_or_original_tablename FROM 'source_path' [LOCATION 'import_target_path']

2)案例

--导出
export table db_hive1.student to '/user/hive/warehouse/export/student';

--导入
import table student2 from '/user/hive/warehouse/export/student'

数据导出:

image-20230608142310578

查看:

image-20230608142433211

数据导入:

image-20230608142509109

查看数据:

image-20230608142538562

参考文章:

1.dml是什么(dml是什么意思) https://www.chx-zs.com/baike/298315.html
2.尚硅谷大数据Hive 3.x教程全新升级版(基于hive3.1.3)p28-p30
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

W_chuanqi

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

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

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

打赏作者

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

抵扣说明:

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

余额充值