Hive partition 分区表

迈不过友情╰ 2022-04-25 04:14 432阅读 0赞

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

需求

需要根据日志产生的日期对日志进行管理。

数据准备

  1. [root@hadoop102 stu_part]# pwd
  2. /opt/module/hive-1.2.1/datas/stu_part
  3. [root@hadoop102 stu_part]# ll
  4. 总用量 16
  5. -rw-r--r--. 1 root root 33 4 3 17:54 20190429.txt
  6. -rw-r--r--. 1 root root 33 4 3 17:53 20190430.txt
  7. -rw-r--r--. 1 root root 33 4 3 17:54 20190501.txt
  8. -rw-r--r--. 1 root root 33 4 3 17:54 20190502.txt

创建分区表

  1. create table stu_part(id int,name string) partitioned by (month string) row format delimited fields terminated by '\t';

\-w1181

加载数据到分区表中

  1. hive (ylj_db)> load data local inpath '/opt/module/hive-1.2.1/datas/stu_part/20190429.txt' overwrite into table stu_part partition(month='201904');
  2. Loading data to table ylj_db.stu_part partition (month=201904)
  3. Partition ylj_db.stu_part{month=201904} stats: [numFiles=1, numRows=0, totalSize=33, rawDataSize=0]
  4. OK
  5. Time taken: 0.765 seconds
  6. hive (ylj_db)> load data local inpath '/opt/module/hive-1.2.1/datas/stu_part/20190502.txt' overwrite into table stu_part partition(month='201905');
  7. Loading data to table ylj_db.stu_part partition (month=201905)
  8. Partition ylj_db.stu_part{month=201905} stats: [numFiles=1, numRows=0, totalSize=33, rawDataSize=0]
  9. OK
  10. Time taken: 0.806 seconds

\-w1164
\-w1152

查询

本次查询使用HiveJDBC查询,返回格式好看☺️

全部查询

  1. 0: jdbc:hive2://hadoop102:10000> select * from stu_part;
  2. +--------------+----------------+-----------------+--+
  3. | stu_part.id | stu_part.name | stu_part.month |
  4. +--------------+----------------+-----------------+--+
  5. | 1 | zhangsan | 201904 |
  6. | 2 | lisi | 201904 |
  7. | 3 | houzi | 201904 |
  8. | 4 | tuzi | 201904 |
  9. | 1 | zhangsan | 201905 |
  10. | 2 | lisi | 201905 |
  11. | 3 | houzi | 201905 |
  12. | 4 | tuzi | 201905 |
  13. +--------------+----------------+-----------------+--+

单分区查询

  1. 0: jdbc:hive2://hadoop102:10000> select * from stu_part where month='201904';
  2. +--------------+----------------+-----------------+--+
  3. | stu_part.id | stu_part.name | stu_part.month |
  4. +--------------+----------------+-----------------+--+
  5. | 1 | zhangsan | 201904 |
  6. | 2 | lisi | 201904 |
  7. | 3 | houzi | 201904 |
  8. | 4 | tuzi | 201904 |
  9. +--------------+----------------+-----------------+--+
  10. 4 rows selected (0.503 seconds)

多分区联合查询

  1. select * from stu_part where month='201904'
  2. union
  3. select * from stu_part where month='201905'
  4. union
  5. select * from stu_part where month='201906';
  6. select * from stu_part where month='201904' or month='201905';

增加与删除分区

创建单个分区

  1. alter table stu_part add partition(month='201906') ;

同时创建多个分区

  1. alter table stu_part add partition(month='201907') partition(month='201908');

\-w1166

删除单个分区

  1. alter table stu_part drop partition (month='201906');

同时删除多个分区

  1. alter table stu_part drop partition (month='201907'),partition (month='201908');

查看分区表有多少分区

  1. hive (ylj_db)> show partitions stu_part;
  2. OK
  3. partition
  4. month=201904
  5. month=201905
  6. Time taken: 0.124 seconds, Fetched: 2 row(s)

二级分区表

创建

  1. create table stu_part2(id int,name string) partitioned by (month string,day string) row format delimited fields terminated by '\t';

正常的加载数据

  1. hive (ylj_db)> load data local inpath '/opt/module/hive-1.2.1/datas/stu_part/20190429.txt' overwrite into table stu_part2 partition(month='201904',day='29');
  2. Loading data to table ylj_db.stu_part2 partition (month=201904, day=29)
  3. Partition ylj_db.stu_part2{month=201904, day=29} stats: [numFiles=1, numRows=0, totalSize=33, rawDataSize=0]
  4. OK
  5. Time taken: 0.62 seconds

