怎么理解hive分区partition

56次阅读
没有评论

共计 3719 个字符,预计需要花费 10 分钟才能阅读完成。

本篇文章给大家分享的是有关怎么理解 hive 分区 partition,丸趣 TV 小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着丸趣 TV 小编一起来看看吧。

一、背景

1、在 Hive Select 查询中一般会扫描整个表内容,会消耗很多时间做没必要的工作。有时候只需要扫描表中关心的一部分数据,因此建表时引入了 partition 概念。

2、分区表指的是在创建表时指定的 partition 的分区空间。

3、如果需要创建有分区的表,需要在 create 表的时候调用可选参数 partitioned by,详见表创建的语法结构。

二、技术细节

1、一个表可以拥有一个或者多个分区,每个分区以文件夹的形式单独存在表文件夹的目录下。

2、表和列名不区分大小写。

3、分区是以字段的形式在表结构中存在,通过 describe table 命令可以查看到字段存在,但是该字段不存放实际的数据内容,仅仅是分区的表示。

4、建表的语法(建分区可参见 PARTITIONED BY 参数):

CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name [(col_name data_type [COMMENT col_comment], …)] [COMMENT table_comment] [PARTITIONED BY (col_name data_type [COMMENT col_comment], …)] [CLUSTERED BY (col_name, col_name, …) [SORTED BY (col_name [ASC|DESC], …)] INTO num_buckets BUCKETS] [ROW FORMAT row_format] [STORED AS file_format] [LOCATION hdfs_path]

5、分区建表分为 2 种,一种是单分区,也就是说在表文件夹目录下只有一级文件夹目录。另外一种是多分区,表文件夹下出现多文件夹嵌套模式。

a、单分区建表语句:create table day_table (id int, content string) partitioned by (dt string); 单分区表,按天分区,在表结构中存在 id,content,dt 三列。

b、双分区建表语句:create table day_hour_table (id int, content string) partitioned by (dt string, hour string); 双分区表,按天和小时分区,在表结构中新增加了 dt 和 hour 两列。

怎么理解 hive 分区 partition

表文件夹目录示意图(多分区表):

6、添加分区表语法(表已创建,在此基础上添加分区):

ALTER TABLE table_name ADD partition_spec [LOCATION location1] partition_spec [LOCATION location2] … partition_spec: : PARTITION (partition_col = partition_col_value, partition_col = partiton_col_value, …)

用户可以用  ALTER TABLE ADD PARTITION  来向一个表中增加分区。当分区名是字符串时加引号。例:

ALTER TABLE day_table ADD PARTITION (dt= 2008-08-08 , hour= 08) location /path/pv1.txt PARTITION (dt= 2008-08-08 , hour= 09) location /path/pv2.txt

7、删除分区语法:

ALTER TABLE table_name DROP partition_spec, partition_spec,…

用户可以用  ALTER TABLE DROP PARTITION  来删除分区。分区的元数据和数据将被一并删除。例:

ALTER TABLE day_hour_table DROP PARTITION (dt= 2008-08-08 , hour= 09

8、数据加载进分区表中语法:

LOAD DATA [LOCAL] INPATH filepath [OVERWRITE] INTO TABLE tablename [PARTITION (partcol1=val1, partcol2=val2 …)]

例:

LOAD DATA INPATH /user/pv.txt INTO TABLE day_hour_table PARTITION(dt= 2008-08- 08 , hour= 08 LOAD DATA local INPATH /user/hua/* INTO TABLE day_hour partition(dt= 2010-07- 07

当数据被加载至表中时,不会对数据进行任何转换。Load 操作只是将数据复制至 Hive 表对应的位置。数据加载时在表下自动创建一个目录,文件存放在该分区下。

9、基于分区的查询的语句:

SELECT day_table.* FROM day_table WHERE day_table.dt = 2008-08-08

10、查看分区语句:

hive show partitions day_hour_table; OK dt=2008-08-08/hour=08 dt=2008-08-08/hour=09 dt=2008-08-09/hour=09

三、总结

1、在  Hive  中,表中的一个  Partition  对应于表下的一个目录,所有的  Partition  的数据都存储在最字集的目录中。

2、总的说来 partition 就是辅助查询,缩小查询范围,加快数据的检索速度和对数据按照一定的规格和条件进行管理。

——————————————————————————————————————

hive 中关于 partition 的操作:hive  create table mp (a string) partitioned by (b string, c string);
OK
Time taken: 0.044 seconds
hive  alter table mp add partition (b= 1 , c= 1
OK
Time taken: 0.079 seconds
hive alter table mp add partition (b= 1 , c= 2
OK
Time taken: 0.052 seconds
hive alter table mp add partition (b= 2 , c= 2
OK
Time taken: 0.056 seconds
hive  show partitions mp ;
OK
b=1/c=1
b=1/c=2
b=2/c=2
Time taken: 0.046 seconds
hive  explain extended alter table mp drop partition (b= 1
OK
ABSTRACT SYNTAX TREE:
 (TOK_ALTERTABLE_DROPPARTS mp (TOK_PARTSPEC (TOK_PARTVAL b 1)))

STAGE DEPENDENCIES:
 Stage-0 is a root stage

STAGE PLANS:
 Stage: Stage-0
     Drop Table Operator:
     Drop Table
        table: mp

Time taken: 0.048 seconds
hive alter table mp drop partition (b= 1
FAILED: Error in metadata: table is partitioned but partition spec is not specified or tab: {b=1}
FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask
hive show partitions mp ;
OK
b=1/c=1
b=1/c=2
b=2/c=2
Time taken: 0.044 seconds
hive  alter table mp add   partition (b= 1 , c = 3) partition (b= 1 , c= 4
OK
Time taken: 0.168 seconds
hive show partitions mp ;
OK
b=1/c=1
b=1/c=2
b=1/c=3
b=1/c=4
b=2/c=2
b=2/c=3
Time taken: 0.066 seconds
hive insert overwrite table mp partition (b= 1 , c= 1) select cnt from tmp_et3 ;

hive alter table mp add columns (newcol string);

location 指定目录结构
hive alter table alter2 add partition (insertdate= 2008-01-01) location 2008/01/01

hive alter table alter2 add partition (insertdate= 2008-01-02) location 2008/01/02

以上就是怎么理解 hive 分区 partition,丸趣 TV 小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注丸趣 TV 行业资讯频道。

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-08-25发表,共计3719字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)