• 头歌平台云计算实验



    其他部分答案在B站工房 https://gf.bilibili.com/item/detail/1105242061

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    Hive综合应用案例——用户学历查询

    1 查询每一个用户从出生到现在的总天数

    ---------- 禁止修改 ----------
     drop database if exists mydb cascade;
    ---------- 禁止修改 ----------
    
    
    ---------- begin ----------
    ---创建mydb数据库
    create database if not exists mydb;
    ---使用mydb数据库
    use mydb;
    
    
    
    ---创建表user
    create table usertab(
        id string,
        sex string,
        time string,
        education string,
        occupation string,
        income string,
        area string,
        desired_area string,
        city_countryside string
    )
    row format delimited fields terminated by ',';
    
    
    
    ---导入数据:/root/data.txt
    load data local inpath '/root/data.txt' into table usertab;
    
    
    
    --查询每一个用户从出生到2019-06-10的总天数
    select id, datediff('2019-06-10',regexp_replace(time, '/', '-')) from usertab;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    2 同一个地区相同的教育程度的最高收入

    ---------- 禁止修改 ----------
     drop database if exists mydb cascade;
    ---------- 禁止修改 ----------
    
    
    ---------- begin ----------
    
    
    
    --创建mydb数据库
    create database if not exists mydb;
    
    
    
    ---使用mydb数据库
    use mydb;
    
    
    
    ---创建表user
    create table usertab1(
        id int,
        sex string,
        time string,
        education string,
        occupation string,
        income string,
        area string,
        desired_area string,
        city_countryside string
    )
    row format delimited fields terminated by ',';
    
    
    
    ---导入数据:/root/data.txt
    load data local inpath '/root/data1.txt' into table usertab1;
    
    
    
    --同一个地区相同的教育程度的最高收入
    select area,education,income
    from(
        select area,education,income,
        row_number() over(
            partition by area, education order by income desc
        ) as t1
        from usertab1
    ) as t2
    where t2.t1 = 1;
    
    
    
    ---------- end ----------
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    3 统计各级学历所占总人数百分比

    ---------- 禁止修改 ----------
    
     drop database if exists mydb cascade;
     set hive.mapred.mode=nonstrict;
    ---------- 禁止修改 ----------
    
    
    ---------- begin ----------
    
    
    
    --创建mydb数据库
    create database if not exists mydb;
    
    
    
    ---使用mydb数据库
    use mydb;
    
    
    
    ---创建表user
    create table usertab2(
        id int,
        sex string,
        time string,
        education string,
        occupation string,
        income string,
        area string,
        desired_area string,
        city_countryside string
    )
    row format delimited fields terminated by ',';
    
    
    
    ---导入数据:/root/data.txt
    load data local inpath '/root/data.txt' into table usertab2;
    
    
    
    --统计各级学历所占总人数百分比(对结果保留两位小数)
    select concat(round(t1.cnted * 100 / t2.cnt, 2),'%'), t1.education
    from
        (
            select count(*) as cnted,education
            from usertab2
            group by education
        ) as t1,
    
        (
            select count(*) as cnt from usertab2
        ) as t2
    order by t1.education;
    
    
    
    ---------- end ----------
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
  • 相关阅读:
    解决 MacOS 连接公司 VPN 成功但是不能网络的问题
    27.3 Java集合学习之List(基本概念,API,存储原理)
    【UE5 Cesium】17-Cesium for Unreal 建立飞行跟踪器(2)
    gRPC四种通信模式
    Activiti进阶
    快人一步!利用LLM实现数据处理自动化
    【论文阅读笔记】Deep learning for time series classification: a review
    【目的:windows下VS2017/2022配置使用opengl - 初探-创建一个空窗口】
    STL类模板入门
    nlp与知识图谱代码解读_词嵌入
  • 原文地址:https://blog.csdn.net/weixin_44177494/article/details/137903552