Spark和Cassandra Java应用程序:线程中的异常" main" java.lang.NoClassDefFoundError:org / apache / spark / sql / Dataset

时间:2016-10-06 04:05:02

标签: java apache-spark cassandra datastax

我得到了一个令人惊讶的siplme java应用程序,我几乎从这个例子中复制过:http://markmail.org/download.xqy?id=zua6upabiylzeetp&number=2

我想要做的就是读取表格数据并在Eclipse控制台中显示。

我的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>chat_connaction_test</groupId>
  <artifactId>ChatSparkConnectionTest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
 <dependencies> 
    <dependency>
    <groupId>com.datastax.cassandra</groupId>
    <artifactId>cassandra-driver-core</artifactId>
    <version>3.1.0</version>
    </dependency>

    <dependency>
    <groupId>org.apache.spark</groupId>
    <artifactId>spark-core_2.10</artifactId>
    <version>2.0.0</version>
    </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.0-M3</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-streaming_2.10 -->
    <dependency>
    <groupId>org.apache.spark</groupId>
    <artifactId>spark-streaming_2.10</artifactId>
    <version>2.0.0</version>
    </dependency>
    <!--
    <dependency> 
    <groupId>org.apache.spark</groupId> 
    <artifactId>spark-hive_2.10</artifactId> 
    <version>1.5.2</version> 
    </dependency>
    -->
  </dependencies>
</project>

我的java代码:

    package com.chatSparkConnactionTest;

import static com.datastax.spark.connector.japi.CassandraJavaUtil.javaFunctions;
import java.io.Serializable;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.Function;
import com.datastax.spark.connector.japi.CassandraRow;

public class JavaDemo implements Serializable {
    private static final long serialVersionUID = 1L;
    public static void main(String[] args) {

        SparkConf conf = new SparkConf().
            setAppName("chat").
            setMaster("local").
            set("spark.executor.memory","1g").
            set("spark.cassandra.connection.host", "127.0.0.1");
        JavaSparkContext sc = new JavaSparkContext(conf);

        JavaRDD<String> cassandraRowsRDD = javaFunctions(sc).cassandraTable(
            "chat", "dictionary")

            .map(new Function<CassandraRow, String>() {
                @Override
                public String call(CassandraRow cassandraRow) throws Exception {
                    String tempResult = cassandraRow.toString();
                    System.out.println(tempResult);
                    return tempResult;
                    }
                }
            );
        System.out.println("Data as CassandraRows: \n" + 
        cassandraRowsRDD.collect().size()); // THIS IS A LINE WITH ERROR
    } 
}

这是我的错误:

  

16/10/05 20:49:18 INFO CassandraConnector:连接到Cassandra   cluster:在线程&#34; main&#34;中测试群集异常   java.lang.NoClassDefFoundError:org / apache / spark / sql / Dataset at   java.lang.Class.getDeclaredMethods0(Native Method)at   java.lang.Class.privateGetDeclaredMethods(Unknown Source)at   java.lang.Class.getDeclaredMethod(未知来源)at   java.io.Object.StreamClass.getPrivateMethod(未知来源)at   java.io.ObjectStreamClass.access $ 1700(未知来源)at   java.io.ObjectStreamClass $ 2.run(未知来源)at   java.io.ObjectStreamClass $ 2.run(未知来源)at   java.security.AccessController.doPrivileged(Native Method)at   java.io.ObjectStreamClass。(未知来源)at   java.io.ObjectStreamClass.lookup(未知来源)at   java.io.ObjectOutputStream.writeObject0(未知来源)at   java.io.ObjectOutputStream.defaultWriteFields(未知来源)at   java.io.ObjectOutputStream.writeSerialData(未知来源)at   java.io.ObjectOutputStream.writeOrdinaryObject(未知来源)at   java.io.ObjectOutputStream.writeObject0(未知来源)at   java.io.ObjectOutputStream.defaultWriteFields(未知来源)at   java.io.ObjectOutputStream.writeSerialData(未知来源)at   java.io.ObjectOutputStream.writeOrdinaryObject(未知来源)at   java.io.ObjectOutputStream.writeObject0(未知来源)at   java.io.ObjectOutputStream.writeObject(未知来源)at   。scala.collection.immutable $结肠$ colon.writeObject(List.scala:379)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)at   sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)at   java.lang.reflect.Method.invoke(未知来源)at   java.io.Object.StreamClass.invokeWriteObject(未知来源)at   java.io.ObjectOutputStream.writeSerialData(未知来源)at   java.io.ObjectOutputStream.writeOrdinaryObject(未知来源)at   java.io.ObjectOutputStream.writeObject0(未知来源)at   java.io.ObjectOutputStream.defaultWriteFields(未知来源)at   java.io.ObjectOutputStream.writeSerialData(未知来源)at   java.io.ObjectOutputStream.writeOrdinaryObject(未知来源)at   java.io.ObjectOutputStream.writeObject0(未知来源)at   java.io.ObjectOutputStream.defaultWriteFields(未知来源)at   java.io.ObjectOutputStream.writeSerialData(未知来源)at   java.io.ObjectOutputStream.writeOrdinaryObject(未知来源)at   java.io.ObjectOutputStream.writeObject0(未知来源)at   java.io.ObjectOutputStream.defaultWriteFields(未知来源)at   java.io.ObjectOutputStream.writeSerialData(未知来源)at   java.io.ObjectOutputStream.writeOrdinaryObject(未知来源)at   java.io.ObjectOutputStream.writeObject0(未知来源)at   java.io.ObjectOutputStream.writeObject(未知来源)at   org.apache.spark.serializer.JavaSerializationStream.writeObject(JavaSerializer.scala:43)     在   org.apache.spark.serializer.JavaSerializerInstance.serialize(JavaSerializer.scala:100)     在   org.apache.spark.util.ClosureCleaner $ .ensureSerializable(ClosureCleaner.scala:295)     在   org.apache.spark.util.ClosureCleaner $ .ORG $阿帕奇$火花$ UTIL $ ClosureCleaner $$干净(ClosureCleaner.scala:288)     在   org.apache.spark.util.ClosureCleaner $清洁机壳(ClosureCleaner.scala:108)     在org.apache.spark.SparkContext.clean(SparkContext.scala:2037)at   org.apache.spark.SparkContext.runJob(SparkContext.scala:1896)at at   org.apache.spark.SparkContext.runJob(SparkContext.scala:1911)at at   org.apache.spark.rdd.RDD $$ anonfun $ collect $ 1.apply(RDD.scala:893)at at   org.apache.spark.rdd.RDDOperationScope $ .withScope(RDDOperationScope.scala:151)     在   org.apache.spark.rdd.RDDOperationScope $ .withScope(RDDOperationScope.scala:112)     在org.apache.spark.rdd.RDD.withScope(RDD.scala:358)at   org.apache.spark.rdd.RDD.collect(RDD.scala:892)at   org.apache.spark.api.java.JavaRDDLike $ class.collect(JavaRDDLike.scala:360)     在   org.apache.spark.api.java.AbstractJavaRDDLike.collect(JavaRDDLike.scala:45)     在com.chatSparkConnactionTest.JavaDemo.main(JavaDemo.java:37)引起   by:java.lang.ClassNotFoundException:org.apache.spark.sql.Dataset at at   java.net.URLClassLoader.findClass(未知来源)at   java.lang.ClassLoader.loadClass(未知来源)at   sun.misc.Launcher $ AppClassLoader.loadClass(未知来源)at   java.lang.ClassLoader.loadClass(未知来源)... 58更多

