本关任务:2013年7月22日买入量最高的三种股票。
为了完成本关任务,你需要掌握:1. Hive的几种排序;2. limit使用。
① order by
order by后面可以有多列进行排序,默认按字典排序(desc:降序,asc(默认):升序);order by为全局排序;order by需要reduce操作,且只有一个reduce,无法配置(因为多个reduce无法完成全局排序);hive.mapred.mode=strict(默认值是nonstrict),这时就必须指定limit来限制输出条数。表名:student
| class | name | scores |
|---|---|---|
A | xiaoming | 89 |
A | xiaojun | 72 |
B | xiaohong | 88 |
C | xiaoqiang | 92 |
C | xiaogang | 84 |
按scores降序:
select * from student order by scores desc;
输出
C xiaoqiang 92
A xiaoming 89
B xiaohong 88
C xiaogang 84
A xiaojun 72
② sort by
Hive中指定了sort by,那么在每个reducer端都会做排序,也就是说保证了局部有序(每个reducer出来的数据是有序的,但是不能保证所有的数据是有序的,除非只有一个reducer),好处是:执行了局部排序之后可以为接下去的全局排序提高不少的效率(其实就是做一次归并排序就可以做到全局排序了)。
按scores降序:
select * from student sort by scores desc;
输出:
C xiaoqiang 92
A xiaoming 89
B xiaohong 88
C xiaogang 84
A xiaojun 72
③ distribute by
distribute by控制map输出结果的分发,相同字段的map输出会发到一个reduce节点去处理。sort by为每一个reducer产生一个排序文件,他俩一般情况下会结合使用。(这个肯定是全局有序的,因为相同的class会放到同一个reducer去处理。这里需要注意的是distribute by必须要写在sort by之前)。
按scores降序:
select * from student distribute by class sort by scores desc;
输出:
C xiaoqiang 92
A xiaoming 89
B xiaohong 88
C xiaogang 84
A xiaojun 72
④ cluster by
如果sort by和distribute by中所用的列相同,可以缩写为cluster by以便同时制定两者所用的列cluster by的功能就是distribute by和sort by相结合(注意被cluster by指定的列只能是升序,不能指定asc和desc)。
以下两句HQL查询结果相同:
select * from student cluster by scores;
select * from student distribute by scores sort by scores desc;
输出:
A xiaojun 72
C xiaogang 84
B xiaohong 88
A xiaoming 89
C xiaoqiang 92
在Hive查询中要限制查询输出条数, 可以用limit关键词指定
只输出2条数据:
select * from student limit 2;
输出:
A xiaoming 89
A xiaojun 72
在右侧编辑器补充代码,查询出2013年7月22日的哪三种股票买入量最多。
表名:total
| col_name | data_type | comment |
|---|---|---|
tradedate | string | 交易日期 |
tradetime | string | 交易时间 |
securityid | string | 股票ID |
bidpx1 | string | 买入价 |
bidsize1 | int | 买入量 |
offerpx1 | string | 卖出价 |
bidsize2 | int | 卖出量 |
部分数据如下所示:
20130724 145004 152896 2.62 6960 2.63 13000
20130724 145101 152896 2.86 13880 2.89 6270
20130724 145128 152896 2.85 327400 2.851 1500
20130724 145143 152896 2.603 44630 2.8 10650
数据说明:
(152896:每种股票id)
(20130724: 2013年7月24日)
(145004: 14点50分04秒)
平台会对你编写的代码进行测试:
预期输出:
股票id 买入量
553211 680580680
412233 230929160
856947 104360800
开始你的任务吧,祝你成功!
----------禁止修改----------
create database if not exists mydb;
use mydb;
create table if not exists total(
tradedate string,
tradetime string,
securityid string,
bidpx1 string,
bidsize1 int,
offerpx1 string,
bidsize2 int)
row format delimited fields terminated by ','
stored as textfile;
truncate table total;
load data local inpath '/root/files' into table total;
----------禁止修改----------
----------begin----------
select securityid, sum(bidsize1) s
from total
where tradedate="20130722" group by securityid order by s desc limit 3;
----------end----------