Hive的排序以及分桶抽样查询

版权声明:有一种生活不去经历不知其中艰辛,有一种艰辛不去体会,不会知道其中快乐,有一种快乐,没有拥有不知其中纯粹 https://blog.csdn.net/wwwzydcom/article/details/84202031

排序

全局排序(Order by) :一个MapReduce

使用order by子句查询

ASC(ascend):升序(默认)
DESC(descend):降序

Order by子句在Select语句结尾

查询员工信息按工资升序排列

 select * from emp order by sal;

查询员工信息按工资降序排列

	 select * from emp order by sal desc;

按照别名排序

按照员工薪水的 2 倍排序

hive (hive)> select ename,sal*2 twosal from emp order by twosal;

多个列排序

按照部门和工资升序排序

hive (hive)> select ename,deptno,sal from emp order by deptno,sal;

每个MapReduce内部排序(Sort By)

Sort By:每个MapReduce内部进行排序,对全局结果集来说不是排序

1)设置 reduce 个数

set mapreduce.job.reduces=3;

2)查看设置 reduce 个数

set mapreduce.job.reduces;

3)根据部门编号降序查看员工信息

select * from emp sort by empno desc;

4)将查询结果导入到文件中(按照部门编号降序排序)

扫描二维码关注公众号,回复: 4330631 查看本文章

分区排序(Distribute By)

Distribute By : 类似MR中partition,进行分区,结合Sort by使用

注意:Hive要求distribute by 语句要写在sort by语句之前
对于 distribute by 进行测试,一定要分配多 reduce 进行处理,否则无法看到 distribute by
的效果

(1)先按照部门编号分区,再按照员工编号降序排序。

hive (default)> set mapreduce.job.reduces=3;
hive (default)> insert overwrite local directory '/opt/module/datas/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。

select * from emp cluster by deptno;

等价于

select * from emp distribute by deptno sort by deptno;

分桶以及抽样查询

分桶表数据存储

分桶和分区的区别

分区针对的数据存储路径,分桶针对的是数据文件
分区提供一个隔离数据和优化查询的便利方式,不过并非所有的数据集都可以形成合理的分区,特别是确定合适的划分大小疑虑
分桶是将数据集分解成更容易管理的若干部分的另一个技术

原始数据

1001 ss1
1002 ss2
1003 ss3
1004 ss4
1005 ss5
1006 ss6
1007 ss7
1008 ss8
1009 ss9
1010 ss10
1011 ss11
1012 ss12
1013 ss13
1014 ss14
1015 ss15
1016 ss16

创建分桶表

create table stu_buck(id int,name string)
clustered by(id)
into 4 buckets
row format delimited fields terminated by '\t';

查看表的详细结构

hive (hive)> desc formatted stu_buck;
...
Num Buckets:        	4  

加载数据

hive (hive)> load data local inpath '/opt/datas/student.txt' into table stu_buck;

在这里插入图片描述
并没有将文件分开

因为没有设置参数

2)创建分桶表时,数据通过子查询的方式导入

(1)先建一个普通的 stu 表

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

(2)向普通的 stu 表中导入数据

hive (hive)> load data local inpath '/opt/datas/student.txt' into table stu;

(3)清空 stu_buck 表中数据

hive (hive)> truncate table stu_buck;

(4)设置属性开启分桶

hive (hive)> set hive.enforce.bucketing=true;
hive (hive)> set mapreduce.job.reduces=-1;

(5)重写写入数据

hive (hive)> insert into table stu_buck select id,name from stu cluster by (id);

在这里插入图片描述
这里可以看出分区是对于文件夹的操作,分桶是对文件的操作

分桶抽样查询

对于非常大的数据集,有时用户需要使用的是一个具有代表性的查询结果而不是全部结
果。Hive 可以通过对表进行抽样来满足这个需求

查询stu_buck中的数据

hive (hive)> select * from stu_buck tablesample(bucket 1 out of 4 on id);
OK
stu_buck.id	stu_buck.name
1004	ss4
1008	ss8
1012	ss12
1016	ss16
Time taken: 0.169 seconds, Fetched: 4 row(s)

tablesample是抽样语句,语法:tablesample(bucket x out of y)

y必须是table总bucket数的倍数或者因子,hive根据y的大小,决定抽样比例
table总共分成4份,当y=2时,抽取(4/2)=2个bucket的数据,当y=8时,抽取(4/8=)1/2个bucket的数据
x 表示从哪个 bucket 开始抽取。例如,table 总 bucket 数为 4,tablesample(bucket 4 out of4),表示总共抽取(4/4=)1 个 bucket 的数据,抽取第 4 个 bucket 的数据
注意:x必须小于y
否则

FAILED: SemanticException [Error 10061]: Numerator should not be bigger than
denominator in sample clause for table stu_buck

数据块抽样

Hive 提供了另外一种按照百分比进行抽样的方式,这种是基于行数的,按照输入路径
下的数据块百分比进行的抽样。

hive (hive)> select * from stu tablesample(0.1 percent);

提示:这种抽样方式不一定适用于所有的文件格式。另外,这种抽样的最小抽样单元是
一个 HDFS 数据块。因此,如果表的数据大小小于普通的块大小 128M 的话,那么将会返回
所有行。

猜你喜欢

转载自blog.csdn.net/wwwzydcom/article/details/84202031