我更新了pom.xml,但没有解决错误。有人可以帮我解决这个问题吗?

谢谢!

更新1: 这是我的构建路径截图: Link to my screenshot

3 个答案:

答案 0 :(得分:4)

您收到“java.lang.NoClassDefFoundError:org / apache / spark / sql / Dataset”错误,因为您的pom.xml文件中缺少“spark-sql”依赖项。

如果您想使用Spark 2.0.0阅读Cassandra表,那么您需要低于最低依赖性。

<dependency>
    <groupId>org.apache.spark</groupId>
    <artifactId>spark-core_2.11</artifactId>
    <version>2.0.0</version>
</dependency>
<dependency>
    <groupId>org.apache.spark</groupId>
    <artifactId>spark-sql_2.11</artifactId>
    <version>2.0.0</version>
</dependency>
<dependency>
    <groupId>com.datastax.spark</groupId>
    <artifactId>spark-cassandra-connector_2.11</artifactId>
    <version>2.0.0-M3</version>
</dependency>

Spark 2.0.0提供SparkSession和Dataset API。下面是用于读取Cassandra表并打印记录的示例程序。

 public class SparkCassandraDatasetApplication {
 public static void main(String[] args) {
 SparkSession spark = SparkSession
          .builder()
          .appName("SparkCassandraDatasetApplication")
          .config("spark.sql.warehouse.dir", "/file:C:/temp")
          .config("spark.cassandra.connection.host", "127.0.0.1")
          .config("spark.cassandra.connection.port", "9042")
          .master("local[2]")
          .getOrCreate();

 //Read data
 Dataset<Row> dataset = spark.read().format("org.apache.spark.sql.cassandra")
        .options(new HashMap<String, String>() {
            {
                put("keyspace", "mykeyspace");
                put("table", "mytable");
            }
        }).load();

   //Print data
   dataset.show();       
   spark.stop();
   }        
}

如果您仍想使用RDD,请使用以下示例程序。

public class SparkCassandraRDDApplication {
public static void main(String[] args) {
    SparkConf conf = new SparkConf()
            .setAppName("SparkCassandraRDDApplication")
            .setMaster("local[2]")
            .set("spark.cassandra.connection.host", "127.0.0.1")
            .set("spark.cassandra.connection.port", "9042");

    JavaSparkContext sc = new JavaSparkContext(conf);

    //Read
    JavaRDD<UserData> resultsRDD = javaFunctions(sc).cassandraTable("mykeyspace", "mytable",CassandraJavaUtil.mapRowTo(UserData.class));

    //Print
    resultsRDD.foreach(data -> {
        System.out.println(data.id);
        System.out.println(data.username);
    });

    sc.stop();
  }
}

上面程序中使用的Javabean(UserData)如下所示。

public class UserData implements Serializable{  
  String id;
  String username;     
  public String getId() {
      return id;
  }
  public void setId(String id) {
      this.id = id;
  }
  public String getUsername() {
     return username;
  }
  public void setUsername(String username) {
     this.username = username;
   }    
}

答案 1 :(得分:0)

我认为您需要确保您的类路径中存在以下资源:

cassandra-driver-core-2.1.0.jar
metrics-core-3.0.2.jar
slf4j-api-1.7.5.jar
netty-3.9.0-Final.jar
guava-16.0.1.jar

希望这会对你有所帮助

答案 2 :(得分:0)

删除

<!-- https://mvnrepository.com/artifact/com.datastax.spark/spark-cassandra-connector-java_2.10 -->
<dependency>
<groupId>com.datastax.spark</groupId>
<artifactId>spark-cassandra-connector-java_2.10</artifactId>
<version>1.6.0-M1</version>
</dependency>

您正在类路径上混合版本。 java模块包含在Spark Cassandra Connector 2.0.0的核心模块中。所以这只是引入了火花1.6引用。