hive分区笔记

hive分区笔记,第1张

hive分区笔记 hive分区 1.一级分区

Hive 中的分区就是分目录。和Map中的切片是基本一致的。Map的切片也是为了提高并行度。把表中的数据分开放,当你查表里数据的时候写上分区信息,避免全表扫描; 是一个优化的方案。

分区表实际上就是对应一个 HDFS 文件系统上的独立的文件夹,该文件夹下是该分区所有的数据文件。Hive 中的分区就是分目录,把一个大的数据集根据业务需要分割成小的数据集。在查询时通过 WHERe 子句中的表达式选择查询所需要的指定的分区,这样的查询效率会提高很多。

2.创建分区表

​ 注:分区字段不能是表中已存在的数据, 可以将分区字段看做表的伪列

create table stu1(name string, age int, score int) partitioned by (site string)
    row format delimited fields terminated by ",";
3.导入数据 3-1. 静态分区

​ 注: 分区表加载数据时, 必须指定分区, 这种指定的方式叫做静态分区

3-1-1.从文件导入

​ 注:只能指定分区

load data local inpath "/root/data/day11/stu.txt" into table stu1 partition (site = "北京昌平");  #分区字段 数据应在最后位置

3-1-2. 从表导入
insert into stu1 partition (site="河南濮阳")
    (select s.name, s.age, s.score from stu s where s.site = "河南濮阳");

3-2. 动态分区

我们有的时候并不是一开始就知道数据要去哪个分区,最终的数据是要根据查询条件进入分区的。这种就叫做动态分区。所以一定是先查到结果,在把查到的结果填充到分区表中。以往的load模式就补满足我们的需求。
所以它是一个insert+select的模式;

insert into stu1 partition (site)
    select s.name, s.age, s.score, s.site from stu s;

​ 注:分区字段写在最
​ 设置为非严格模式(动态分区的模式,默认 strict,表示必须指定至少一个分区为静态分区,nonstrict 模式表示允许所有的分区字段都可以使用动态分区。

set hive.exec.dynamic.partition.mode="nonstrict";

4.查看分区 4-1 查看分区数量
show partitions stu1;
4-2 查看分区表结构
desc formatted stu1;
4-3单分区查看
select * from dept_partition where site='河南';
4-2:union多分区联合查看
select * from dept_partition where site='河南' union
select * from dept_partition where site='山东' union
select * from dept_partition where site='湖北';

select * from dept_partition where site='河南' or site='山东' or site='湖北';
5.二级分区


​ 建表

create table stu1(name string, age int, score int) partitioned by (state string, city string)
    row format delimited fields terminated by ",";

​ 导入数据

insert into stu1 partition (state,city)
    select s.name, s.age, s.score, s.state, s.city from stu s;
6.增加分区
alter table stu1 add partition (state="中国",city="天津");

增加多个分区, 用空格分割:

alter table stu1 add partition (state="中国",city="天津") partition (state="中国",city="山东");
7.删除分区
alter table stu1 drop partition (state="中国",city="天津");

删除多个分区, 用逗号分割

alter table stu1 drop partition (state="中国",city="天津"),partition (state="中国",city="山东");

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5715991.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存