gpt4 book ai didi

java - Spark-Cassandra Maven 项目,带有进行 scala-lib 调用的 java 源代码

转载 作者:行者123 更新时间:2023-11-30 03:26:29 26 4
gpt4 key购买 nike

我对 Spark 和 Cassandra 都很陌生,需要一些指导。我正在设置一个使用 Spark v1.3.1 和 Cassandra v2.0.14 的 Maven 项目。我正在尝试以下操作:

1) 使用以下方式与Oracle DB建立连接,进行数据输入;利用 Spark 1.3.0 中新增的 DataFrame:http://www.sparkexpert.com/2015/03/28/loading-database-data-into-spark-using-data-sources-api/

2)使用spark-cassandra-connector来进行后者之间的连接;在 github 上找到的。

3)一旦我在 DataFrame 中拥有数据库数据,我应该能够转换为 JavaRDD 类型并推送到 Cassandra 键空间,如下所示:http://www.datastax.com/dev/blog/accessing-cassandra-from-spark-in-java

4) 简而言之:[Oracle DB]<---[Spark]---[spark-cassandra-connector]--->[Cassandra]

我遇到的问题是在我的 Java 代码中调用 Scala-lib 期间(上面的步骤 1);更具体地说,在加载函数调用期间: DataFrame jdbcDF = sqlContext.load(“jdbc”, options);

运行时错误:java.lang.ClassNotFoundException:scala.collection.GenTraversableOnce$class”

尽管在我的 pom.xml 文件中尝试了推荐的 2.10.X Scala 的几个不同版本,但还是出现了上述错误。根据我之前的研究,我认为这可能是 Spark-Scala 兼容性问题。我还读到我需要将 scala-lib.jar 包含在我的类路径中,但我不确定如何使用 Maven 执行此操作。对此有什么想法吗?我在下面包含了 pom.xml 和 java 代码:

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.dev</groupId>
<artifactId>spark-cassandra</artifactId>
<version>0.0.1-SPARK-CASSANDRA</version>

<dependencies>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.11</artifactId>
<version>1.3.1</version>
</dependency>

<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.11</artifactId>
<version>1.3.1</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0</version>
</dependency>


<dependency>
<groupId>com.datastax.spark</groupId>
<artifactId>spark-cassandra-connector_2.10</artifactId>
<version>1.0.0-rc4</version>
</dependency>
<dependency>
<groupId>com.datastax.spark</groupId>
<artifactId>spark-cassandra-connector-java_2.10</artifactId>
<version>1.0.0-rc4</version>
</dependency>
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-core</artifactId>
<version>2.1.5</version>
</dependency>

<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming_2.10</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>com.dev.cassandra</groupId>
<artifactId>spark-cassandra</artifactId>
<version>1.0</version>
</dependency>

<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.10.3</version>
</dependency>

<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-compiler</artifactId>
<version>2.10.3</version>
</dependency>
<!--
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-reflect</artifactId>
<version>2.10.0-M1</version>
</dependency>
-->


<!--

<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-swing</artifactId>
<version>2.10.0-M1</version>
</dependency>
-->

</dependencies>


<build>
<pluginManagement>

<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.1.5</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<mainClass>com.dev.cassandra.Main</mainClass>
<cleanupDaemonThreads>false</cleanupDaemonThreads>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</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 to create a single jar that includes all dependencies
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.dev.cassandra.Main</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
-->
</plugins>
</build>

</project>

JAVA代码:

package com.dev.cassandra;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import java.sql.*;

import org.apache.spark.*;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.*;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.sql.DataFrame;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SQLContext;
import org.apache.spark.sql.types.DataTypes;
import org.apache.spark.sql.types.StructField;
import org.apache.spark.sql.types.StructType;

import oracle.jdbc.*;

import com.datastax.spark.connector.cql.CassandraConnector;
import static com.datastax.spark.connector.CassandraJavaUtil.*;

public class Main implements Serializable {

private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger.getLogger(Main.class);

private static final String JDBC_DRIVER = "oracle.jdbc.driver.OracleDriver";
private static final String JDBC_USERNAME = "XXXXXO01";
private static final String JDBC_PWD = "XXXXXO01";
private static final String JDBC_CONNECTION_URL =
"jdbc:oracle:thin:" + JDBC_USERNAME + "/" + JDBC_PWD + "@CONNECTION VALUES";

private transient SparkConf conf;

private Main(SparkConf conf) {
this.conf = conf;
}

private void run() {
JavaSparkContext sc = new JavaSparkContext(conf);
SQLContext sqlContext = new SQLContext(sc);
generateData(sc);
compute(sc);
showResults(sc);
sc.stop();
}

private void generateData(JavaSparkContext sc) {

SQLContext sqlContext = new org.apache.spark.sql.SQLContext(sc);
System.out.println("AFTER SQL CONTEXT");

//Data source options
Map<String, String> options = new HashMap<>();
options.put("driver", JDBC_DRIVER);
options.put("url", JDBC_CONNECTION_URL);
options.put("dbtable","(SELECT * FROM XXX_SAMPLE_TABLE WHERE ROWNUM <=5)");

CassandraConnector connector = CassandraConnector.apply(sc.getConf());

try{
Class.forName(JDBC_DRIVER);

System.out.println("BEFORE jdbcDF");

//Load JDBC query result as DataFrame
DataFrame jdbcDF = sqlContext.load("jdbc", options);
System.out.println("AFTER jdbcDF");

List<Row> tableRows = jdbcDF.collectAsList();

System.out.println("AFTER tableRows");

for (Row tableRow : tableRows) {
System.out.println();
LOGGER.info(tableRow);
System.out.println();
}

}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}
}

private void compute(JavaSparkContext sc) {
}

private void showResults(JavaSparkContext sc) {
}


public static void main(String[] args) throws InterruptedException
{

if (args.length != 2) {
System.err.println("Syntax: com.datastax.spark.dev.cassandra <Spark Master URL> <Cassandra contact point>");
System.exit(1);
}

//JavaSparkContext sc = new JavaSparkContext(new SparkConf().setAppName("SparkJdbcDs").setMaster("local[*]"));
SparkConf conf = new SparkConf().setAppName("SparkJdbcDs").setMaster("local[*]");


//SparkConf conf = new SparkConf();
//conf.setAppName("SparkJdbcDs");
//conf.setMaster(args[0]);
//conf.set("spark.cassandra.connection.host", args[1]);

Main app = new Main(conf);
app.run();

}
}

提前致谢!

最佳答案

pom.xml 正在请求某些 Spark JAR 的 Scala 2.11 版本

<artifactId>spark-core_2.11</artifactId>

<artifactId>spark-sql_2.11</artifactId>

以及另一个 Spark JAR 和 Cassandra 连接器 JAR 的 Scala 2.10 版本。

<artifactId>spark-streaming_2.10</artifactId>

<artifactId>spark-cassandra-connector_2.10</artifactId>

<artifactId>spark-cassandra-connector-java_2.10</artifactId>

(基于 Scala Artifact ID 的命名约定,以您希望为其构建的 Scala 版本结尾。)

这些需要(a)彼此一致以及(b)与您实际使用的 Scala 版本一致。

关于java - Spark-Cassandra Maven 项目,带有进行 scala-lib 调用的 java 源代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30084047/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com