您好,登錄后才能下訂單哦!
這篇文章主要介紹了如何解決Hbase、elasticsearch整合中jar包沖突的問題,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
問題背景
再數據平臺中,項目搭建需要使用es和HBASE搭建數據查詢接口,整合的過程中出現jar包沖突的bug :com.google.common.base.Stopwatch.()V from class org.apache.hadoop.hbase.zookeeper.MetaTableLocator
org.apache.hadoop.hbase.DoNotRetryIOException: java.lang.IllegalAccessError: tried to access method com.google.common.base.Stopwatch.<init>()V from class org.apache.hadoop.hbase.zookeeper.MetaTableLocator at org.apache.hadoop.hbase.client.RpcRetryingCaller.translateException(RpcRetryingCaller.java:239) at org.apache.hadoop.hbase.client.RpcRetryingCaller.callWithRetries(RpcRetryingCaller.java:150) ... at java.lang.Thread.run(Thread.java:745) Caused by: java.lang.IllegalAccessError: tried to access method com.google.common.base.Stopwatch.<init>()V from class org.apache.hadoop.hbase.zookeeper.MetaTableLocator at org.apache.hadoop.hbase.zookeeper.MetaTableLocator.blockUntil ... at org.apache.hadoop.hbase.client.RegionServerCallable.prepare(RegionServerCallable.java:75) at org.apache.hadoop.hbase.client.RpcRetryingCaller.callWithRetries(RpcRetryingCaller.java:134) ... 45 more
解決辦法
經排查,確認是因為 com.google.guava 這個包引起的沖突。es依賴于18及以上版本,而HBASE只支持16及以上版本。而guava這個包從17開始內部發生變化,方法發生改變,所以18并不會向下兼容16版本。在項目運行的過程中,如果同時引入16、18版本,es及hbase的調用過程會發生混亂。那么接下來就好辦了,我們可以重新打包,將guava18打入es內部,再在pom文件中顯示引用guava16版本的包。這樣,es調用包內部打入的guava18,而hbase調用外部的guava16。
重新打包
新建maven工程,在pom文件中進行如下配置:
<?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> <!-- this maven project is aim to fix the jar comflic in hbase & elasticsearch. in es, required guava 18+ or up. but in hbase you should use guava 16- or blow. cd the project directory and run 'sh ./cleanbuild.sh',you will get a new-self jar. then, adjust your project pom.xml whth: <dependency> <groupId>douguo.shaded.elasticsearch</groupId> <artifactId>douguo_shaded_elasticsearch</artifactId> <version>1.0-SNAPSHOT</version> <exclusions> <exclusion> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> </exclusion> </exclusions> </dependency> finally, your jar comflic will be fixed. @date:2017-11-30 @author:zhangjianfei @since:douguo.shaded.elasticsearch-1.0.0 --> <groupId>douguo.shaded.elasticsearch</groupId> <artifactId>douguo_shaded_elasticsearch</artifactId> <version>1.0-SNAPSHOT</version> <properties> <elasticsearch.version>2.4.1</elasticsearch.version> </properties> <dependencies> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>${elasticsearch.version}</version> </dependency> <dependency> <groupId>org.elasticsearch.plugin</groupId> <artifactId>shield</artifactId> <version>${elasticsearch.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.4.1</version> <configuration> <createDependencyReducedPom>false</createDependencyReducedPom> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <relocations> <relocation> <pattern>com.google.guava</pattern> <shadedPattern>douguo.shaded.elasticsearch.guava</shadedPattern> </relocation> <relocation> <pattern>org.joda</pattern> <shadedPattern>douguo.shaded.elasticsearch.joda</shadedPattern> </relocation> <relocation> <pattern>com.google.common</pattern> <shadedPattern>douguo.shaded.elasticsearch.common</shadedPattern> </relocation> <relocation> <pattern>com.google.thirdparty</pattern> <shadedPattern>douguo.shaded.elasticsearch.thirdparty</shadedPattern> </relocation> </relocations> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer" /> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build> <repositories> <repository> <id>elasticsearch-releases</id> <url>http://maven.elasticsearch.org/releases</url> <releases> <enabled>true</enabled> <updatePolicy>daily</updatePolicy> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
將org.joda等4個可能有沖突的jar包通過maven-shade-plugin插件遷移后重新打個jar包從而使得在引入這個jar包時能夠使用該jar包自己的依賴而不是使用外部依賴。這里需要注意的是,需要將com.google.common等4個包全部重新遷移,否則會出現java.lang.IllegalAccessError: tried to access method com.google.common.base的錯誤
項目打包
mvn clean install
新的依賴包會在.m2 maven倉庫中,如果公司搭建了倉庫的話,需要上傳jar包。如果直接運行jar包的話,記得重新編譯項目,并替換lib目錄
項目載入新包
只需要在pom文件中配置:
<!-- douguo.shaded.elasticsearch --> <dependency> <groupId>douguo.shaded.elasticsearch</groupId> <artifactId>douguo_shaded_elasticsearch</artifactId> <version>1.0-SNAPSHOT</version> <exclusions> <exclusion> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> </exclusion> </exclusions> </dependency> <!-- https://mvnrepository.com/artifact/com.google.guava/guava --> <!-- this guava is only used in habse in es, 18.0+ is required, but hbase only supported 16.0 or blow. clean as install douguo.shaded.elasticsearch --> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>16.0</version> </dependency>
這樣,guava18包就在douguo.shaded.elasticsearch下,es會優先調用。而外部配置的guava16就會被HBASE調用。2個版本的jar包互相獨立存在!
感謝你能夠認真閱讀完這篇文章,希望小編分享的“如何解決Hbase、elasticsearch整合中jar包沖突的問題”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。