\-w1179

\-w1184

查询数据

  1. hive (ylj_db)> select * from stu_part2 where month='201904' and day='29';
  2. OK
  3. stu_part2.id stu_part2.name stu_part2.month stu_part2.day
  4. 1 zhangsan 201904 29
  5. 2 lisi 201904 29
  6. 3 houzi 201904 29
  7. 4 tuzi 201904 29
  8. Time taken: 0.244 seconds, Fetched: 4 row(s)

分区表和数据产生关联的三种方式

上传数据后修复

创建分区目录
  1. dfs -mkdir -p /user/hive/warehouse/ylj_db.db/stu_part/month=201906;

\-w1178

上传数据
  1. dfs -put /opt/module/hive-1.2.1/datas/stu_part/20190601.txt /user/hive/warehouse/ylj_db.db/stu_part/month=201906;

\-w1176

查询数据

刚上传的数据查询不到,因为没有对应的元数据信息。

  1. hive (ylj_db)> select * from stu_part where month='201906';
  2. OK
  3. stu_part.id stu_part.name stu_part.month
  4. Time taken: 0.16 seconds

执行修复命令

  1. hive (ylj_db)> msck repair table stu_part;
  2. OK
  3. Partitions not in metastore: stu_part:month=201906
  4. Repair: Added partition to metastore stu_part:month=201906
  5. Time taken: 0.343 seconds, Fetched: 2 row(s)
  6. hive (ylj_db)> select * from stu_part where month='201906';
  7. OK
  8. stu_part.id stu_part.name stu_part.month
  9. 1 zhangsan 201906
  10. 2 lisi 201906
  11. 3 houzi 201906
  12. 4 tuzi 201906
  13. Time taken: 0.147 seconds, Fetched: 4 row(s)

上传数据后添加分区

上传数据
  1. hive (ylj_db)> dfs -mkdir -p /user/hive/warehouse/ylj_db.db/stu_part/month=201907;
  2. hive (ylj_db)> dfs -put /opt/module/hive-1.2.1/datas/stu_part/20190701.txt /user/hive/warehouse/ylj_db.db/stu_part/month=201907;

\-w1178

执行添加分区
  1. alter table stu_part add partition(month='201907');
查询数据
  1. hive (ylj_db)> select * from stu_part where month='201907';
  2. OK
  3. stu_part.id stu_part.name stu_part.month
  4. 1 zhangsan 201907
  5. 2 lisi 201907
  6. 3 houzi 201907
  7. 4 tuzi 201907
  8. Time taken: 0.128 seconds, Fetched: 4 row(s)

创建文件夹后load数据到分区

创建目录
  1. dfs -mkdir -p /user/hive/warehouse/ylj_db.db/stu_part/month=201908;
load上传数据
  1. load data local inpath '/opt/module/hive-1.2.1/datas/stu_part/20190801.txt' into table stu_part partition(month='201908');
查询数据
  1. hive (ylj_db)> select * from stu_part where month='201908';
  2. OK
  3. stu_part.id stu_part.name stu_part.month
  4. 1 zhangsan 201908
  5. 2 lisi 201908
  6. 3 houzi 201908
  7. 4 tuzi 201908
  8. Time taken: 0.128 seconds, Fetched: 4 row(s)

发表评论

表情:
评论列表 (有 0 条评论,432人围观)

还没有评论,来说两句吧...

相关阅读

    相关 hive分区partition介绍

    一、简要概述 1. Hive分区更方便于数据管理,常见的有时间分区和业务分区。 二、hive分区原理 通过实例来理解Hive分区的原理: (一)多分区操作: 1.

    相关 hive分区

    分区表 1 为什么出现分区表? 假设有海量的数据保存在hdfs的某一个hive表明对应的目录下,使用hive进行操作的时候,往往会搜索这个目录下的所有文件,这有时会

    相关 Hive partition 分区

    分区表实际上就是对应一个HDFS文件系统上的独立的文件夹,该文件夹下是该分区所有的数据文件。Hive中的分区就是分目录,把一个大的数据集根据业务需要分割成小的数据集。在查询时通

    相关 hive 分区

    hive中创建分区表没有什么复杂的分区类型(范围分区、列表分区、hash分区、混合分区等)。分区列也不是表中的一个实际的字段,而是一个或者多个伪列。意思是说在表的数据文件中实际