Spark和Cassandra Guava使用Maven冲突

时间:2016-08-24 08:34:45

标签: maven apache-spark cassandra guava

我正在尝试使用带有Spark 1.6.2,Cassandra 3和Cassandra连接器1.6的maven构建一个项目。

我发现问题是Cassandra使用Guava + 16v和Spark 14v所以当我尝试执行时,shell会给我一个错误,我必须使用和Guava版本+16

我在maven中的依赖关系是:

        <!--Spark dependencies -->
    <dependency>
        <groupId>org.apache.spark</groupId>
        <artifactId>spark-core_2.10</artifactId>
        <version>1.6.2</version>
    </dependency>

    <!--Cassandra dependencies-->
    <dependency>
        <groupId>org.apache.cassandra</groupId>
        <artifactId>cassandra-all</artifactId>
        <version>3.0.2</version>
    </dependency>
    <dependency>
        <groupId>com.datastax.spark</groupId>
        <artifactId>spark-cassandra-connector_2.10</artifactId>
        <version>1.6.0</version>
    </dependency>

我尝试添加番石榴依赖但不起作用。

任何人都知道如何修复它?我应该停止使用maven并使用sbt吗?

谢谢!

2 个答案:

答案 0 :(得分:4)

我终于明白了。 leshkin如何说,有必要掩盖依赖性。

这是我用来解决它的maven代码!

      <pluginManagement>
        <plugins>
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>3.2.2</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugin</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
            </plugin>
        </plugins>
    </pluginManagement>
    <plugins>
        <plugin>
            <groupId>net.alchim31.maven</groupId>
            <artifactId>scala-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>scala-compile-first</id>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>add-source</goal>
                        <goal>compile</goal>
                    </goals>
                </execution>
                <execution>
                    <id>scala-test-compile</id>
                    <phase>process-test-resources</phase>
                    <goals>
                        <goal>testCompile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.4.3</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>
                            shade
                        </goal>
                    </goals>
                </execution>
            </executions>

            <configuration>
                <minimizeJar>true</minimizeJar>
                <shadedArtifactAttached>true</shadedArtifactAttached>
                <shadedClassifierName>fat</shadedClassifierName>

                <relocations>
                    <relocation>
                        <pattern>com.google</pattern>
                        <shadedPattern>shaded.guava</shadedPattern>
                        <includes>
                            <include>com.google.**</include>
                        </includes>

                        <excludes>
                            <exclude>com.google.common.base.Optional</exclude>
                            <exclude>com.google.common.base.Absent</exclude>
                            <exclude>com.google.common.base.Present</exclude>
                        </excludes>
                    </relocation>
                </relocations>

                <filters>
                    <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>META-INF/*.SF</exclude>
                            <exclude>META-INF/*.DSA</exclude>
                            <exclude>META-INF/*.RSA</exclude>
                        </excludes>
                    </filter>
                </filters>

            </configuration>
        </plugin>
        <!-- Plugin to create a single jar that includes all dependencies -->
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>

我希望它可以帮助别人。

谢谢!

答案 1 :(得分:1)

我最近遇到了同样的问题但是使用了SBT。我发现这篇文章非常有用:https://hadoopist.wordpress.com/2016/05/22/how-to-connect-cassandra-and-spark/

所以,我添加了

assemblyShadeRules in assembly := Seq(ShadeRule.rename("com.google.**" -> "shadeio.@1").inAll)

到我的SBT构建文件来解决问题。在我的例子中,我创建了一个胖jar(sbt程序集插件),其中包含所有Spark和Cassandra依赖项。

我认为您可以使用maven shade插件来实现相同的结果(如果切换到SBT是一个问题)。