• HIVE基本查询操作(二)——第1关:Hive排序


    第1关:Hive排序


    任务描述

    本关任务:2013722日买入量最高的三种股票。

    相关知识

    为了完成本关任务,你需要掌握:1. Hive的几种排序;2. limit使用。

    hive的排序

    order by

    • order by后面可以有多列进行排序,默认按字典排序(desc:降序,asc(默认):升序);
    • order by为全局排序;
    • order by需要reduce操作,且只有一个reduce,无法配置(因为多个reduce无法完成全局排序);
    • 如果指定了hive.mapred.mode=strict(默认值是nonstrict),这时就必须指定limit来限制输出条数。

    表名:student

    classnamescores
    Axiaoming89
    Axiaojun72
    Bxiaohong88
    Cxiaoqiang92
    Cxiaogang84

    scores降序:

    select * from student order by scores desc;
    
    • 1

    输出

    C    xiaoqiang    92
    A    xiaoming    89
    B    xiaohong    88
    C    xiaogang    84
    A    xiaojun    72
    
    • 1
    • 2
    • 3
    • 4
    • 5

    sort by

    Hive中指定了sort by,那么在每个reducer端都会做排序,也就是说保证了局部有序(每个reducer出来的数据是有序的,但是不能保证所有的数据是有序的,除非只有一个reducer),好处是:执行了局部排序之后可以为接下去的全局排序提高不少的效率(其实就是做一次归并排序就可以做到全局排序了)。

    scores降序:

    select * from student sort by scores desc;
    
    • 1

    输出:

    C    xiaoqiang    92
    A    xiaoming    89
    B    xiaohong    88
    C    xiaogang    84
    A    xiaojun    72
    
    • 1
    • 2
    • 3
    • 4
    • 5

    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;
    
    • 1

    输出:

    C    xiaoqiang    92
    A    xiaoming    89
    B    xiaohong    88
    C    xiaogang    84
    A    xiaojun    72
    
    • 1
    • 2
    • 3
    • 4
    • 5

    cluster by

    如果sort bydistribute by中所用的列相同,可以缩写为cluster by以便同时制定两者所用的列cluster by的功能就是distribute bysort by相结合(注意被cluster by指定的列只能是升序,不能指定ascdesc)。

    以下两句HQL查询结果相同:

    select * from student cluster by scores;
    select * from student distribute by scores sort by scores desc;
    
    • 1
    • 2

    输出:

    A    xiaojun    72
    C    xiaogang    84
    B    xiaohong    88
    A    xiaoming    89
    C    xiaoqiang    92
    
    • 1
    • 2
    • 3
    • 4
    • 5
    limit

    Hive查询中要限制查询输出条数, 可以用limit关键词指定

    只输出2条数据:

    select * from student limit 2;
    
    • 1

    输出:

    A    xiaoming    89
    A    xiaojun    72
    
    • 1
    • 2
    编程要求

    在右侧编辑器补充代码,查询出2013722日的哪三种股票买入量最多。

    表名:total

    col_namedata_typecomment
    tradedatestring交易日期
    tradetimestring交易时间
    securityidstring股票ID
    bidpx1string买入价
    bidsize1int买入量
    offerpx1string卖出价
    bidsize2int卖出量

    部分数据如下所示:

    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
    
    • 1
    • 2
    • 3
    • 4

    数据说明:

    (152896:每种股票id)
    (20130724: 2013年7月24日)
    (145004: 14点50分04秒)
    
    • 1
    • 2
    • 3
    测试说明

    平台会对你编写的代码进行测试:

    预期输出:

    股票id 买入量

    553211    680580680
    412233    230929160
    856947    104360800
    
    • 1
    • 2
    • 3

    开始你的任务吧,祝你成功!


    ----------禁止修改----------
    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----------
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
  • 相关阅读:
    java基础10
    并查集介绍 & 代码实现 & 优化思路详解
    Java八股文(Spring Security)
    设计模式① :适应设计模式
    闲话Python编程-集合set
    【具身智能评估2】具身视觉语言规划(EVLP)数据集基准汇总
    给定任意字符串,反转字符串中的单词
    (2022版)一套教程搞定k8s安装到实战 | TLS Bootstrap初始化流程
    深入探求:中国精益生产与管理实践的崭新视角
    【webrtc】PhysicalSocket::Bind返回-1 ,10013错误
  • 原文地址:https://blog.csdn.net/qq_51916951/article/details/127544665