//五种子句是有严格顺序的:
where → group by → having → order by → limit

//where和having的区别:
//where是先过滤再分组(对原始数据过滤),where限定聚合函数
hive> select count(*),age from tea where id>18 group by age; //having是先分组再过滤(对每个组进行过滤,having后只能跟select中已有的列)
hive> select age,count(*) c from tea group by age having c>2; //group by后面没有的列,select后面也绝不能有(聚合函数除外)
hive> select ip,sum(load) as c from logs group by ip sort by c desc limit 5; //distinct关键字返回唯一不同的值(返回age和id均不相同的记录)
hive> select distinct age,id from tea; //hive只支持Union All,不支持Union
//hive的Union All相对sql有所不同,要求列的数量相同,并且对应的列名也相同,但不要求类的类型相同(可能是存在隐式转换吧)
select name,age from tea where id<80
union all
select name,age from stu where age>18;

Order By特性

  • 对数据进行全局排序,只有一个reducer task,效率低下。
  • 与mysql中 order by区别在于:在 strict 模式下,必须指定 limit,否则执行会报错
• 使用命令set hive.mapred.mode; 查询当前模式
• 使用命令set hive.mapred.mode=strick; 设置当前模式
hive> select * from logs where date='2015-01-02' order by te;
FAILED: SemanticException 1:52 In strict mode,
if ORDER BY is specified, LIMIT must also be specified.
Error encountered near token 'te'
  • 对于分区表,还必须显示指定分区字段查询
hive> select * from logs order by te limit 5;
FAILED: SemanticException [Error 10041]:
No partition predicate found for Alias "logs" Table "logs"

Sort BY特性:

  • 可以有多个Reduce Task(以DISTRIBUTE BY后字段的个数为准)。也可以手工指定:set mapred.reduce.tasks=4;
  • 每个Reduce Task 内部数据有序,但全局无序
set mapred.reduce.tasks = 2;
insert overwrite local directory '/root/hive/b'
select * from logs
sort by te;

上述查询语句,将结果保存在本地磁盘 /root/hive/b ,此目录下产生2个结果文件:000000_0 + 000001_0 。每个文件中依据te字段排序。

Distribute by特性:

  • 按照指定的字段对数据进行划分到不同的输出 reduce 文件中
  • distribute by相当于MR 中的paritioner,默认是基于hash 实现的
  • distribute by通常与Sort by连用
set mapred.reduce.tasks = 2;
insert overwrite local directory '/root/hive/b'
select * from logs
distribute by date
sort by te;

Cluster By特性:

  • 如果 Sort By 和 Distribute By 中所有的列相同,可以缩写为Cluster By以便同时指定两者所使用的列。
  • 注意被cluster by指定的列只能是降序,不能指定asc和desc。一般用于桶表
set mapred.reduce.tasks = 2;
insert overwrite local directory '/root/hive/b'
select * from logs
cluster by date;

Hive中排序和聚集的更多相关文章

  1. Hive 中的四种排序详解,再也不会混淆用法了

    Hive 中的四种排序 排序操作是一个比较常见的操作,尤其是在数据分析的时候,我们往往需要对数据进行排序,hive 中和排序相关的有四个关键字,今天我们就看一下,它们都是什么作用. 数据准备 下面我们 ...

  2. Hive中的排序语法

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

  3. mysql分组排序取最大值所在行,类似hive中row_number() over partition by

    如下图, 计划实现 :按照 parent_code 分组, 取组中code最大值所在的整条记录,如红色部分.(类似hive中: row_number() over(partition by)) sel ...

  4. Hive中的窗口函数

    简介 本文主要介绍hive中的窗口函数.hive中的窗口函数和sql中的窗口函数相类似,都是用来做一些数据分析类的工作,一般用于olap分析 概念 我们都知道在sql中有一类函数叫做聚合函数,例如su ...

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

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

  6. hive中rcfile格式(收藏文)

    首先声明,此文是属于纯粹收藏文,感觉讲的很不错. 本文介绍了Facebook公司数据分析系统中的RCFile存储结构,该结构集行存储和列存储的优点于一身,在MapReduce环境下的大规模数据分析中扮 ...

  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 中窗口函数row_number,rank,dense_ran,ntile分析函数的用法

    hive中一般取top n时,row_number(),rank,dense_ran()这三个函数就派上用场了, 先简单说下这三函数都是排名的,不过呢还有点细微的区别. 通过代码运行结果一看就明白了. ...

  9. hive分组排序 取top N

    pig可以轻松获取TOP n.书上有例子 hive中比较麻烦,没有直接实现的函数,可以写udf实现.还有个比较简单的实现方法: 用row_number,生成排名序列号.然后外部分组后按这个序列号多虑, ...

随机推荐

  1. Java基础ArrayList、Servlet与Filter

    一.技术分享 迭代器(Iterator) 迭代器是一种设计模式,它是一个对象,它可以遍历并选择序列中的对象,而开发人员不需要了解该序列的底层结构.迭代器通常被称为"轻量级"对象,因 ...

  2. SQL中的循环获取根节点

    方法一: DECLARE @employeeid INT; set @employeeid = 8115; DECLARE @id INT; DECLARE @pid int; DECLARE @le ...

  3. js对用户信息加密传输 java后端解密

    1.加密采用服务端随机生成加密因子放入session中,传入登录或注册界面(每次进入都刷新) 2.页面中引入jquery.aes.js(这个js从网上下的坑比较多,引入先后顺序不一致都会报错,所以最后 ...

  4. 微信开发 获取用户openId 与路由控制

    w实践,满足当前需求. www.w.com www.w.com/w1.php $wxurl='https://open.weixin.qq.com/connect/oauth2/authorize?a ...

  5. 谷歌浏览器Chrome错误提示Flash过期怎么办(转)

    在使用谷歌浏览器Chrome时,会碰到谷歌浏览器Chrome的错误提示:“Adobe Flash Player因过期而遭到阻止”,点击“更新插件”是不行的,国内的网络根本就打不开,点击“运行一次”是可 ...

  6. Linux kernel 'mq_notify'内存错误引用漏洞

    更新系统内核即可!参看http://www.cnblogs.com/peteremperor/p/9065998.html

  7. Elment UI的使用说明

    一. Elment UI 1. 简介 Element UI是饿了么团队提供的一套基于Vue2.0的组件库,可以快速搭建网站,提高开发效率,就如同bootstrap. 2.组件分类 ElementUI  ...

  8. 【转帖】C++经典书籍汇总

    TCPL和D&E 1:Bjarne Stroustrup, The C++ Programming Language (Special 3rd Edition) <C++ 程序设计语言( ...

  9. 关于websocket通讯

    var ws = { init:function(callback){ var _this = this; _this.callback = callback; }, websocket:functi ...

  10. Writing a device driver for Windows

    Writing a device driver for Windows        In order to write a device driver for windows, one needs ...