Hadoop: how to resolve java.lang.VerifyError: Bad type on operand stack

问题描述

一般是在使用Maven配置Hadoop开发环境时,出现这种错误。主要症状是在点击“调试”或“运行”Hadoop project时提示错误

Exception in thread "main" java.lang.VerifyError: Bad type on operand stack
Exception Details:
  Location:
    org/apache/hadoop/mapred/JobTrackerInstrumentation.create(Lorg/apache/hadoop/mapred/JobTracker;Lorg/apache/hadoop/mapred/JobConf;)Lorg/apache/hadoop/mapred/JobTrackerInstrumentation; @5: invokestatic
  Reason:
    Type 'org/apache/hadoop/metrics2/lib/DefaultMetricsSystem' (current frame, stack[2]) is not assignable to 'org/apache/hadoop/metrics2/MetricsSystem'
  Current Frame:
    bci: @5
    flags: { }
    locals: { 'org/apache/hadoop/mapred/JobTracker', 'org/apache/hadoop/mapred/JobConf' }
    stack: { 'org/apache/hadoop/mapred/JobTracker', 'org/apache/hadoop/mapred/JobConf', 'org/apache/hadoop/metrics2/lib/DefaultMetricsSystem' }
  Bytecode:
    0000000: 2a2b b200 03b8 0004 b0

原因分析

可能是由于Hadoop依赖混乱或依赖顺序错误,导致Hadoop运行出错。

解决方法

移除不必要的依赖

首先尝试在Maven的pom.xml配置中移除不必要的Hadoop依赖。

基础依赖:

hadoop-core
hadoop-common

需要读取HDFS,需要依赖:

hadoop-hdfs
hadoop-client

需要读取HBase,需要依赖:

hbase-client

上述依赖中如有不需要,则将其从Maven依赖中去除。

尝试调整依赖顺序

这个一般出现在hadoop-corehadoop-common之间。

尝试在pom.xml中调整依赖的相互位置,如将hadoop-core依赖与hadoop-common依赖互换,重新载入Maven配置后尝试运行Hadoop。

示例

附上我的pom.xml,在调整依赖顺序后能够成功运行。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.polarxiong</groupId>
    <artifactId>bigdata</artifactId>
    <version>1.0-SNAPSHOT</version>

    <repositories>
        <repository>
            <id>apache</id>
            <url>http://maven.apache.org</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-core</artifactId>
            <version>1.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>2.7.2</version>
        </dependency>
    </dependencies>
</project>

参考