Apache Doris 动态分区使用示例
一、Doris分区概述
在某些使用场景下,用户会将表按照天进行分区划分,每天定时执行例行任务,这时需要使用方手动管理分区,否则可能由于使用方没有创建分区导致数据导入失败,这给使用方带来了额外的维护成本。
通过动态分区功能,用户可以在建表时设定动态分区的规则。FE 会启动一个后台线程,根据用户指定的规则创建或删除分区。用户也可以在运行时对现有规则进行变更。动态分区数默认最大500(参数max_dynamic_partition_num (fe.conf))
Doris分区官方手册:https://doris.apache.org/master/zh-CN/administrator-guide/dynamic-partition.html
一、动态分区创建示例
DROP TABLE IF EXISTS test_dynamic_partition;
CREATE TABLE test_dynamic_partition (
partition_key int(11) NOT NULL COMMENT '分区键',
app_code varchar(64) NOT NULL COMMENT '应用编码',
app_name varchar(255) NULL COMMENT '应用名称'
)
ENGINE=OLAP
DUPLICATE KEY(partition_key,app_code,app_name)
COMMENT '动态分区示例'
PARTITION BY RANGE(partition_key) ()
DISTRIBUTED BY HASH(partition_key)
PROPERTIES
(
"dynamic_partition.create_history_partition" = "true",
"dynamic_partition.history_partition_num" = "3",
"dynamic_partition.enable" = "true",
"dynamic_partition.time_unit" = "DAY",
"dynamic_partition.start" = "-400",
"dynamic_partition.end" = "3",
"dynamic_partition.prefix" = "p",
"dynamic_partition.buckets" = "1"
);
二、分区信息查看和修改
#查看动态分区表调度情况
SHOW DYNAMIC PARTITION TABLES;
#展示指定db下指定表的所有非临时分区信息
SHOW PARTITIONS FROM test_dynamic_partition;
#修改分区参数
ALTER TABLE test_dynamic_partition
SET ("dynamic_partition.end" = "4");
#清空表 tbl 的 p20211101 和 p20211102 分区
TRUNCATE TABLE tbl PARTITION(p20211101, p20211102);
#修改单分区表的实际副本数量(只限单分区表):
ALTER TABLE example_db.my_table
SET ("replication_num" = "3");
#修改表的所有分区:
ALTER TABLE example_db.my_table
MODIFY PARTITION(*)
SET ("replication_num" = "3");
ALTER TABLE example_db.my_table
MODIFY PARTITION (*) SET("storage_medium"="HDD");
#修改表的动态分区属性(支持未添加动态分区属性的表添加动态分区属性)
ALTER TABLE example_db.my_table set ("dynamic_partition.enable" = "false");
#如果需要在未添加动态分区属性的表中添加动态分区属性,则需要指定所有的动态分区属性
ALTER TABLE example_db.my_table set (
"dynamic_partition.enable" = "true",
"dynamic_partition.time_unit" = "DAY",
"dynamic_partition.end" = "3",
"dynamic_partition.prefix" = "p",
"dynamic_partition.buckets" = "32"
);