使用CassandraSQLContext从Spark查询Cassandra

时间:2019-03-20 19:15:46

标签: scala apache-spark cassandra apache-spark-sql catalyst-optimizer

我尝试使用CassandraSQLContext从Spark查询Cassandra,但出现奇怪的缺少依赖项错误。我有一个类似以下的Spark应用程序:

val spark: SparkSession = SparkSession.builder().appName(appName).getOrCreate()
val cassandraSQLContext = new org.apache.spark.sql.cassandra.CassandraSQLContext(spark.sparkContext)

val path = args(0)
cassandraSQLContext.setKeyspace(args(1))

val dataFrame: DataFrame = cassandraSQLContext.sql(args(2))
dataFrame.write.mode(SaveMode.Overwrite).option("header", "true").csv(path)

我得到一个缺少的Spark Scala类错误:

User class threw exception: java.lang.NoClassDefFoundError: org/apache/spark/sql/catalyst/analysis/Catalog
    at com.test.batch.utils.CSVFromCassandraSQLQuery$.<init>(CSVFromCassandraSQLQuery.scala:19)
    at com.test.batch.utils.CSVFromCassandraSQLQuery$.<clinit>(CSVFromCassandraSQLQuery.scala)
    at com.test.batch.utils.CSVFromCassandraSQLQuery.main(CSVFromCassandraSQLQuery.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.spark.deploy.yarn.ApplicationMaster$$anon$4.run(ApplicationMaster.scala:721)
Caused by: java.lang.ClassNotFoundException: org.apache.spark.sql.catalyst.analysis.Catalog
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 8 more

我还尝试在spark-catalyst命令中显式添加spark-submit jar路径,但是仍然遇到相同的问题(无论是在本地还是在纱线簇上运行)...

这是我的项目设置build.sbt

scalaVersion := "2.11.11"
val sparkVersion = "2.3.1"

libraryDependencies ++= Seq(
  "log4j" % "log4j" % "1.2.17",
  "org.apache.spark" %% "spark-core" % sparkVersion % "provided",
  "org.apache.spark" %% "spark-sql" % sparkVersion  % "provided",
  "org.apache.spark" %% "spark-hive" % sparkVersion % "provided",
  "org.apache.spark" %% "spark-catalyst" % sparkVersion % "provided",
  "org.apache.spark" %% "spark-streaming" % sparkVersion % "provided",
  "com.datastax.spark" %% "spark-cassandra-connector" % "2.3.1",
  "org.scala-lang.modules" %% "scala-java8-compat" % "0.9.0",
  "com.twitter" % "jsr166e" % "1.1.0"
)

关于我想念的东西有什么想法吗?

1 个答案:

答案 0 :(得分:1)

发生此问题是因为org.apache.spark.sql.catalyst.analysis。目录类不再是 spark-catalyst_2.11:2.xx 的一部分。请参考 spark-catalyst_2.11:2.0.0

的源代码

https://jar-download.com/artifacts/org.apache.spark/spark-catalyst_2.11/2.0.0/source-code

org.apache.spark.sql.catalyst.analysis.Catalog 类在 spark-catalyst_2.11:1.6.3 版本之前可用。

https://jar-download.com/artifacts/org.apache.spark/spark-catalyst_2.11/1.6.3/source-code

我已要求您不要使用 CassandraSQLContext ,因为它已被弃用。请检查https://datastax-oss.atlassian.net/browse/SPARKC-399

请选中SO帖子以在Spark 2.0中使用 Cassandra Context

how to use Cassandra Context in spark 2.0