You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用spark-sql执行Hive动态分区插入脚本遇SparkException报错

Spark SQL执行Hive动态分区插入报错解决

问题现象

用spark-sql -f insert_script替代hive -f insert_script执行Hive表全动态分区插入时,尽管已在脚本中设置分区模式为非严格,仍触发报错:

org.apache.spark.SparkException: Dynamic partition strict mode requires at least one static partition column. To turn this off set hive.exec.dynamic.partition.mode=nonstrict

用户的SQL脚本片段:

set spark.hadoop.hive.exec.dynamic.partition.mode=true;
set spark.hadoop.hive.exec.dynamic.partition.mode=nonstrict;
set tez.am.resource.memory.mb=4098;
set spark.hadoop.hive.tez.container.size=4098;
set spark.hadoop.hive.optimize.sort.dynamic.partition=false;

INSERT OVERWRITE TABLE stackdatabase.stacktable
Partition(year, month, day, job_id)
SELECT
tickersymbol,
S&Ptickersymbol,
DOWticersymbol,
processdate,
....
...
year, 
month, 
day, 
job_id
FROM stackdatabase.bkp_stacktable

问题原因

Spark SQL脚本中配置Hive参数时错误添加了spark.hadoop.前缀。该前缀仅适用于Spark代码或spark-submit的--conf参数,在spark-sql脚本中应直接使用Hive原生参数名,否则配置无法生效,导致分区模式仍处于默认的严格模式。

解决方案

修改脚本中的参数配置,移除spark.hadoop.前缀,并清理重复的参数设置,修改后的脚本如下:

set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nonstrict;
set tez.am.resource.memory.mb=4098;
set hive.tez.container.size=4098;
set hive.optimize.sort.dynamic.partition=false;

INSERT OVERWRITE TABLE stackdatabase.stacktable
Partition(year, month, day, job_id)
SELECT
tickersymbol,
S&Ptickersymbol,
DOWticersymbol,
processdate,
....
...
year, 
month, 
day, 
job_id
FROM stackdatabase.bkp_stacktable

补充说明

  • 在spark-sql交互式命令或脚本中,Hive参数的配置方式与原生Hive完全一致,无需添加Spark相关前缀
  • 需确保hive.exec.dynamic.partition设置为true(部分环境默认开启,显式设置更稳妥),同时mode=nonstrict允许全动态分区插入

内容的提问来源于stack exchange,提问作者freshman_2021

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 06:54:25