0
votes

Getiing following error while trying to read data from Kafka. I am using docker-compose for running kafka and spark.

Exception in thread "main" org.apache.spark.sql.AnalysisException: Failed to find data source: kafka. Please deploy the application as per the deployment section of "Structured Streaming + Kafka Integration Guide".

Here is my code for reading:

object Livedata extends App with LazyLogging {
  logger.info("starting livedata...")
  val spark = SparkSession.builder().appName("livedata").master("local[*]").getOrCreate()

  val df = spark.readStream
        .format("kafka")
        .option("kafka.bootstrap.servers", "kafka:9092")
        .option("subscribe", "topic")
        .option("startingOffsets", "latest")
        .load()

  df.printSchema()

  val hadoopConfig = spark.sparkContext.hadoopConfiguration
  hadoopConfig.set("fs.hdfs.impl", classOf[org.apache.hadoop.hdfs.DistributedFileSystem].getName)
  hadoopConfig.set("fs.file.impl", classOf[org.apache.hadoop.fs.LocalFileSystem].getName)

}

After reading few answers I have added all packages for sbt build

Here is build.sbt file:

lazy val root = (project in file(".")).
  settings(
    inThisBuild(List(
      organization := "com.live.data",
      version := "0.1.0",
      scalaVersion := "2.12.2",
      assemblyJarName in assembly := "livedata.jar"
)),
    name := "livedata",
    libraryDependencies ++= List(
      "org.scalatest" %% "scalatest" % "3.0.5",
      "com.typesafe.scala-logging" %% "scala-logging" % "3.9.0",
      "org.apache.spark" %% "spark-sql" % "2.4.0",
      "org.apache.spark" %% "spark-sql-kafka-0-10" % "2.4.0" % "provided",
      "org.apache.kafka"           % "kafka-clients"            % "2.5.0",
      "org.apache.kafka"           % "kafka-streams"            % "2.5.0",
      "org.apache.kafka"           %% "kafka-streams-scala"     % "2.5.0"
)
)
assemblyMergeStrategy in assembly := {
  case PathList("META-INF", xs@_*) => MergeStrategy.discard
  case x => MergeStrategy.first
}

Not sure what is the main issue here.

Update:

Finally I got the solution from here Error when connecting spark structured streaming + kafka

Main issue was getting this org.apache.spark.sql.AnalysisException: Failed to find data source: kafka exception because spark-sql-kafka library is not available in classpath & It is unable to find org.apache.spark.sql.sources.DataSourceRegister inside META-INF/services folder.

Following codeblock need to add in build.sbt. This will include org.apache.spark.sql.sources.DataSourceRegister file in the final jar.

// META-INF discarding
assemblyMergeStrategy in assembly := {
  case PathList("META-INF","services",xs @ _*) => MergeStrategy.filterDistinctLines
  case PathList("META-INF",xs @ _*) => MergeStrategy.discard
  case "application.conf" => MergeStrategy.concat
  case _ => MergeStrategy.first
}```

1
Can you add your docker-compose.yml and the error you are getting while upping the container - khari-sing

1 Answers

0
votes

spark-sql-kafka-0-10 is not provided, so remove that part of the dependencies. (spark-sql is provided, though, so you could add it to that one)

You also shouldn't pull Kafka Streams (since that's not used by Spark), and kafka-clients is transitively pulled by sql-kafka, so don't need that either