hive中常见的高级查询包括:group by、Order by、join、distribute by、sort by、cluster by、Union all。今天我们来看看order by操作,Order by表示按照某些字段排序,语法如下:

  1. select col,col2...
  2. from tableName
  3. where condition
  4. order by col1,col2 [asc|desc]

注意:

(1):order by后面可以有多列进行排序,默认按字典排序。

(2):order by为全局排序。

(3):order by需要reduce操作,且只有一个reduce,无法配置(因为多个reduce无法完成全局排序)。

order by操作会受到如下属性的制约:

  1. set hive.mapred.mode=nonstrict; (default value / 默认值)
  2. set hive.mapred.mode=strict;

注:如果在strict模式下使用order by语句,那么必须要在语句中加上limit关键字,因为执行order by的时候只能启动单个reduce,如果排序的结果集过大,那么执行时间会非常漫长。

下面我们通过一个示例来深入体会order by的用法:

数据库有一个employees表,数据如下:

  1. hive> select * from employees;
  2. OK
  3. lavimer 15000.0 ["li","lu","wang"]  {"k1":1.0,"k2":2.0,"k3":3.0}    {"street":"dingnan","city":"ganzhou","num":101} 2015-01-24  love
  4. liao    18000.0 ["liu","li","huang"]    {"k4":2.0,"k5":3.0,"k6":6.0}    {"street":"dingnan","city":"ganzhou","num":102} 2015-01-24  love
  5. zhang   19000.0 ["xiao","wen","tian"]   {"k7":7.0,"k8":8.0,"k8":8.0}    {"street":"dingnan","city":"ganzhou","num":103} 2015-01-24  love

现在我要按第二列(salary)降序排列:

  1. hive> select * from employees order by salary desc;
  2. //执行MapReduce的过程
  3. Job 0: Map: 1  Reduce: 1   Cumulative CPU: 2.62 sec   HDFS Read: 415 HDFS Write: 245 SUCCESS
  4. Total MapReduce CPU Time Spent: 2 seconds 620 msec
  5. OK
  6. zhang   19000.0 ["xiao","wen","tian"]   {"k7":7.0,"k8":8.0} {"street":"dingnan","city":"ganzhou","num":103} 2015-01-24  love
  7. liao    18000.0 ["liu","li","huang"]    {"k4":2.0,"k5":3.0,"k6":6.0}    {"street":"dingnan","city":"ganzhou","num":102} 2015-01-24  love
  8. lavimer 15000.0 ["li","lu","wang"]  {"k1":1.0,"k2":2.0,"k3":3.0}    {"street":"dingnan","city":"ganzhou","num":101} 2015-01-24  love
  9. Time taken: 20.484 seconds
  10. hive>

此时的hive.mapred.mode属性为:

  1. hive> set hive.mapred.mode;
  2. hive.mapred.mode=nonstrict
  3. hive>

现在我们将它改为strict,然后再使用order by进行查询:

  1. hive> set hive.mapred.mode=strict;
  2. hive> select * from employees order by salary desc;
  3. FAILED: Error in semantic analysis: 1:33 In strict mode, if ORDER BY is specified, LIMIT must also be specified. Error encountered near token 'salary'
  4. hive>

注:在strict模式下查询必须加上limit关键字。

  1. hive> select * from employees order by salary desc limit 3;
  2. FAILED: Error in semantic analysis: No partition predicate found for Alias "employees" Table "employees"

注:另外还有一个要注意的是strict模式也会限制分区表的查询,解决方案是必须指定分区

先来看看分区:

  1. hive> show partitions employees;
  2. OK
  3. date_time=2015-01-24/type=love
  4. Time taken: 0.096 seconds

在strict模式先使用order by查询:

  1. hive> select * from employees where partition(date_time='2015-01-24',type='love') order by salary desc limit 3;
  2. FAILED: Parse Error: line 1:30 cannot recognize input near 'partition' '(' 'date_time' in expression specification
  3. hive
  4. > select * from employees where date_time='2015-01-24' and type='love' order by salary desc limit 3;
  5. //执行MapReduce程序
  6. Total MapReduce CPU Time Spent: 3 seconds 510 msec
  7. OK
  8. zhang   19000.0 ["xiao","wen","tian"]   {"k7":7.0,"k8":8.0} {"street":"dingnan","city":"ganzhou","num":103} 2015-01-24  love
  9. liao    18000.0 ["liu","li","huang"]    {"k4":2.0,"k5":3.0,"k6":6.0}    {"street":"dingnan","city":"ganzhou","num":102} 2015-01-24  love
  10. lavimer 15000.0 ["li","lu","wang"]  {"k1":1.0,"k2":2.0,"k3":3.0}    {"street":"dingnan","city":"ganzhou","num":101} 2015-01-24  love
  11. Time taken: 19.861 seconds
  12. hive>

