9.3.9 动态分区调整

关系型数据库中,对分区表Insert数据时候,数据库自动会根据分区字段的值,将数据插入到相应的分区中.

Hive 中也提供了类似的机制,即动态分区 (Dynamic Partition),只不过,使用Hive 的动态分区,需要进行相应的配置。

我们前面学习的手动指定分区叫做静态分区.

9.3.9.1 参数设置

参数1: 开启动态分区功能

属性:hive.exec.dynamic.partition可以控制. 默认是true, 表示已经开启.

set hive.exec.dynamic.partition=true;

参数2: 设置动态分区的模式

动态分区的模式有: nonstrictstrict

严格模式下, 用户必须至少指定一个静态分区.

非严格模式下, 所有的分区都可以是动态的.

我们把分区模式指定为:

set hive.exec.dynamic.partition.mode=nonstrict;

参数3: 设置允许的动态分区数的上限

set hive.exec.max.dynamic.partitions=1000;

参数4: 允许创建的 HDFS 文件的上限

set hive.exec.max.created.files=100000;

参数5: 当有空分区的时候是否需要抛出异常.

一般都是不抛出异常

set hive.error.on.empty.partition=false;

9.3.9.2 实例操作

步骤1: 创建分区表

create table ori_partitioned(
    id bigint, 
    time bigint, 
    uid string, 
    keyword string,
    url_rank int, 
    click_num int, 
    click_url string
) 
partitioned by (p_time bigint) 
row format delimited fields terminated by '\t';

步骤2: 加载数据到分区表

load data local inpath '/opt/module/datas/ds/ds1' into table ori_partitioned 
partition(p_time='20111230000010');

load data local inpath '/opt/module/datas/ds/ds1' into table ori_partitioned 
partition(p_time='20111230000011');

步骤3: 创建目标分区表

一会从前面的分区表中查询数据, 然后插入到这个分区表中.

create table ori_partitioned_target(
    id bigint, 
    time bigint, 
    uid string,
    keyword string, 
    url_rank int, 
    click_num int, 
    click_url string
) 
PARTITIONED BY (p_time STRING) 
row format delimited fields terminated by '\t';

步骤4: 设置动态分区

# 开启动态分区
set hive.exec.dynamic.partition = true;
# 动态分区模式: strict 要求至少有一个静态分区. nonstrict 可以都是动态分区
set hive.exec.dynamic.partition.mode = nonstrict;
# 一个动态分区创建语句所能创建的最大分区数. 超过会抛出异常
set hive.exec.max.dynamic.partitions = 1000;
# 每个 mapper 或者 reducer 所允许创建的的最大分区数.超过会抛出异常
set hive.exec.max.dynamic.partitions.pernode = 100;
# 全局可以创建的文件个数.
set hive.exec.max.created.files = 100000;
# 如果有空分区是否抛出异常
set hive.error.on.empty.partition = false;
set hive.exec.dynamic.partition = true;
set hive.exec.dynamic.partition.mode = nonstrict;
set hive.exec.max.dynamic.partitions = 1000;
set hive.exec.max.dynamic.partitions.pernode = 100;
set hive.exec.max.created.files = 100000;
set hive.error.on.empty.partition = false;

步骤5: 向目标分区表插入数据

insert overwrite table ori_partitioned_target 
partition (p_time)
    select id, time, uid, keyword, url_rank, click_num, click_url, p_time 
    from ori_partitioned;

说明:

  • 动态分区是根据查询到的最后一个字段来自动分区.
  • 本例中 select...., p_time, 所以会按照p_time来动态分区.
  • partition (p_time) 这个分区名不一定必须和最后一个字段名相同.

步骤6: 查看分区情况

show partitions ori_partitioned_target;

Copyright © 尚硅谷大数据 2019 all right reserved,powered by Gitbook
该文件最后修订时间: 2019-03-16 07:15:56

results matching ""

    No results matching ""