使命形貌
本关使命:根据相关知识内容实现 Hive 内部分区表的操纵。
相关知识
为了完本钱关使命,你必要掌握: 1.内部分区表的创建 2.增长与删除分区 2.相关表的操纵
分区表概述
分区表现实上就是对应一个 HDFS 文件系统上的独立的文件夹,该文件夹下是该分区所有的数据文件。Hive 中的分区就是分目录,把一个大的数据集根据业务必要分割成小的数据集。这样就能使查询效率提拔许多,并且便于对数据举行管理。
创建内部分区表
通过PARTITIONED BY子句指定,分区的顺序决定了谁是父目录,谁是子目录。
注意:分区字段不能是表中已经存在的数据,可以将分区字段看作表的伪列。
- [/code] [list=1]
- [*]CREATE TABLE IF NOT EXISTS part_test1(
- [*]id int,
- [*]name string
- [*])
- [*]PARTITIONED BY (year string) ## 分区
- [*]ROW FORMAT DELIMITED FIELDS TERMINATED BY ' ' ## 列分隔符
- [*]STORED AS TEXTFILE; ## 存储类型
- [/list]
- [list]
- [*]多分区创建
- [/list] [code]
复制代码
- CREATE TABLE IF NOT EXISTS part_test2(
- id int,
- name string
- )
- PARTITIONED BY (month string,day string) ## 分区
- ROW FORMAT DELIMITED FIELDS TERMINATED BY ' ' ## 列分隔符
- STORED AS TEXTFILE; ## 存储类型
加载数据到分区表中
通过load加载将本地的/root/test.txt文本数据加载到part_test1表中,并且指定partition分区字段year为2022。
- [/code] [list=1]
- [*]load data local inpath '/root/test.txt' into table part_test1 partition(year='2022');
- [/list] 增长分区
- 通过add partition的方式添加新的分区。
-
- [list]
- [*]创建单个分区 [code]
复制代码
- alter table part_test2 add partition(month='202203',day='20220301');
同时创建多个分区- [/code][list=1]
- [*]alter table part_test1 add partition(year='2021') partition(year='2022');
- [/list]
- [/list] 删除分区
- 通过drop partition的方式删除分区。
-
- [list]
- [*]创建单个分区 [code]
复制代码
- alter table part_test2 drop partition(month='202203',day='20220301');
同时创建多个分区- [/code][list=1]
- [*]alter table part_test1 drop partition(year='2021') partition(year='2022');
- [/list]
- [/list] 检察分区表分区
- 通过show partitions检察part_test1表的分区环境
- [code]
复制代码
- show partitions part_test1;
编程要求
请根据右侧命令行内的提示,在Begin - End区域内举行sql语句代码增补,具体使命如下:
- 创建内部分区表:student;设置分区字段为month,类型为string
- 加载表数据时指定分区为2022-03
- 同时创建student表的两个新分区:2022-04,2022-05
- 删除分区:2022-04
- 检察student表的分区环境
- 查询student表数据
student表布局:
INFOTYPEidintnamestringageintsexstring 部分数据如下:
- [/code] [list=1]
- [*]202201,Anne,18,female
- [*]202202,Tom,20,male
- [*]202203,Jack,19,male
- [/list] 数据切分方式:逗号(,)
- 数据所在目录:/root/student.txt
- [size=3]测试阐明[/size]
- 平台会对你编写的代码举行测试:
- 预期输出:
- [code]
复制代码
- month=2022-03
- month=2022-05
- 202201 Anne 18 female 2022-03
- 202202 Tom 20 male 2022-03
- 202203 Jack 19 male 2022-03
- 202204 xiaoming 20 male 2022-03
- 202205 anni 19 female 2022-03
开始你的使命吧,祝你成功!
代码如下:
- ---创建mydb数据库
- create database if not exists mydb;
- ---使用mydb数据库
- use mydb;
- ---------- Begin ----------
- ---创建student内部分区表
- CREATE TABLE IF NOT EXISTS student(
- id int,
- name string,
- age int,
- sex string
- )
- PARTITIONED BY (month string)
- ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
- STORED AS TEXTFILE;
- ---导入数据:/root/student.txt
- load data local inpath '/root/student.txt' into table student partition(month='2022-03');
- ---同时创建student表的两个新分区
- alter table student add partition(month='2022-04') partition(month='2022-05');
- ---删除student表的分区
- alter table student drop partition(month='2022-04');
- ---查看student表分区情况
- show partitions student;
- ---查询student表数据
- select * from student;
- ---------- End ----------
- ---清空student表
- truncate table student;
- ---删除student表
- drop table student;
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |