栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

03:Hive从0到1系列学习:DML数据操作语言

03:Hive从0到1系列学习:DML数据操作语言

文章目录
    • Hive的DML语言
      • 1、数据导入
        • ①向表中装载数据
        • ②从hdfs加载到hive表中
        • ③通过查询向hive表中装载数据
        • ④查询语句中创建表并加载数据(As Select)
        • ⑤创建表时通过Location指定加载数据路径
        • ⑥import数据到指定Hive表中
      • 2、数据导出
        • ①Insert导出
        • ②Hadoop命令导出到本地
        • ③Hive Shell 命令导出
        • ④Export导出到HDFS上
      • 3、清除表中的数据Truncate

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:UserslenovoAppDataRoamingTyporatypora-user-imagesimage-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;

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/677081.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号