HBase中报错 java.lang.NoClassDefFoundError: com/google/protobuf/LiteralByteString

Protobuf(全称 Protocol Buffers)是 Google 开发的一种数据描述语言,能够将结构化数据序列化,可用于数据存储、通信协议等方面。在 HBase 里面用使用了 Protobuf 的类库。
版本:
HBase:1.3.1
MySQL:8.0.13
错误报告
org.apache.hadoop.hbase.DoNotRetryIOException: java.lang.NoClassDefFoundError: com/google/protobuf/LiteralByteString
原因分析
运行 mvn dependency:tree 后发现 MySQL8.0.13 中有 com.google.protobuf:protobuf-java🏺3.6.1:compile。
查看了 3.6.x 的 protobuf 的源码,里面并没有 LiteralByteString 这个类。
https://github.com/protocolbuffers/protobuf/tree/3.6.x/java/core/src/main/java/com/google/protobuf
查看了 2.6.1 及以下版本的 protobuf 的源码,里面存在 LiteralByteString 这个类。
https://github.com/google/protobuf/blob/v2.5.0/java/src/main/java/com/google/protobuf/LiteralByteString.java
解决方法尝试
将 maven 中的 MySQL8.0.13 的引入注释掉或 MySQL 版本换成低版本,如 5.0.7,可以正常运行。
原因猜测
应用程序已经获得了另外一个版本的 Protobuf(如 3.6.1)并将其放在类路径中。建议在应用程序上运行 mvn dependency:tree 以查看它是否正在拾取不兼容的 Protobuf 依赖项(可能是传递性的)。如果应用程序的 Maven 依赖关系看起来很好,那么当你启动应用程序并拾取一个不正确的 Protobuf 版本时,可能是在运行时重载了类路径。
解决办法
1、降低 protobuf 版本(实操可用)

<dependency>
  <groupId>com.google.protobuf</groupId>
  <artifactId>protobuf-java</artifactId>
  <version>2.5.0</version>
</dependency>


2、将客户端的 Protobuf 类库重命名(尝试了下还是不行,应该是我本身项目哪还有问题)

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.4.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <relocations>
                    <relocation>
                        <pattern>com.google.protobuf</pattern>
                        <shadedPattern>com.fazi.google.protobuf</shadedPattern>
                    </relocation>
                </relocations>
            </configuration>
        </plugin>
    </plugins>
</build>


3、使用 hbase-shaded-client(实操可用)
hbase-shaded-client 是社区里有人将 HBase 里面比较常见的依赖进行了重命名,在 pom 文件中我们可以将引入的 hbase-client 替换成 hbase-shaded-client

<dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-shaded-client</artifactId>
            <version>1.3.1</version>
</dependency>


参考地址:
https://stackoverflow.com/questions/41734330/protobuf-error-hbase-createtable-put-in-java-codeprotobuf-literalbytestring
https://www.iteblog.com/archives/2463.html
---------------------
作者:发孖、
来源:CSDN
原文:https://blog.csdn.net/xcf111/article/details/86692591
版权声明:本文为博主原创文章,转载请附上博文链接!