升级Apache Spark 3.2.0后IntelliJ运行测试报Jackson方法不存在错误如何解决
问题根因
NoSuchMethodError报错的核心原因是版本不兼容:Spark 3.2.0默认依赖Jackson 2.12.x版本,其中JsonFormat$Value.empty()是Jackson 2.9版本才新增的API,而hive-exec 2.3.9内置的shaded Jackson 2.6.x版本不包含该方法,类加载时优先加载了低版本类导致报错。
解决方案
可根据业务场景选择对应方案:
方案1:排除hive-exec中的Jackson依赖(优先选择)
如果业务不需要用到hive-exec内置的Jackson功能,直接排除hive-exec传递引入的所有Jackson相关依赖即可。
Maven配置
在pom.xml中hive-exec的依赖节点添加排除规则:
<dependency> <groupId>org.apache.hive</groupId> <artifactId>hive-exec</artifactId> <version>2.3.9</version> <exclusions> <!-- 排除所有Jackson相关传递依赖 --> <exclusion> <groupId>com.fasterxml.jackson.*</groupId> <artifactId>*</artifactId> </exclusion> </exclusions> </dependency>
同时在properties中统一全局Jackson版本,和Spark 3.2.0默认依赖版本对齐:
<properties> <jackson.version>2.12.10</jackson.version> </properties>
Gradle配置
在build.gradle中添加排除和版本锁定规则:
// 排除hive-exec中的Jackson依赖 implementation('org.apache.hive:hive-exec:2.3.9') { exclude group: 'com.fasterxml.jackson.*' } // 全局锁定Jackson版本和Spark对齐 configurations.all { resolutionStrategy.force( "com.fasterxml.jackson.core:jackson-core:2.12.10", "com.fasterxml.jackson.core:jackson-annotations:2.12.10", "com.fasterxml.jackson.core:jackson-databind:2.12.10" ) }
方案2:重定向hive-exec的Jackson包路径(适用于排除依赖后hive功能异常的场景)
如果业务强依赖hive-exec内置的Jackson版本,排除依赖会导致hive功能报错,可以通过shade插件对hive-exec的Jackson包路径做重定向,避免和Spark的Jackson冲突。
以Maven Shade插件为例,配置如下:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.4.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <relocations> <relocation> <!-- 把hive内置的Jackson路径重定向到自定义路径 --> <pattern>com.fasterxml.jackson</pattern> <shadedPattern>custom.shaded.com.fasterxml.jackson</shadedPattern> <includes> <include>org.apache.hive:hive-exec</include> </includes> </relocation> </relocations> </configuration> </execution> </executions> </plugin>
方案3:IntelliJ运行测试适配
如果构建工具配置完成后,IDEA本地运行测试仍然报错,调整IDEA的类加载优先级:
- 打开
File > Project Structure > Modules > 对应业务模块 > Dependencies - 将Spark相关依赖的排序移动到hive-exec依赖之前,保证优先加载Spark提供的高版本Jackson
- 勾选依赖页签下的
Resolve conflicts using classpath of the module选项
内容的提问来源于stack exchange,提问作者igreenfield
相关产品推荐
相关产品推荐

