Hive 热门数据分析笔试题(干货满满,持续更新中...)
发布日期:2021-05-08 01:13:37 浏览次数:31 分类:精选文章

本文共 2600 字,大约阅读时间需要 8 分钟。

云祁的技术博客

我是「云祁」,一枚热爱技术、会写诗的大数据开发猿。昵称来源于王安石诗中一句《云之祁祁,或雨于渊》,这句诗给我的感觉,与技术开发有着异曲同工之处,总觉得技术路上也需要一步一步稳扎稳打。

写博客一方面是对自己学习的一点点总结及记录,另一方面则是希望能够帮助更多对大数据感兴趣的朋友。如果你也对数据中台、数据建模、数据分析以及Flink/Spark/Hadoop/数仓开发感兴趣,可以关注我的动态,共同探索大数据的无限魅力!


背景

在用HQL进行数据分析时,我们不仅想要聚集前的数据,又想要聚集后的数据,这时候往往可以想到用窗口函数。具体窗口函数的使用,可以参考这篇博文。


笔试题实战

Hive 的五个笔试题

以下是一些 Hive 的常见笔试题,供大家参考:

  • 统计每个月的销售额前三名,按月份分组

    • 可以通过自连接或窗口函数来实现。
  • 计算每个学校每个班级每科的平均分

    • 使用 group by 和 avg 函数即可。
  • 统计每个客户最近三个月的消费金额总和

    • 可以利用窗口函数 range between 2 preceding and 2 following 来实现。
  • 计算每个地区每个季度的销售额总和

    • 使用 partition by 和 group by 结合。
  • 统计每个学生的成绩排名

    • 可以通过排名函数 row_number() 来实现。

  • 数据分析题(案例)

    场景举例:北京市学生成绩分析

    成绩的数据格式:时间,学校,年级,姓名,科目,成绩

    样例数据如下:

    2013,北大,1,裘容絮,语文,9722013,北大,1,庆眠拔,语文,5222013,北大,1,乌洒筹,语文,8522012,清华,0,钦尧,英语,6122015,北理工,3,f冼殿,物理,8122016,北科,4,况飘索,化学,9222014,北航,2,孔须,数学,7022012,清华,0,王脊,英语,5922014,北航,2,方部盾,数学,4922014,北航,2,东门雹,数学,77

    表创建语句:

    create table t_score (time int,  school string,  class int,  name string,  subjects string,  score int)row format delimited fields terminated by ',';load data local inpath "/opt/data/a.csv" into table t_score;

    问题一:分组 TopN,选出2014年每个学校、每个年级、分数前三的科目

    解决方案:

    select t.* from (  select     time,     school,     class,     score,     row_number() over (partition by school, class, subjects order by score desc) rank_code  from t_score   where time = "2014") t where t.rank_code <= 3;

    问题二:2014年,北航,每个班级,每科的分数及分数上下浮动2分的总和

    解决方案:

    select   time,   school,   class,   subjects,   sum(score) over (order by score range between 2 preceding and 2 following) sscore from t_score where time = "2014"   and school = "北航";

    问题三:2012年,清华 0 年级,总成绩大于200分的学生及学生数

    解决方案:

    select *, sum(score) as total_score, count(1) over (partition by school, class) from t_score where school = "清华"   and class = 0   and time = "2012"group by school, class, name, time, subjects, score having total_score > 50;

    情景分析题

    数据倾斜处理

    今年加入了10个学校,学校数据差异很大。计算每个学校的平均分。

    注意事项:

  • 数据倾斜的处理方式

    • Hive 中默认会对 group by 的字段进行负载均衡处理,但如果数据分布不均,可能会导致倾斜。
    • 可以通过以下方式解决:
      set hive.groupby.skewindata=true;

      这样会生成两个 MapReduce 任务,第一个任务负责打散数据,第二个任务负责聚合。

  • 具体操作步骤:

    • 创建分区表:
      create table partitioned_transaction (cust_id int, amount float, country string) partitioned by (month string) row format delimited fields terminated by ',';
    • 启用动态分区:
      set hive.exec.dynamic.partition=true;set hive.exec.dynamic.partition.mode=nonstrict;
    • 导入数据:
      insert overwrite table partitioned_transaction partition (month) select cust_id, amount, country, month from transaction_details;
    • 查询数据:
      select month, sum(amount) as total_income from partitioned_transaction group by month;

  • 最新文章推荐


    关注我的公众号,获取更多数据仓库、数据建模与大数据技术的实用内容!

    上一篇:SSM框架整合详解
    下一篇:Apache Flink 替换 Spark Stream的架构与实践( bilibili 案例解读)

    发表评论

    最新留言

    很好
    [***.229.124.182]2025年04月01日 06时09分14秒

    关于作者

        喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
    -- 愿君每日到此一游!

    推荐文章