HIVE中的order by操作的更多相关文章

  1. Hive中的Order by与关系型数据库中的order by语句的异同点

    在Hive中,ORDER BY语句是对查询结果集进行整体的排序,最终将会产生一个reducer进行全局的排序,达到的最终结果是和传统的关系型数据库是一样的. 在数据量非常大的时候,全局排序的单个red ...

  2. Hive中的order by、sort by、distribute by、cluster by解释及测试

    结论: order by:全局排序,这也是4种排序手段中唯一一个能在终端输出中看出全局排序的方法,只有一个reduce,可能造成renduce任务时间过长,在严格模式下,要求必须具备limit子句. ...

  3. Hive 中的 order by, sort by, distribute by 与 cluster by

    Order By order by 会对输入做全排序, 因此只有一个Reducer(多个Reducer无法保证全局有序), 然而只有一个Reducer, 会导致当输入规模较大时, 消耗较长的计算时间. ...

  4. hive中order by,sort by, distribute by, cluster by的用法

    1.order by hive中的order by 和传统sql中的order by 一样,对数据做全局排序,加上排序,会新启动一个job进行排序,会把所有数据放到同一个reduce中进行处理,不管数 ...

  5. hive中Sort By,Order By,Cluster By,Distribute By,Group By的区别

    order by:  hive中的order by 和传统sql中的order by 一样,对数据做全局排序,加上排序,会新启动一个job进行排序,会把所有数据放到同一个reduce中进行处理,不管数 ...

  6. hive中order by,sort by, distribute by, cluster by作用以及用法

    1. order by     Hive中的order by跟传统的sql语言中的order by作用是一样的,会对查询的结果做一次全局排序,所以说,只有hive的sql中制定了order by所有的 ...

  7. [转载]hive中order by,sort by, distribute by, cluster by作用以及用法

    1. order by     Hive中的order by跟传统的sql语言中的order by作用是一样的,会对查询的结果做一次全局排序,所以说,只有hive的sql中制定了order by所有的 ...

  8. Hive中的排序语法

    ORDER BY hive中的ORDER BY语句和关系数据库中的sql语法相似.他会对查询结果做全局排序,这意味着所有的数据会传送到一个Reduce任务上,这样会导致在大数量的情况下,花费大量时间. ...

  9. hive:数据库“行专列”操作---使用collect_set/collect_list/collect_all & row_number()over(partition by 分组字段 [order by 排序字段])

    方案一:请参考<数据库“行专列”操作---使用row_number()over(partition by 分组字段 [order by 排序字段])>,该方案是sqlserver,orac ...

随机推荐

  1. HDUOJ---(1995)汉诺塔V

    汉诺塔V Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  2. 学会快速装系统 图解硬盘分区软件Norton Ghost使用

    http://edu.itbulo.com/200909/126313_5.htm即使你拥有最先进的电脑,采用传统的方法,Windows的安装速度仍然是令人头痛的!有没有什么重装系统的简便方法呢?当然 ...

  3. iOS - App 与外设间的通信方式

    1.前言 一般 iOS 开发者做 App 开发大部分时候都是通过 Http(s) 请求跟后台服务器打交道,做一些信息展示和用户交互.很少涉及到去跟外部硬件设备连接的开发.随着近年来车联网和物联网的兴起 ...

  4. hello oc

    printf("Hello C\n"); //OC可以采用C语言的输出方式 printf("The number is %d\n",100);//%d 输出数字 ...

  5. 套接字I/O超时设置方法和用select实现超时

    注:如无特殊说明,sockfd 原始状态都是阻塞的. 一.使用alarm 函数设置超时  C++ Code  1 2 3 4 5 6 7 8 9 10 11 12 13   void handler( ...

  6. WIN7下恼人的AppData——删除没用的缓存文件

    今日.打开电脑,发现C盘可用容量居然变得非常小.认为非常是可疑,例如以下图所看到的: 最初的反应是电脑中毒了,于是使用360卫士.360杀毒对C盘查杀,由于明明记得C盘有40多G的可用空间才对.出现这 ...

  7. Unix lrzsz命令 上传本地文件到服务器 / 发送文件到客户端

    第三方教程:https://www.jb51.net/article/73690.htm 安装命令: $ yum install lrzsz 本地上传文件到服务器,如果是xshell,直接拖拽文件进入 ...

  8. jquery.dataTables的用法

    写页面前端时,使用表格的插件可以快速漂亮的排版.本例子中使用jquery.dataTables来处理table.直接来点干货 html代码如下 <table cellpadding=" ...

  9. ASP.NET MVC 操作AD 获取域服务器当前用户姓名和OU信息

    #region 根据当前登录域账号 获取AD用户姓名和所在OU目录 /// <summary> /// 根据当前登录域账号 获取AD用户姓名和所在OU目录 /// </summary ...

  10. VS2008配置OpenGl 亲测可行

    OpenGL作为当前主流的图形API之一,它在一些场合具有比DirectX更优越的特性.1.与C语言紧密结合.OpenGL命令最初就是用C语言函数来进行描述的,对于学习过C语言的人来讲,OpenGL是 ...