Hive详解(4)

Hive

窗口函数

分析函数

  1. 聚合函数,例如sumavgmaxmin

  2. 移位函数

    1. lag(colName, n):以当前行为基础,来处理第前n行的数据

    2. lead(colName, n):以当前行为基础,来处理第后n行的数据

    3. ntile(n):要求数据必须有序,将有序的数据依次放入n个桶中,保证每个桶中的数据几乎一致,相差最多不超过1个

  3. 排序函数

    1. row_number:数据排序之后,按顺序给数据进行编号,即使数据相同,也是给定不同的编号

    2. rank:数据排序之后,按顺序给数据进行编号,如果数据相同,则给定相同的序号,会产生空位

    3. dense_rank:数据排序之后,按顺序给数据进行编号,如果数据相同,则给定相同的序号,但是不会产生空位

移位函数案例

  1. 需求二:查询每一位顾客的消费明细以及上一次的消费时间

    复制代码
    select *,
           lag(order_date, 1) over (partition by name order by order_date) as last_order_date
    from orders;
  2. 需求三:查询最早进店消费的前20%的顾客信息

    复制代码
    select * from (
        select *,
               ntile(5) over (order by order_date) as n
        from orders
    ) t1 where n = 1;

排序函数案例

  1. 原始数据

    复制代码
    Bob Chinese 85
    Alex Chinese 76
    Bill Chinese 78
    David Chinese 92
    Jack Chinese 69
    Lucy Chinese 74
    LiLy Chinese 78
    Bob Maths 91
    Alex Maths 82
    Bill Maths 69
    David Maths 60
    Jack Maths 69
    Lucy Maths 71
    LiLy Maths 82
    Bob English 60
    Alex English 62
    Bill English 85
    David English 85
    Jack English 69
    Lucy English 78
    LiLy English 93
  2. 案例

    复制代码
    -- 建表
    create table scores (
        name    string,
        subject string,
        score   int
    ) row format delimited fields terminated by ' ';
    -- 加载数据
    load data local inpath '/opt/hive_data/scores' into table scores;
    -- 查询数据
    select *
    from scores tablesample (5 rows);
    -- 按科目对成绩进行降序排序
    select *,
           row_number() over (partition by subject order by score desc) as rn,
           rank() over (partition by subject order by score desc)       as ra,
           dense_rank() over (partition by subject order by score desc) as dr
    from scores;
    -- 获取各科目前三名的信息
    select * from (
        select *, rank() over (partition by subject order by score desc) as n from scores
    ) t where n <= 3;
相关推荐
潘达斯奈基~32 分钟前
在使用spark的applyInPandas方法过程中,遇到类型冲突问题如何解决
大数据·笔记
火星资讯2 小时前
腾多多数字零售模式:从成本转嫁到全生态共赢的破局实践
大数据
望获linux2 小时前
【实时Linux实战系列】实时 Linux 的自动化基准测试框架
java·大数据·linux·运维·网络·elasticsearch·搜索引擎
金宗汉3 小时前
《宇宙递归拓扑学:基于自指性与拓扑流形的无限逼近模型》
大数据·人工智能·笔记·算法·观察者模式
直有两条腿3 小时前
【数据迁移】HBase Bulkload批量加载原理
大数据·数据库·hbase
Joy T3 小时前
海南蓝碳:生态财富与科技驱动的新未来
大数据·人工智能·红树林·海南省·生态区建设
风清再凯4 小时前
01-ELK安装ES,ES-head
大数据·elk·elasticsearch
Guheyunyi4 小时前
风险感知中枢:监测预警系统的架构与核心
大数据·运维·安全·重构·架构·自动化
正在走向自律4 小时前
大数据背景下时序数据库选型指南:国产开源技术的突破与实践
大数据·开源·时序数据库
shinelord明6 小时前
【大数据技术实战】Kafka 认证机制全解析
大数据·数据结构·分布式·架构·kafka