在 Apache Spark 和 Cassandra 之间创建会话时出现 NullpointerException

NullpointerException when create session between Apache Spark and Cassandra

我正在使用 Apache Spark 2.1.0 和 Cassandra 3.0.14。在我的代码中,我想在 Spark 和 Cassandra 之间创建一个连接:

            ...
 SparkSession sparkSession = SparkSession.builder()
     .appName(appName)
     .config("spark.cassandra.connection.host", "localhost")                            
     .config("spark.cassandra.connection.port", 9042)
     .getOrCreate();

CassandraConnector cassandraConnector = CassandraConnector
     .apply(sparkSession.sparkContext().getConf()); 
Session session = cassandraConnector.openSession();
ResultSet rs = session.execute("select * from myDB.myTable");
            ...

当我 运行 在 eclipse 中本地代码时,一切正常,但是当我 运行 我本地 spark 服务器上的 jar 文件时,我得到

Exception in thread "main" java.lang.NullPointerException

导致这个错误的方法是

cassandraConnector.openSession();

这是我的 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.0</modelVersion>
    <groupId>xign_analysis</groupId>
    <artifactId>xign_analysis_jar_archive</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <build>
    </build>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-core_2.10 -->
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_2.10</artifactId>
            <version>2.1.1</version>
            <scope>compile</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-sql_2.10 -->
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-sql_2.10</artifactId>
            <version>2.1.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-mllib_2.10</artifactId>
            <version>2.1.1</version>
            <scope>compile</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.cassandra/cassandra-all -->
        <dependency>
            <groupId>org.apache.cassandra</groupId>
            <artifactId>cassandra-all</artifactId>
            <version>3.11.0</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>log4j-over-slf4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.datastax.spark/spark-cassandra-connector_2.10 -->
        <dependency>
            <groupId>com.datastax.spark</groupId>
            <artifactId>spark-cassandra-connector_2.10</artifactId>
            <version>2.0.5</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>
</project>

我将 Macbook 与 El Capitan (10.11.06) 一起使用。我的 Spark Master、Spark Worker 和 Cassandra 服务器 运行 正常。我不知道如何解决这个问题。

我找到了解决方案。在 spark/jars 目录中有一个旧版本的 Guava:Google Java 的核心库。我用最新版本 (v. 23.0) 替换了旧版本 (v. 14.0.1),一切正常。