hive数据分析

2020年全国高校计算机能力挑战赛

第三部分:大数据分析(hive)

product.csv 链接:https://pan.baidu.com/s/1dGap6B_ub_bucx4BzfgH-w
提取码:nu68

sales.csv 链接:https://pan.baidu.com/s/1nsi6Hd5ah7Fc_0mqhK2xEw
提取码:s08r

数据说明:

Product:产品表

product_id, 产品ID

product_name, 产品名称

unit_price, 产品单价

inventory 产品库存

salse: 销售表

order_id, 订单ID

product_id, 产品ID

customer_id,客户ID

year, 年

month,月

day, 日

freight_charges,运费

unit_price,单价

quantity 销售数量

任务一、hive创建数据库

  1. 创建一个数据库,以你的用户名命名,创建成功后使用use命令切换为该库,并执行set hive.cli.print.current.db=true;截图作为答案

    create database zhaoxiaofeng;
    use zhaoxiaofeng;
    set hive.cli.print.current.db=true;
    
    
    
  2. 在HIVE中创建需要的表,并写出建表语句,表结构如下:

    (1).产品表(product)

    字段字段描述字段类型
    product_id产品IDString
    product_name产品名称String
    unit_price产品单价decimal(20,3)
    inventory产品库存decimal(20,3)

    (2)销售表(salse)

    字段字段描述字段类型
    order_id订单IDString
    product_id产品IDString
    customer_id客户IDString
    yearInt
    monthInt
    dayInt
    freight_charges运费decimal(20,3)
    unit_price单价decimal(20,3)
    quantity销售数量decimal(20,3)

    1)创建product表

    create table product(product_id string,product_name string,unit_price  decimal(20,3),   inventory decimal(20,3) ) row format delimitad fields terminated by ',';
    

    补充:

    decimal(M,D)

    • M是最大位数(精度),范围是1到65。可不指定,默认值是10。
    • D是小数点右边的位数(小数位)。范围是0到30,并且不能大于M,可不指定,默认值是0。

    2)创建salse表

    create table salse( order_id  stirng, product_id  string, customer_id  string, year  int,month int,day int,freight_charges , decimal(20,3),unit_price   decimal(20,3) ,  quantity   decimal(20,3) )  row format delimitad fields terminated by ','; 
    
  3. 将数据加载到表中,写出加载数据的语句

    1)导入product表

    load data local inpath '/home/zhaoxiaofeng/product.csv' into table product;
    #参看数据发现第一行数据不是我们想要插进表的数据
    alter table product set TBLPROPERTIES ('skip.header.line.count'='1');
    

    2)导入salse表

    load data local inpath '/home/zhaoxiaofeng.sales.csv' into table salse;
    alter table salse set TBLPROPERTIES ('skip.header.line.count'='1';
    

    任务二、hive数据分析

  4. 查询每个产品的平均运费,平均销售额

    create table a1 as
    select product_id ,avg(freight_charges),avg(unit_price*quantity)
    from salse
    group by product_id;
    
  5. 查询选购了3种产品以上(包含3种产品)的客户ID

    create table a2 as
    select customer_id
    from salse
    group by customer_id
    having count(product_id)>=3;
    
  6. 使用所建的表统计各产品总销售数量,总销售额(单价*销售数量),按照总销售额降序排列。

    输出的字段包含

    字段字段描述
    product_name产品名称
    Total_ quantity总销量
    Total_salse总销售额
    create table a3 as
    select p.product_name product_name,sum(s.quantity) Total_quantity,sum(s.unit_price*s.quantity) Total_salse
    from product p,salse s
    where p.product_id = s.product_id
    group by p.product_name
    order by Total_salse desc;
    
    #查看表结构
    desc as;
    
  7. 使用所建的表统计每年销售额最多的4个产品信息。 输出的字段 包含 产品id,年,总销售额

    create table a4 as
    select s.product_id,s.year,a.Total_salse
    from salse s,a4 a
    order by Total_salse desc;
    
    
    #查看销售量最多的4个产品信息
    select * from a4 limit 4;
    
    
  • 3
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值