首页 > 其他分享 >hive 的order by ,sort by,distribute by,cluster by

hive 的order by ,sort by,distribute by,cluster by

时间:2023-08-10 21:55:15浏览次数:41  
标签:sort NULL distribute hive cluster 排序 tb

order by

order by会对输入做全局排序,因此只有一个Reducer(多个Reducer无法保证全局有序),然而只有一个Reducer,会导致当输入规模较大时,消耗较长的计算时间,在生产环境中遇到数据量较大的情况,一般无法成功。

sort by

sort by不是全局排序,其在数据进入reducer前完成排序,因此,如果用sort by进行排序,并且设置mapreduce.job.reduces 多于1个,则sort by只会保证每个reducer的输出有序,但不保证全局有序。

sort by的数据只能保证在同一个reduce中的数据可以按指定字段排序。使用sort by你可以指定执行的reduce个数(通过set mapreduce.job.reduces=n来指定),对输出的数据再执行归并排序,即可得到全部结果。

distribute by

distribute by是控制在map端如何拆分数据给reduce端的。hive会根据distribute by后面列,对应reduce的个数进行分发,默认是采用hash算法。sort by为每个reduce产生一个排序文件。

在有些情况下,你需要控制某个特定行应该到哪个reducer,这通常是为了进行后续的聚集操作。distribute by刚好可以做这件事。因此,distribute by经常和sort by配合使用。

注意

distribute by 的分区规则是根据分区字段的 hash 码与 reduce 的个数进行模除后, 余数相同的分到一个区,也就意味着同一个分区中的分区字段不一定相同。

Hive 要求 distribute by 语句要写在 sort by 语句之前,因为,sort by 是对分区中排序

cluster by

当 distribute by 和 sorts by 字段相同时,可以使用 cluster by 方式。

cluster by 除了具有 distribute by 的功能外还兼具 sort by 的功能。但是排序只能是升序排序,不能指定排序规则为 ASC 或者 DESC。

在分区和排序字段相同的前提下,他等价于 distribute by 和sort by 的一个简写方式。

 

演练

数据和建表

tb_loc(地域表)

2001    NewYork
2002    Boston
2003    Dallas
2004    Chicago
create table tb_loc(
loc int,
locname string
)
row format delimited fields terminated by '\t';

tb_dept(部门表)

10  ACCOUNTING  2001
20  RESEARCH    2002
30  SALES   2003
40  OPERATIONS  2004
create table tb_dept(
deptno int,
dname string,
loc int
)
row format delimited fields terminated by '\t';

tb_emp(员工表)

7369    SMITH   CLERK   7902    1980-12-17  800.00  NULL    20
7499    ALLEN   SALESMAN    7698    1981-02-20  1600.00 300.00  30
7521    WARD    SALESMAN    7698    1981-02-22  1250.00 500.00  30
7566    JONES   MANAGER 7839    1981-04-02  2975.00 NULL    20
7654    MARTIN  SALESMAN    7698    1981-09-28  1250.00 1400.00 30
7698    BLAKE   MANAGER 7839    1981-05-01  2850.00 NULL    30
7782    CLARK   MANAGER 7839    1981-06-09  2450.00 NULL    10
7788    SCOTT   ANALYST 7566    1987-04-19  3000.00 NULL    20
7839    KING    PRESIDENT   NULL    1981-11-17  5000.00 NULL    10
7844    TURNER  SALESMAN    7698    1981-09-08  1500.00 0.00    30
7876    ADAMS   CLERK   7788    1987-05-23  1100.00 NULL    20
7900    JAMES   CLERK   7698    1981-12-03  950.00  NULL    30
7902    FORD    ANALYST 7566    1981-12-03  3000.00 NULL    20
7934    MILLER  CLERK   7782    1982-01-23  1300.00 NULL    10
7934    TOM CLERK   7782    1982-01-23  1200.00 NULL    50
7934    JACK    CLERK   7782    1982-01-23  1100.00 NULL    60

 

create table tb_emp(
empno int,
ename string,
job string,
mgr int,
hiredate string,
sal float,
comm float,
deptno int
)
row format delimited fields terminated by '\t';

导入数据

load data local inpath '/hivedata/dept.txt' into table tb_dept;
load data local inpath '/hivedata/emp.txt' into table tb_emp;
load data local inpath '/hivedata/loc.txt' into table tb_loc;

Order By

-- 全局排序,只有一个reduce
hive (default)> select * from tb_epm order by sal;
​
-- 按照别名排序
hive (default)> select ename,sal*2 as 2sal from tb_emp order by 2sal;
​
-- 多个列排序
hive (default)> select ename, deptno, sal from tb_emp order by deptno, sal;

 

每个 Reduce 内部排序(Sort By)

-- 设置 reduce 个数
hive (default)> set mapreduce.job.reduces=3;
​
-- 查看设置 reduce 个数
hive (default)> set mapreduce.job.reduces;
​
-- 每个reduce内部按照deptno 降序排列
hive (default)> select * from tb_emp sort by deptno desc;
​
-- 将查询结果导入到文件中
hive (default)> insert overwrite local directory 
'/hivedata/sortby-result'
select * from tb_emp sort by deptno desc;

 

Distribute By

