栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

Hive练习题--分区

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');

小结

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/774805.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号