Hive中order by,sort by,distribute by,cluster by的区别
一:order by
order by会对输入做全局排序,因此只有一个Reducer(多个Reducer无法保证全局有序),然而只有一个Reducer,会导致当输入规模较大时,消耗较长的计算时间。关于order by的详细介绍请参考这篇文章:Hive Order by操作。
二:sort by
sort by不是全局排序,其在数据进入reducer前完成排序,因此,如果用sort by进行排序,并且设置mapred.reduce.tasks>1,则sort by只会保证每个reducer的输出有序,并不保证全局有序。sort by不同于order by,它不受Hive.mapred.mode属性的影响,sort by的数据只能保证在同一个reduce中的数据可以按指定字段排序。使用sort by你可以指定执行的reduce个数(通过set mapred.reduce.tasks=n来指定),对输出的数据再执行归并排序,即可得到全部结果。
三:distribute by
distribute by是控制在map端如何拆分数据给reduce端的。hive会根据distribute by后面列,对应reduce的个数进行分发,默认是采用hash算法。sort by为每个reduce产生一个排序文件。在有些情况下,你需要控制某个特定行应该到哪个reducer,这通常是为了进行后续的聚集操作。distribute by刚好可以做这件事。因此,distribute by经常和sort by配合使用。
注:Distribute by和sort by的使用场景
1.Map输出的文件大小不均。
2.Reduce输出文件大小不均。
3.小文件过多。
4.文件超大。
四:cluster by
cluster by除了具有distribute by的功能外还兼具sort by的功能。但是排序只能是倒叙排序,不能指定排序规则为ASC或者DESC。
示例:
#sort by
- hive (hive)> select * from user;
- OK
- id name
- 1 lavimer
- 2 liaozhongmin
- 3 liaozemin
使用sort by按id降序排列:
- hive (hive)> select * from user sort by id desc;
- //MapReduce...
- Execution completed successfully
- Mapred Local Task Succeeded . Convert the Join into MapJoin
- OK
- id name
- 3 liaozemin
- 2 liaozhongmin
- 1 lavimer
- Time taken: 3.828 seconds
#distribute by
- hive (hive)> select * from user;
- OK
- id name
- 1 lavimer
- 2 liaozhongmin
- 3 liaozemin
- 100 hello
- 200 hadoop
#设置reduce的个数
- hive (hive)> set mapred.reduce.tasks=2;
- hive (hive)> set mapred.reduce.tasks;
- mapred.reduce.tasks=2
#使用带distribute by的数据从user表中导出数据
- hive (hive)> insert overwrite local directory '/usr/local/src/user.txt' select * from user distribute by id;
- //MapReduce...
- Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 2
注:从上述语句执行过程可以看到启动了两个Reducer。
#导出到本地的数据
- [root@liaozhongmin5 src]# cd user.txt/
- [root@liaozhongmin5 user.txt]# ll
- 总用量 8
- -rwxrwxrwx. 1 root root 36 1月 30 14:35 000000_0
- -rwxrwxrwx. 1 root root 22 1月 30 14:35 000001_0
- [root@liaozhongmin5 user.txt]# more 000000_0
- 2<span style="white-space:pre"> </span>liaozhongmin
- 100<span style="white-space:pre"> </span>hello
- 200<span style="white-space:pre"> </span>hadoop
- [root@liaozhongmin5 user.txt]# more 000001_0
- 1<span style="white-space:pre"> </span>lavimer
- 3<span style="white-space:pre"> </span>liaozemin
- [root@liaozhongmin5 user.txt]#
注:从上述结果中,我们可以看到数据被分发到了两个Reducer中处理。
#distribute by和sort by结合使用
- hive (hive)> select * from temperature;
- OK
- year tempra
- 2008 30`C
- 2008 35`C
- 2008 32.5`C
- 2008 31.5`C
- 2008 31`C
- 2015 41`C
- 2015 39`C
- 2015 36`C
- 2015 33`C
- 2015 35`C
- 2015 37`C
#根据年份和气温对气象数据进行排序,以确保所具有相同年份的行最终都在一个reduce分区中。
- hive (hive)> select * from temperature distribute by year sort by year asc,tempra desc;
- //MapReduce...
- Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 2
- //MapReduce...
- OK
- year tempra
- 2008 35`C
- 2008 32.5`C
- 2008 31`C
- 2008 31.5`C
- 2008 30`C
- 2015 41`C
- 2015 39`C
- 2015 37`C
- 2015 36`C
- 2015 35`C
- 2015 33`C
- Time taken: 17.358 seconds
Hive中order by,sort by,distribute by,cluster by的区别的更多相关文章
- hive中order by ,sort by ,distribute by, cluster by 的区别(**很详细**)
hive 查询语法 select [all | distinct] select_ condition, select_ condition from table_name a [join table ...
- hive中order by,sort by, distribute by, cluster by作用以及用法
1. order by Hive中的order by跟传统的sql语言中的order by作用是一样的,会对查询的结果做一次全局排序,所以说,只有hive的sql中制定了order by所有的 ...
- [转载]hive中order by,sort by, distribute by, cluster by作用以及用法
1. order by Hive中的order by跟传统的sql语言中的order by作用是一样的,会对查询的结果做一次全局排序,所以说,只有hive的sql中制定了order by所有的 ...
- hive中order by,sort by, distribute by, cluster by的用法
1.order by hive中的order by 和传统sql中的order by 一样,对数据做全局排序,加上排序,会新启动一个job进行排序,会把所有数据放到同一个reduce中进行处理,不管数 ...
- hive 中 Order by, Sort by ,Dristribute by,Cluster By 的作用和用法
order by order by 会对输入做全局排序,因此只有一个reducer(多个reducer无法保证全局有序) 只有一个reducer,会导致当输入规模较大时,需要较长的计算时间. set ...
- hive 排序 order by sort by distribute by cluster by
order by: order by是全局排序,受hive.mapred.mode的影响. 使用orderby有一些限制: 1.在严格模式下(hive.mapred.mod ...
- hive中order by、distribute by、sort by和cluster by的区别和联系
hive中order by.distribute by.sort by和cluster by的区别和联系 order by order by 会对数据进行全局排序,和oracle和mysql等数据库中 ...
- Hadoop Hive 中的排序 Order by ,Sort by ,Distribute by以及 Cluster By
order by order by 会对输入做全局排序,因此只有一个reducer(多个reducer无法保证全局有序)只有一个reducer,会导致当输入规模较大时,需要较长的计算时间. set h ...
- [大数据相关] Hive中的全排序:order by,sort by, distribute by
写mapreduce程序时,如果reduce个数>1,想要实现全排序需要控制好map的输出,详见Hadoop简单实现全排序. 现在学了hive,写sql大家都很熟悉,如果一个order by解决 ...
随机推荐
- HDUOJ---Piggy-Bank
Piggy-Bank Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- JavaScript 设计模式之命令模式
一.命令模式概念解读 1.命令模式概念文字解读 命令模式(Command)的定义是:用来对方法调用进行参数化处理和传送,经过这样处理过的方法调用可以在任何需要的时候执行.也就是说该模式旨在将函数的调用 ...
- RabbitMQ 安装和监控[原,转]
在Windows上安装Rabbit MQ 指南,最好的是这篇<Rabbit MQ Windows Installation guide>,其中还包括了使用.NET RabbitMQ.Cli ...
- 转Jmeter报告优化之New XSL stylesheet
Jmeter默认的报告展示的信息比较少,如果出错了,不是很方便定位问题.由Jmeter默认报告优化这篇文章可知,其实由.jtl格式转换为.html格式的报告过程中,style文件起了很关键的作用.下面 ...
- Accounting_会计电算化工作指南
会计电算化工作指南 会计电算化实施的内容目标及原则 企业会计电算化的实施,也就是企业建立会计电算化的整个过程,是一项复杂的系统工程.在整个系统的实施过程中,包括会计电算化工作的规划,会计信息的建立与管 ...
- Unix环境高级编程(十二)线程控制
本章介绍了一个进程中多个线程之间如何保持数据的似有性及进程的系统调用如何与线程进行交互. 1.线程限制: Single Unix定义了一线线程操作的限制,和其他的限制一样,可以通过sysconf来查询 ...
- tomcat6的编译和导入myeclipse
声明:近期在学习tomcat6的源代码,网上搜索了些相关的资料,并自己操作了下进行了对应的汇总.如今总结例如以下 本文目的:编译tomcat6源代码+导入tomcat6源代码到myeclipse 測试 ...
- oracle启动、关闭
windows上启动关闭 http://www.cnblogs.com/victor_chou/archive/2009/07/09/1519594.html
- mysql-5.7 密码过期详解
一.起源: 今天一上班就听到说error-log里记录了大量的 ERROR 1820 (HY000): You must reset your password using ALTER USER st ...
- unity3d 通过添加rigidBody来指明物体是动态的,以避免cache开销
unity3d 通过添加rigidBody来指明物体是动态的,以避免cache开销. 如果不添加rigidBody,则unity会认为它是静态的,会对物理计算进行cache,但如果此物体实际上tran ...