内部分区表——头歌

张裕  金牌会员 | 2024-11-26 13:26:54 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 566|帖子 566|积分 1698

使命形貌

本关使命:根据相关知识内容实现 Hive 内部分区表的操纵。
相关知识

为了完本钱关使命,你必要掌握: 1.内部分区表的创建 2.增长与删除分区 2.相关表的操纵
分区表概述
分区表现实上就是对应一个 HDFS 文件系统上的独立的文件夹,该文件夹下是该分区所有的数据文件。Hive 中的分区就是分目录,把一个大的数据集根据业务必要分割成小的数据集。这样就能使查询效率提拔许多,并且便于对数据举行管理。
创建内部分区表
通过PARTITIONED BY子句指定,分区的顺序决定了谁是父目录,谁是子目录。
   注意:分区字段不能是表中已经存在的数据,可以将分区字段看作表的伪列。
  

  • 单分区创建
  1. [/code] [list=1]
  2. [*]CREATE TABLE IF NOT EXISTS part_test1(
  3. [*]id int,
  4. [*]name string
  5. [*])
  6. [*]PARTITIONED BY (year string) ## 分区
  7. [*]ROW FORMAT DELIMITED FIELDS TERMINATED BY ' ' ## 列分隔符
  8. [*]STORED AS TEXTFILE; ## 存储类型
  9. [/list]
  10. [list]
  11. [*]多分区创建
  12. [/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。


  • load 加载
  1. [/code] [list=1]
  2. [*]load data local inpath '/root/test.txt' into table part_test1 partition(year='2022');
  3. [/list] 增长分区
  4. 通过add partition的方式添加新的分区。
  5. [list]
  6. [*]创建单个分区 [code]
复制代码

  • alter table part_test2 add partition(month='202203',day='20220301');

  • 同时创建多个分区
    1. [/code][list=1]
    2. [*]alter table part_test1 add partition(year='2021') partition(year='2022');
    3. [/list]
    4. [/list] 删除分区
    5. 通过drop partition的方式删除分区。
    6. [list]
    7. [*]创建单个分区 [code]
    复制代码

    • alter table part_test2 drop partition(month='202203',day='20220301');

  • 同时创建多个分区
    1. [/code][list=1]
    2. [*]alter table part_test1 drop partition(year='2021') partition(year='2022');
    3. [/list]
    4. [/list] 检察分区表分区
    5. 通过show partitions检察part_test1表的分区环境
    6. [code]
    复制代码

    • show partitions part_test1;
    编程要求

    请根据右侧命令行内的提示,在Begin - End区域内举行sql语句代码增补,具体使命如下:


    • 创建内部分区表:student;设置分区字段为month,类型为string
    • 加载表数据时指定分区为2022-03
    • 同时创建student表的两个新分区:2022-04,2022-05
    • 删除分区:2022-04
    • 检察student表的分区环境
    • 查询student表数据
    student表布局:
    INFOTYPEidintnamestringageintsexstring 部分数据如下:
    1. [/code] [list=1]
    2. [*]202201,Anne,18,female
    3. [*]202202,Tom,20,male
    4. [*]202203,Jack,19,male
    5. [/list] 数据切分方式:逗号(,)
    6. 数据所在目录:/root/student.txt
    7. [size=3]测试阐明[/size]
    8. 平台会对你编写的代码举行测试:
    9. 预期输出:
    10. [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

    开始你的使命吧,祝你成功!
    代码如下:
    1. ---创建mydb数据库
    2. create database if not exists mydb;
    3. ---使用mydb数据库
    4. use mydb;
    5. ---------- Begin ----------
    6. ---创建student内部分区表
    7. CREATE TABLE IF NOT EXISTS student(   
    8.   id    int,   
    9.   name    string,  
    10.   age int,  
    11.   sex string  
    12. )   
    13. PARTITIONED BY (month string)     
    14. ROW FORMAT DELIMITED FIELDS TERMINATED BY ','     
    15. STORED AS TEXTFILE;
    16. ---导入数据:/root/student.txt
    17. load data local inpath '/root/student.txt' into table student partition(month='2022-03');
    18. ---同时创建student表的两个新分区
    19. alter table student add partition(month='2022-04') partition(month='2022-05');
    20. ---删除student表的分区
    21. alter table student drop partition(month='2022-04');
    22. ---查看student表分区情况
    23. show partitions student;
    24. ---查询student表数据
    25. select * from student;
    26. ---------- End ----------
    27. ---清空student表
    28. truncate table student;
    29. ---删除student表
    30. drop table student;
    复制代码
    免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
  • 回复

    使用道具 举报

    0 个回复

    倒序浏览

    快速回复

    您需要登录后才可以回帖 登录 or 立即注册

    本版积分规则

    张裕

    金牌会员
    这个人很懒什么都没写!

    标签云

    快速回复 返回顶部 返回列表