在 Hive 中,“合并小文件”通常出现在两个场景:
下面按场景说明常见做法。
-- 在 Map 输出阶段合并
set hive.merge.mapfiles=true;
-- 在 Reduce 输出阶段合并
set hive.merge.mapredfiles=true;
-- 合并后文件大小(单位:字节,下面示例为 128MB)
set hive.merge.size.per.task=134217728;
-- 平均文件大小小于该值时触发合并
set hive.merge.smallfiles.avgsize=134217728;
适用场景:
INSERT OVERWRITE TABLE target_table
SELECT *
FROM source_table
DISTRIBUTE BY rand();
或按业务字段:
DISTRIBUTE BY dt, province;
目的:
set mapreduce.job.reduces=10;
或:
set hive.exec.reducers.bytes.per.reducer=256000000;
减少 Reducer ⇒ 减少文件数
INSERT OVERWRITE TABLE my_table
SELECT * FROM my_table;
配合合并参数:
set hive.merge.mapfiles=true;
set hive.merge.mapredfiles=true;
set hive.merge.size.per.task=134217728;
⚠ 注意:
INSERT OVERWRITE TABLE my_table
PARTITION (dt='2024-01-01')
SELECT
col1, col2, col3
FROM my_table
WHERE dt='2024-01-01';
-- 建临时表(结构一致)
CREATE TABLE tmp_table LIKE my_table;
-- 写数据
INSERT INTO tmp_table
SELECT * FROM my_table;
-- 改名替换
ALTER TABLE my_table RENAME TO my_table_bak;
ALTER TABLE tmp_table RENAME TO my_table;
适合:
SET hive.exec.compress.output=true;
SET mapreduce.output.fileoutputformat.compress.codec=org.apache.hadoop.io.compress.SnappyCodec;
INSERT OVERWRITE TABLE orc_table
SELECT * FROM orc_table;
ORC 本身支持:
set hive.execution.engine=tez;
set tez.am.resource.memory.mb=2048;
SET spark.sql.files.maxPartitionBytes=134217728;
✅ 写时合并
✅ 控制 Reducer
✅ 按分区治理
✅ ORC + 压缩
❌ 放任小文件增长
如果你愿意,可以告诉我:
我可以给你一套针对性的合并方案。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。