DML
# DML
# 数据导入
load data [local] inpath '/opt/module/datas/student.txt' [overwrite] into table student [partition (partcol1=val1,…)];
1
- load data: 表示加载数据
- local: 表示从本地加载数据到 hive 表;否则从 HDFS 加载数据到 hive 表
- inpath: 表示加载数据的路径
- overwrite: 表示覆盖表中已有数据,否则表示追加
- into table: 表示加载到哪张表
- student: 表示具体的表
- partition: 表示上传到指定分区
load data local inpath "/opt/module/datas/student.txt" into table stu_par; -- 从宿机加载数据
load data local inpath "/opt/module/datas/student.txt" overwrite into table stu_par; -- 覆盖导入
load data inpath "/opt/module/datas/student.txt" overwrite into table stu_par; -- 从hdfs中加载指定路径文件 HDFS的导入是移动文件,而本地导入是复制上传
1
2
3
2
3
- insert 导入
insert into table stu_par partition(month='201709') values(1,'wangwu') -- 插入单条 一个括号对应一条
insert into table stu_par partition(month='201709') values(1,'wangwu'),(2,'zhaoliu'); -- 插入多条指定数据 多行数据用逗号隔开并用括号包裹 要类型一致
insert into table stu_par select id,name from stu_par2 where class="01"; -- 插入查询后的数据
1
2
3
2
3
- 建表时 as select 导入
create table if not exists student3
as select id, name from student;
1
2
2
- 建表时通过 location 加载
create external table if not exists student4
(id int, name string)
row format delimited fields terminated by '\t'
location '/student';
1
2
3
4
2
3
4
# 数据导出
# Insert 导出
- 将查询的结果导出到本地 默认不带格式
insert overwrite local directory '/opt/module/datas/export/student' select * from student;
1
- 将查询的结果格式化导出到本地
insert overwrite local directory '/opt/module/datas/export/student1' ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' select * from student;
1
# Hive Shell 命令导出
用 shell 命令查询表并写出本地
如果不指定库 默认为 default 库
hive -e 'select * from default.student;' > /opt/module/datas/export/student4.txt;
1
# Export 导出到 HDFS 上
将表导出到 HDFS 上 元数据和表数据文件
export table default.student to '/user/hive/warehouse/export/student';
1
# Import 数据导入到 hive 表中
要先用 export 导出到 hdfs 中 必须包含元数据和表数据
import table student from '/export/student';
1
# 数据删除
清空表 只删除表数据 不删除表本身 Truncate 只能删除管理表,不能删除外部表中数据
truncate table student;
1
编辑 (opens new window)
上次更新: 2023/12/06, 01:31:48