Hive练习题--分区

目录

分区表

单级分区表(一级文件夹)

多级分区表(多级文件夹)

小结


#博学谷IT学习技术支持#

鸽了这么久,最近在做项目有点忙不过来。。。。

分区表

  • 单级分区表(一级文件夹)

    • 用到的数据(score.txt)
    • 01	01	80
      01	02	90
      01	03	99
      02	01	70
      02	02	60
      02	03	80
      03	01	80
      03	02	80
      03	03	80
      04	01	50
      04	02	30
      04	03	20
      05	01	76
      05	02	87
      06	01	31
      06	03	34
      07	02	89
      07	03	98
      

-- 1、创建分区表

    静态分区:
       所有的分区的值需要手动指定
    动态分区:
      所有的分区的值自动生成


use myhive;

create table score
(
    sid    string, -- 学号
    cid    string, -- 学科id
    sscore int     -- 成绩
)
partitioned by (month string)
row format delimited fields terminated by '\t';


 -- 2、给分区表添加数据


 -- 3.查询数据


-- 3.1 条件查询 - 只查询6月份的月考成绩


-- 3.1 条件查询 - 只查询6月份或7月份成绩

答案如下

#答案

 -- 2、给分区表添加数据
-- /user/hive/warehouse/myhive.db/score/month=202006
load data local inpath '/export/data/score.txt' into table score partition (month='202006');

-- /user/hive/warehouse/myhive.db/score/month=202007
load data local inpath '/export/data/score.txt' into table score partition (month='202007');

-- /user/hive/warehouse/myhive.db/score/month=202008
load data local inpath '/export/data/score.txt' into table score partition (month='202008');

-- 3.查询数据
select * from score;

-- 3.1 条件查询 - 只查询6月份的月考成绩
select * from score where  month='202006';

-- 3.1 条件查询 - 只查询6月份或7月份成绩
select * from score where  month='202007';

  • 多级分区表(多级文件夹)

#建表

create table score2
(
    sid    string,
    cid    string,
    sscore int
)
partitioned by (year string,month string,day string)
row format delimited fields terminated by '\t';


#多级分区例1
load data local inpath '/export/data/score.txt'
    into table score2 partition (year='2022',month='01',day='01');

#多级分区例2
load data local inpath '/export/data/score.txt'
    into table score2 partition (year='2022',month='02',day='02');

-- 查询
-- 查询所有数据


-- 查询指定时间的成绩(2022年成绩)


-- 查询指定时间的成绩(2023年2月成绩)


-- 查询指定时间的成绩(2023年2月2日成绩)


-- 查看分区信息


-- 查看表结构,包含分区信息


-- 添加分区


--删除分区

答案如下

#答案
-- 查询
-- 查询所有数据
select * from score2 ;

-- 查询指定时间的成绩(2022年成绩)
select * from score2 where year='2022';

-- 查询指定时间的成绩(2023年2月成绩)
select * from score2 where year='2023' and month='02';

-- 查询指定时间的成绩(2023年2月2日成绩)

select * from score2 where year='2023' and month='02' and day = '02';


--- union all,将两个表的结果上下拼接在一起(和join不同,join是左右拼接)
explain select * from score where month = '202006' union all select * from score where month = '202007';
select * from score where month = '202006' or month = '202007';

-- 查看分区信息
show  partitions score;
show  partitions score2;

-- 查看表结构,包含分区信息
desc score;
desc formatted  covid2;  //Table Type: EXTERNAL_TABLE


-- 添加分区
alter table score add partition(month='202009');

alter table score add partition(month='202010') partition(month='202011');

--删除分区
alter table score drop partition(month = '202010');

小结

这些都是来自我自己的笔记,可以让你认识基础的分区表(静态)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值