当前位置:网站首页>分区的使用及案例
分区的使用及案例
2022-07-26 22:40:00 【不会打球的摄影师不是好程序员】
分区表的使用
为什么使用分区
在使用select查询数据时,如果不限定条件那么会对整个表进行扫描,但是有很多数据并不是我们想要的,这样会消耗大量的时间。
分区表对应到hdfs上就是一个一个独立的文件夹(后面会有案例),在查找数据的时候我们只需要对这个文件夹中的数据进行扫面即可,大大提高了查询的效率。分区又分为静态分区和动态分区,后面会着重介绍两者的区别。
下面从一个简单的实例来了解分区:
静态分区
1.创建分区表:
CREATE table student(id int, name string, age int)
PARTITIONED by(dt string)
ROW format delimited fields terminated by ','
STORED as textfile;
2.插入数据
1,zs,18
2,li,19
3,ws,20
load data local inpath '/opt/data/student.txt' overwrite into table student partition(dt='2022-07-20');
insert into table student partition(dt='2022-07-21') values(4,'qq',21);
insert into table student partition(dt='2022-07-22') values(5,'ww',22);
insert into table student partition(dt='2022-07-23') values(6,'ee',23);
3.查询表中的数据
hive > select * from student;
OK
student.id student.name student.age student.dt
1 zs 18 2022-07-20
2 li 19 2022-07-20
3 ws 20 2022-07-20
4 qq 21 2022-07-21
5 ww 22 2022-07-22
6 ee 23 2022-07-23
Time taken: 0.293 seconds, Fetched: 6 row(s)
查询某个分区内的数据:
hive > select * from student where dt = '2022-07-20';
OK
student.id student.name student.age student.dt
1 zs 18 2022-07-20
2 li 19 2022-07-20
3 ws 20 2022-07-20
Time taken: 0.361 seconds, Fetched: 3 row(s)
4.(1)查看表中的分区
hive > show partitions student;
OK
partition
dt=2022-07-20
dt=2022-07-21
dt=2022-07-22
dt=2022-07-23
Time taken: 0.104 seconds, Fetched: 4 row(s)
(2)查看hdfs对应的目录结构

可以看到不同的分区在hdfs上对应着不同的目录
动态分区:
创建分区表:
create table student_dync(
id int,
name string,
age int
) partitioned by(dt string)
row format delimited fields terminated by ','
stored as textfile;
向表中插入数据:
set hive.exec.dynamic.partition.mode=nonstrict;
insert overwrite table student_dync partition (dt)
select
id,
name,
age,
dt
from student_tmp;
在向表中插入数据的时候并没有指定分区,然后查看下表中的分区:
hive > show partitions student_dync;
OK
partition
dt=2022-08-01
dt=2022-08-02
dt=2022-08-03
dt=2022-08-04
Time taken: 0.058 seconds, Fetched: 4 row(s)

也同样能够看到不同的分区对应着不同的目录。
总结 :动态分区和静态分区的区别
1.静态分区列是在编译时期通过手动指定的分区来确定的,动态分区是在sql执行的时候才能够确定。
2.由于在使用是需要手动指定分区,因此静态分区适用于分区数量较少、分区名明确的数据
3.动态分区适合分区数量多的情况,动态分区的分区字段位置在最后,多个分区字段按照分区顺序放置
4.插入数据时,静态分区支持load和insert两种方式,而动态分区只支持insert的方式
边栏推荐
- [3. VIM operation]
- VMware Workstation 虚拟机启动就直接蓝屏重启问题解决
- [HarekazeCTF2019]encode_and_encode
- Programmers must do 50 questions
- Use of postman
- [RootersCTF2019]I_< 3_ Flask
- Use csrftester to automatically detect CSRF vulnerabilities
- flinksql 窗口提前触发
- 【Codeforces Round #808 (Div 2.) A·B·C】
- [Network Research Institute] attackers scan 1.6 million WordPress websites to find vulnerable plug-ins
猜你喜欢
随机推荐
当事务遇上分布式锁
2022.7.14DAY604
Two methods of automated testing XSS vulnerabilities using burpsuite
[SQL注入] 联合查询
MYSQL分表DDL操作(存储过程)
Export and import in ES6
Application of encoding in XSS
golang实现AES有五种加密模式函数,Encrypt加解密字符串输出
Yolo of Darknet_ Forward of layer_ yolo_ Layer comments
Valueerror: the device should not be 'GPU', since paddepaddle is not compiled with CUDA
Crop TIF image
关于Thymeleaf的表达式
JSCORE day_01(6.30) RegExp 、 Function
Promise基本用法 20211130
FlinkSql多表(三表) join/interval join
JSCORE day_02(7.1)
Dataframe of sparksql
[SQL注入] 扩展注入手法
[CTF 真题] 2018-网鼎杯-Web-Unfinish
DOM day_02(7.8)网页制作流程、图片src属性、轮播图、自定义属性、标签栏、输入框事件、勾选操作、访问器语法


![[b01lers2020]Welcome to Earth](/img/e7/c8c0427b95022fbdf7bf2128c469c0.png)

![[CTF攻防世界] WEB区 关于备份的题目](/img/af/b78eb3522160896d77d9e82f7e7810.png)
![[By Pass] 文件上传的绕过方式](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)



