I try to query Cassandra from Spark using CassandraSQLContext, but I get an weird missing dependency error. I have a Spark application like the following :
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)
And I get a missing Spark scala class error :
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
I also tried to add the spark-catalyst
jar path explicitly in the spark-submit
command, but I still get the same issue (whether I run it locally or on a yarn cluster) ...
Here is my project settings 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"
)
Any ideas about what I'm missing ?
This issue is occuring because org.apache.spark.sql.catalyst.analysis.Catalog class is not part of spark-catalyst_2.11:2.x.x anymore. Please refer source code of 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 class is available till spark-catalyst_2.11:1.6.3 version.
https://jar-download.com/artifacts/org.apache.spark/spark-catalyst_2.11/1.6.3/source-code
I would request you to not use CassandraSQLContext as it has been deprecated. Please check https://datastax-oss.atlassian.net/browse/SPARKC-399.
Please check SO post to use Cassandra Context in spark 2.0.
how to use Cassandra Context in spark 2.0