Hbase与Elasticsearch的jar包冲突解决办法

问题

当项目中同时集成Hbase和Elasticsearch时,经常遇到依赖包冲突的问题,如com.google.guava,org.joda等。造成guava冲突是因为集成Hbase和ES时都用到了guava包,但是两者要求的版本不一样,ES 2.0 版本以上的要求guava(19.0+),Hbase 1.0要求的guava为16.0,如果把guava统一为16.0,则ES会因为guava的版本太低而报错。joda也如此。所以,要同时使用Hbase和ES,就得解决这个冲突。

解决办法

1、首先,新建maven项目,对pom.xml进行如下配置:


<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.0modelVersion>
    <groupId>com.esgroupId>
    <artifactId>esshadeartifactId>
    <version>1.0-SNAPSHOTversion>
    <packaging>jarpackaging>
    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <maven.compiler.source>1.8maven.compiler.source>
        <maven.compiler.target>1.8maven.compiler.target>
    properties>

    <dependencies>   
        
        <dependency>
            <groupId>org.elasticsearchgroupId>
            <artifactId>elasticsearchartifactId>
            <version>2.4.1version>

        dependency>
        <dependency>
            <groupId>org.elasticsearch.plugingroupId>
            <artifactId>delete-by-queryartifactId>
            <version>2.4.1version>
        dependency>

        
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-shade-pluginartifactId>
                <version>2.4.1version>
                <configuration>
                    <createDependencyReducedPom>falsecreateDependencyReducedPom>
                configuration>
                <executions>
                    <execution>
                        <phase>packagephase>
                        <goals>
                            <goal>shadegoal>
                        goals>
                        <configuration>
                            <relocations>
                                <relocation>
                                    <pattern>com.google.guavapattern>
                                    <shadedPattern>my.elasticsearch.guavashadedPattern>
                                relocation>
                                <relocation>
                                    <pattern>org.jodapattern>
                                    <shadedPattern>my.elasticsearch.jodashadedPattern>
                                relocation>
                                <relocation>
                                    <pattern>com.google.commonpattern>
                                    <shadedPattern>my.elasticsearch.commonshadedPattern>
                                relocation>
                                <relocation>
                                    <pattern>com.google.thirdpartypattern>
                                    <shadedPattern>my.elasticsearch.thirdpartyshadedPattern>
                                relocation>
                            relocations>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer" />
                            transformers>
                        configuration>
                    execution>
                executions>
            plugin>
        plugins>
    build>

    <repositories>
        <repository>
            <id>elasticsearch-releasesid>
            <url>http://maven.elasticsearch.org/releasesurl>
            <releases>
                <enabled>trueenabled>
                <updatePolicy>dailyupdatePolicy>
            releases>
            <snapshots>
                <enabled>falseenabled>
            snapshots>
        repository>
    repositories>
project>

pom.xml文件配置好后,构建,得到jar包。如下图,依赖关系加入.m2文件夹里。

如上配置完成后,其实就是将com.google.guava等4个可能有冲突的jar包通过maven-shade-plugin插件迁移后,重新打个jar包从而使得在引入这个jar包时能够使用该jar包自己的依赖而不是使用外部依赖。

2、Hbase与ES的融合
在同时集成Hbase与ES的项目中,加入Hbase的依赖包,并加入步骤1中构建的依赖包:

<dependency>
            <groupId>org.apache.hadoopgroupId>
            <artifactId>hadoop-hdfsartifactId>
            <version>2.6.4version>
        dependency>

        <dependency>
            <groupId>org.apache.hadoopgroupId>
            <artifactId>hadoop-clientartifactId>
            <version>2.6.4version>
        dependency>

        <dependency>
            <groupId>org.apache.hbasegroupId>
            <artifactId>hbase-clientartifactId>
            <version>1.2.1version>
        dependency>

        <dependency>
            <groupId>org.apache.hbasegroupId>
            <artifactId>hbaseartifactId>
            <version>1.2.1version>
            <type>pomtype>
        dependency>

        <dependency>
            <groupId>com.google.guavagroupId>
            <artifactId>guavaartifactId>
            <version>16.0.1version>
        dependency>

        <dependency>
            <groupId>com.esgroupId>
            <artifactId>esshadeartifactId>
            <version>1.0-SNAPSHOTversion>
            <exclusions>
                <exclusion>
                    <groupId>org.elasticsearchgroupId>
                    <artifactId>elasticsearchartifactId>
                exclusion>
            exclusions>
        dependency>

配置好了,重新清理构建,测试一下,Hbase和ES就可以同时使用了。
可参考此网站

你可能感兴趣的:(环境搭建)