Hive 3.x|第六七天|DML数据操作

数据导入

语法

load data [local] inpath '数据的 path' [overwrite] into table 
student [partition (partcol1=val1,…)];

(1)load data:表示加载数据
(2)local:表示从本地加载数据到 hive 表;否则从 HDFS 加载数据到 hive 表
(3)inpath:表示加载数据的路径
(4)overwrite:表示覆盖表中已有数据,否则表示追加
(5)into table:表示加载到哪张表
(6)student:表示具体的表
(7)partition:表示上传到指定分区

实操

  1. 创建一张表:studetne 设定由\t分割每行
create table student(id string, name string) row format delimited fields terminated by '\t';
  1. 加载本地文件到 hive
load data local inpath '/opt/module/hive/datas/student.txt' into table default.student;

其中student.txt的内容为

1001	buyongyong
1002	xiaosu
1003	ban

在这里插入图片描述

  1. 或者上传文件到 HDFS
hive (default)> dfs -put /opt/module/hive/data/student.txt /user/hive/warehouse;

再加载HDFS上的数据

load data inpath '/user/hive/warehouse/student.txt' into table default.student;
  1. 加载数据覆盖表中已有的数据
load data inpath '/user/hive/warehouse/student.txt' overwrite into table default.student;

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

  1. 创建一张表
create table student_par(id int, name string) row format delimited fields terminated by '\t';
  1. 基本插入数据
insert into table student_par values(1,'wangwu'),(2,'zhaoliu');
  1. 基本模式插入(根据单张表查询结果)
    从student里查出来,往student_par里放
insert overwrite table student_par select id, name from student where month='201709';
  1. 多表(多分区)插入模式(根据多张表查询结果)
 from student
 insert overwrite table student partition(month='201707')
 select id, name where month='201709'
 insert overwrite table student partition(month='201706')
 select id, name where month='201709';

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

根据查询结果创建表(查询的结果会添加到新创建的表中)

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

创建表时通过 Location 指定加载数据路径

  1. 上传数据到 hdfs 上
hive (default)> dfs -mkdir /student;
hive (default)> dfs -put /opt/module/datas/student.txt /student;
  1. 创建表,并指定在 hdfs 上的位置
hive (default)> create external table if not exists student5(id int, name string) row format delimited fields terminated by '\t' location '/student;
  1. 查询数据
hive (default)> select * from student5;

Import 数据到指定 Hive 表中

注意:先用 export 导出后,再将数据导入。

hive (default)> import table student2 from '/user/hive/warehouse/export/student';

数据导出

Insert 导出

  1. 将查询的结果导出到本地
hive (default)> insert overwrite local directory '/opt/module/hive/data/export/student' select * from student;
  1. 将查询的结果格式化导出到本地
insert overwrite local directory  '/opt/module/hive/data/export/student1' ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' select * from student;
  1. 将查询的结果导出到 HDFS 上(没有 local)
hive (default)> insert overwrite directory '/user/atguigu/student2' ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'  select * from student;

Hadoop 命令导出到本地

hive (default)> dfs -get /user/hive/warehouse/student/student.txt /opt/module/data/export/student3.txt;

Hive Shell 命令导出

基本语法:(hive -f/-e 执行语句或者脚本 > file)

[yjr@hadoop102 hive]$ bin/hive -e 'select * from default.student;' > /opt/module/hive/data/export/student4.txt;

Export 导出到 HDFS 上

export 和 import 主要用于两个 Hadoop 平台集群之间 Hive 表迁移。

export table default.student to '/user/hive/warehouse/export/student';

清除表中数据(Truncate)

 truncate table student;

基本查询

感觉很多和SQL一模一样,一模一样的就全跳过了先。
注意:

  1. SQL 语言大小写不敏感。
  2. SQL 可以写在一行或者多行
  3. 关键字不能被缩写也不能分行
  4. 各子句一般要分行写。
  5. 使用缩进提高语句的可读性。

全表和特定列查询

全表查询

hive (default)> select * from emp;
hive (default)> select empno,ename,job,mgr,hiredate,sal,comm,deptno from emp ;

