Javier de la Rosa
Javier de la Rosa

Reputation: 237

Spark and Cassandra Guava conflict using Maven

I am trying to build a project using maven with Spark 1.6.2, Cassandra 3, and Cassandra connector 1.6.

The problem I found is Cassandra uses Guava +16v and Spark 14v so when I try to execute, the shell give me an error, I must use and Guava version +16

My dependencies in maven are:

        <!--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>

I tried adding Guava dependence but doesn't work.

Anyone knows how to fix it? Should I stop using maven and use sbt?

Thanks!!

Upvotes: 4

Views: 1580

Answers (2)

Javier de la Rosa
Javier de la Rosa

Reputation: 237

I got it finally. How leshkin said, it is necessary to shade the dependency.

This is the maven code I used to solved it!

      <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>

I hope it can help someone.

Thanks!!

Upvotes: 4

codejitsu
codejitsu

Reputation: 3182

I had the same problem recently but with SBT. I've found this post very helpful: https://hadoopist.wordpress.com/2016/05/22/how-to-connect-cassandra-and-spark/

So, I've added

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

to my SBT build file to fix the problem. In my case I created a fat jar (sbt assembly plugin) with all Spark and Cassandra dependencies within it.

I think you can use maven shade plugin to achieve the same result (if switching to SBT is an issue).

Upvotes: 1

Related Questions