--先按照部门编号分区,再按照员工编号降序排序
hive (default)> set mapreduce.job.reduces=3; 
hive (default)> insert overwrite local directory
'/hivedata/distribute-result' 
select * from tb_emp distribute by deptno sort by empno desc;

distribute by 的分区规则是根据分区字段的 hash 码与 reduce 的个数进行模除后, 余数相同的分到一个区。

注意:DISTRIBUTE BY 语句要写在 SORT BY 语句之前

 

Cluster By

当 distribute by 和 sorts by 字段相同时,并且排序为升序时,可以使用 cluster by 方式。

cluster by = distribute by + sorts by

只能是升序排序,不能指定排序规则为 ASC 或者 DESC。

-- 下面这两个的结果是一样的
-- 满足条件,即cluster by 和 sort by 的字段相同,并且是升序排列
hive (default)> select * from tb_emp cluster by deptno;
hive (default)> select * from tb_emp distribute by deptno sort by deptno;

标签:sort,NULL,distribute,hive,cluster,排序,tb
From: https://www.cnblogs.com/ExMan/p/17621645.html

相关文章

  • Python列表排序sort和sorted的区别
    lst_1=[3,1,2]lst_2=[4,6,5]print(lst_1.sort())lst_1.sort()print(lst_1)print(sorted(lst_2))#None#[1,2,3]#[4,5,6]所以结论就是:sorted(list)原来的list不变,生成一个新的排好序的list对象。list.sort()改变原有的list,不会返回对象。......
  • 使用 Kafka Tools(现已更名为 Offeset Exploer)无法连接虚拟机的 Kafka 集群,报错error c
    发生缘由学习Kafka的使用,结果发现使用KafkaTools(现已更名为OffesetExploer)无法连接虚拟机的Kafka集群,报错信息:errorconnectingtothecluster.unabletoconnecttozookeeperserverxxx.xxx.xxx.xxx2181withtimeoutof10000ms运行环境电脑系统版本:Windows1......
  • 条条大路通罗马系列—— 使用 Hiredis-cluster 连接 Amazon ElastiCache for Redis 集
    前言AmazonElastiCacheforRedis是速度超快的内存数据存储,能够提供亚毫秒级延迟来支持实时应用程序。适用于Redis的ElastiCache基于开源Redis构建,可与RedisAPI兼容,能够与Redis客户端配合工作,并使用开放的Redis数据格式来存储数据。适用于Redis的ElastiCache......
  • php多维数组自定义排序 uasort()
    对数组进行排序PHP有一些用来排序数组的函数,这个文档会把它们列出来。主要区别有:有些函数基于array的键来排序,而其他的基于值来排序的:$array['key']='value';。排序之后键和值之间的关联关系是否能够保持,是指排序之后数组的键可能会被重置为数字型的(0,1,2...)。排......
  • 笔记 | Sort 的实现逻辑与排序算法
    一、SortSort()的功能是对数组元素就地进行排序,会改变数组本身(返回对象同数组的引用)。默认排序顺序是,先将元素转换为字符串后进行排序。有一个可选参数compareFunction定义排序顺序的函数。返回值应该是一个数字,其正负性表示两个元素的相对顺序。array.sort([compareFunct......
  • CF1682B AND Sorting 题解
    首先,我们按照题意,可以用0来作为中间的一个数来交换其他两个数,这种元素肯定是有的,那就是所有不在正确位置上的所有数的AND值,我们可以开一个数组a来模拟这个过程,a_i&a_j=X,那这里的X就起到我们的0的作用了。代码:#include<bits/stdc++.h>#defineintlonglongusin......
  • k8s部署DataEase1.16.0cluster模式
    1.下载官方helm  chart包下载地址:https://github.com/mfanoffice/dataease-helm/releases,当前最新为1.16.0#下载并解压helmchart包wgethttps://github.com/mfanoffice/dataease-helm/releases/download/1.16.0/dataease-1.16.0.tgztarxfdataease-1.16.0.tgzcddataease......
  • rke up etcd报错: etcd cluster is unhealthy
    问题添加node,rkeup报错:WARN[0197][etcd]host[10.7.0.51]failedtochecketcdhealth:failedtoget/healthforhost[10.7.0.51]:Get"https://10.7.0.51:2379/health":remoteerror:tls:badcertificateWARN[0290][etcd]host[10.7.0.52]failedtoch......
  • Lua script attempted to access a non local key in a cluster node 问题解决
    一、问题描述最近优化公司需要对不同的业务系统的缓存工具提供一个标准化的解决方案。各个业务系统将缓存数据通过map结构进行存储,然后在缓存系统中将这些map获取出来,然后保存在redis数据库中。技术经理想到的最好解决方案是将map集合直接存储在redis的hash表中。但是要求对hash......
  • I - Wish I Knew How to Sort
    I-WishIKnewHowtoSort题意每次随机选择下标\(i,j\)交换\(a[i],a[j]\),求变成不讲序列的期望次数。思路dp,同样也是期望dp,先考虑暴力,可以状态压缩,那么\(010\)可以转移到:\(100\),\(010\),\(001\)这三种,然后我们发现,其实只有\(001\)有点用,而其他的就有点鸡肋,所以......