选择特定列查询

hive (default)> select empno, ename from emp;

Like 和 RLike

  1. 使用 LIKE 运算选择类似的值
  2. 选择条件可以包含字符或数字:% 代表零个或多个字符(任意个字符);_ 代表一个字符。
  3. RLIKE 子句:RLIKE 子句是 Hive 中这个功能的一个扩展,其可以通过 Java 的正则表达式这个更强大的语言来指定匹配条件。
hive (default)> select * from emp where ename RLIKE '[A]';

满外连接

满外连接:将会返回所有表中符合 WHERE 语句条件的所有记录。如果任一表的指定字段没有符合条件的值的话,那么就使用 NULL 值替代。

hive (default)> 
select e.empno, e.ename, d.deptno 
from
emp as e
full join dept as d
on e.deptno = d.deptno;

多表连接

注意:连接 n 个表,至少需要 n-1 个连接条件。例如:连接三个表,至少需要两个连接条件。

hive (default)>
SELECT e.ename, d.dname, l.loc_name
FROM emp e 
JOIN dept d
ON d.deptno = e.deptno 
JOIN location l
ON d.loc = l.loc;

大多数情况下,Hive 会对每对 JOIN 连接对象启动一个 MapReduce 任务。
本例中会首先启动一个 MapReduce job 对表 e 和表 d 进行连接操作,然后会再启动一个 MapReduce job 将第一个 MapReduce job 的输出和表 l;进行连接操作。
优化:当对 3 个或者更多表进行 join 连接时,如果每个 on 子句都使用相同的连接键的话,那么只会产生一个 MapReduce job。

全局排序

Order By:全局排序,只有一个 Reducer

每个 Reduce 内部排序(Sort By)

Sort By:对于大规模的数据集 order by 的效率非常低。在很多情况下,并不需要全局排序,此时可以使用 sort by。
Sort by 为每个 reducer 产生一个排序文件。每个 Reducer 内部进行排序,对全局结果集来说不是排序。

  1. 设置 reduce 个数:hive (default)> set mapreduce.job.reduces=3;
  2. 查看设置 reduce 个数:hive (default)> set mapreduce.job.reduces;
  3. 根据部门编号降序查看员工信息:hive (default)> select * from emp sort by deptno desc;
  4. 将查询结果导入到文件中(按照部门编号降序排序):
insert overwrite local directory 
'/opt/module/data/sortby-result'
select * from emp sort by deptno desc;

分区(Distribute By)

  • Distribute By: 在有些情况下,我们需要控制某个特定行应该到哪个 reducer,通常是为了进行后续的聚集操作。
  • distribute by 子句可以做这件事。distribute by 类似 MR 中 partition
    (自定义分区),进行分区,结合 sort by 使用。

对于 distribute by 进行测试,一定要分配多 reduce 进行处理,否则无法看到 distribute by 的效果。

案例:先按照部门编号分区,再按照员工编号降序排序。Hive 要求 DISTRIBUTE BY 语句要写在 SORT BY 语句之前

hive (default)> set mapreduce.job.reduces=3;
hive (default)> insert overwrite local directory '/opt/module/data/distribute-result' 
select * from emp distribute by deptno sort by empno desc;

Cluster By

当 distribute by 和 sorts by 字段相同时,可以使用 cluster by 方式。
cluster by 除了具有 distribute by 的功能外还兼具 sort by 的功能。但是排序只能是升序排序,不能指定排序规则为 ASC 或者 DESC。
故以下两种写法等价:

hive (default)> select * from emp cluster by deptno;
hive (default)> select * from emp distribute by deptno sort by deptno;

注意:按照部门编号分区,不一定就是固定死的数值,可以是 20 号和 30 号部门分到一个分区里面去。

四个排序总结

order by:只有一个Reducer,不能用于生产排序。
sort by:区内排序,单独使用时会随机划分所在区。
distribute by:帮助sort by进行分区,进行区内排序。
Cluster By:sort by和distribute by字段相同,且升序排列。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值