我正在使用
Spark 2.2.1版
使用Scala 2.11.8版
OpenJDK 64位服务器虚拟机,1.8.0_131
我使用代码添加了jar依赖项
JavaSparkContext sc = new JavaSparkContext(conf);
sc.addJar("./target/CassandraSparkJava-1.0-SNAPSHOT-jar-with-dependencies.jar");
正在执行下面的代码,但遇到ClassNotFoundException异常:
Dataset<org.apache.spark.sql.Row> dataset = sparksession.read().format("org.apache.spark.sql.cassandra")
.options(new HashMap<String, String>() {
{
put("keyspace", "test_db");
put("table", "stats");
}
}).load().where("timestamp = '2018-01-09 00:00:00'");
System.out.println(" Result " + dataset.collectAsList().size());
下面是我的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>com.test.spark.poc</groupId>
<artifactId>CassandraSparkJava</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.felix/maven-bundle-plugin -->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.11</artifactId>
<version>2.2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.11</artifactId>
<version>2.2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.datastax.spark</groupId>
<artifactId>spark-cassandra-connector_2.11</artifactId>
<version>2.0.0-M3</version>
</dependency>
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-core</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
<dependency>
<groupId>io.reactivex</groupId>
<artifactId>rxjava</artifactId>
<version>1.0.0-rc.8</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- A plugin for compiling Java code -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<archive>
<manifest>
<mainClass>com.test.spark.poc.CassandraSparkMockStats</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<!-- Or, a plugin for compiling Scala code -->
<!-- Make sure you are not using "maven-scala-plugin", which is the older version -->
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.2</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<!-- get all project dependencies -->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- MainClass in mainfest make a executable jar -->
<archive>
<manifest>
<mainClass>com.test.spark.poc.CassandraSparkMockStats</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- bind to the packaging phase -->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
帮助我解决此问题
2条答案
按热度按时间p8h8hvxi1#
我有这个异常时,试图运行Spark与cassandra从PC上的java(驱动程序)。
您必须将带有spark-cassandra-connector的jar添加到SparkContext中,在我的示例中,如下所示:
mrzz3bfm2#
对于3.0+ spark/spark cassandra连接器,请尝试the official assembly jar:
spark-cassandra-connector-assembly_2.12-3.1.0.jar
。它具有所有依赖项。