数仓-hive DDL (带你手敲秒懂hive三种常见分区)

hive 数仓DDL 分区

分区是将表的数据以分区字段的值作为目录去存储

---> 减少磁盘IO, 方便数据管理

静态分区

  • 创建外表同时指定静态分区字段

    create  table if not exists table_name(id int,
                                          name string
                                         )
                                         partitioned by (day string,h string);
                                     
  • 添加静态分区

    alter table table_name add  if not exists partition (day='2024-03-30');

  • 单分区的数据写入

    insert overwrite table table_name partition (day="2024-03-03")
    select id,name from table_name limit 10;

  • 多分区数据写入

    insert overwrite table table_name partition (day="20240203",hour="14");

  • 查看分区数据

    select * from table_name    where day="2024-03-30";
    select * from table_name where day="20240203" and hour="14";

  • 查看分区

    show partitions talbe_name;

动态分区

  • 使用动态分区之前要开启动态分区

    # hive默认是关闭动态分区的,所以先开启允许所有分区是动态的,否则要先静态再动态
    set hive.exec.dynamic.partition=true;
    set hive.exec.dynamic.partition.mode=nonstrict;
     
  • 写入数据

    # 原表中字段为id,name,create_time
    # 动态分区是根据原表字段进行表达式的转化得到的动态分区字段的value
    insert overwrite table table_name partition(day)
    select id ,name ,create_time,
    date_format(create_time,"yyyy-MM-dd") as day
    from info;  

动静混合-> 半动半静

# 对于这种混合分区,静态字段必须再前,动态字段要在后面 -> must
insert overwrite table tableTest partition(d="date",day)
select id,name,create_time,
date_format(create_time,"yyyy-mm-dd") as day
from info;
另外对于动态分区其余相关参数的设置:

# 规定在一个MR任务中hive允许创建最大分区数值1024,避免在进行动态分区时产生过多的小文件分区,造成数据倾斜问题,超过就会终止任务
SET hive.exec.max.dynamic.partitions=1024; 

# 规定在一个节点上可以创建的最大分区数量,超过就会抛异常
SET hive.exec.max.dynamic.partitions.pernode=128; 

# 规定hive运行中创建最大文件数限制,超过程序任务即失败
SET hive.exec.max.created.files=10000; 

# 这里设置为true,程序会在某个动态分区为null时,hive将在完成最后一个任务后报一个错误,
# 如果不想因为null动态分区影响整个hive的动态分区,就设置为false即可
SET hive.error.on.empty.partition=true;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值