文章目录
Hive的DML语言
1、数据导入
①向表中装载数据
语法:
load data [local] inpath '数据的path' [overwrite] into table table_name [partition (partcol1=val1,…)];
命令参数说明
参数 | 说明 |
---|---|
Load data | 加载数据 |
Local | 表示从本地加载数据到hive表,否则是从HDFS加载数据到Hive表 |
Inpath | 表是加载数据的路径 |
Overwrite | 表示覆盖表中已有数据,否则表示追加 |
Into table | 表示加载数据到哪张表中 |
Partition | 表示加载数据到指定分区 |
1)创建一张表
create table student(
id string,
name string
)
row format delimited
fields terminated by '\t';
2)加载本地文件到hive
load data local inpath '/opt/module/hive/datas/student.txt' into table test.student;
3)查询数据是否load成功
②从hdfs加载到hive表中
(1)首先将文件上传到HDFS中
dfs -put /opt/module/hive/datas/student.txt /input;
(2)将HDFS文件加载到hive表中
load data inpath '/input/student.txt' into table test.student;
(3)覆盖数据
load data inpath '/input/student.txt' overwrite into table test.student;
注意:从hdfs上加载数据到hive表中,类似于拷贝的方式,所以在加载之后数据已经被拷贝走了,这个时候再次执行加载就会报错,需要重新上传数据到hdfs上
dfs -put /opt/module/hive/datas/student.txt /input;
load data inpath '/input/student.txt' overwrite into table test.student;
③通过查询向hive表中装载数据
insert overwrite table student2 select id, name from student ;
1)首先创建student2表
create table student2(
id string,
name string
)
row format delimited
fields terminated by '\t';
2)基本的插入数据
insert into table student2 values(1,'wangwu'),(2,'zhaoliu');
3)根据查询结果插入数据
insert overwrite table student2 select id, name from student;
查看插入结果
insert into 和 insert overwrite的区别
Insert into: | 以追加数据的方式插入到表或分区,原有数据不会删除 |
---|---|
Insert overwrite: | 会覆盖表中已存在的数据 |
insert不支持插入部分字段
4)多表插入模式
from student
insert overwrite table student2
select id, name
insert overwrite table student3
select id, name;
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eJTKxy48-1640059477245)(C:\Users\lenovo\AppData\Roaming\Typora\typora-user-images\image-20211219191510853.png)]
④查询语句中创建表并加载数据(As Select)
在创建表的时候通过查询结果装载入表
create table if not exists student4
as select
id, name
from student;
⑤创建表时通过Location指定加载数据路径
创建时指定表的存储位置
create external table if not exists student5(
id int, name string
)
row format delimited fields terminated by '\t'
location '/input/student';
⑥Import数据到指定Hive表中
2、数据导出
①Insert导出
1)将查询的结果导出到本地
insert overwrite local directory '/opt/module/hive/datas/export/student'
select * from student;
查看导出的文件
2)上面这种情况导出的数据都挤在了一起,还可以格式化导出文件
insert overwrite local directory '/opt/module/hive/datas/export/student1'
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\t'
select * from student;
查看文件
3)还可以将查询的结果导出到hdfs上,只需要去掉语句中的lcoal即可
insert overwrite directory '/output/student'
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\t'
select * from student;
②Hadoop命令导出到本地
直接将hive存放在hdfs上的文件下载到本地即可
dfs -get /user/hive/warehouse/student/student.txt /opt/module/hive/datas/export/student3.txt;
③Hive Shell 命令导出
hive shell后面的参数加上e可以直接在命令窗口执行hql语句
bin/hive -e 'select * from test.student;' > /opt/module/hive/datas/export/student4.txt;
查看文件
④Export导出到HDFS上
export table test.student to '/user/hive/warehouse/export/student';
export和import主要用于两个Hadoop平台集群之间Hive表迁移
⑤sqoop导出
⑥DataX导出
3、清除表中的数据Truncate
注意:truncate只能清除管理表的数据,不可以清除外部表的数据
truncate table student4;