火花提交错误由以下原因引起:java.lang.ClassNotFoundException:kafka.DefaultSource

spark-submit error Caused by: java.lang.ClassNotFoundException: kafka.DefaultSource

在我的 spark 程序中,我有这样的代码:

val df = spark.readStream
        .format("kafka")
        .option("subscribe", "raw_weather")
        .option("kafka.bootstrap.servers", "<url:port>s of my brokers")
        .option("kafka.security.protocol", "SASL_SSL")
        .option("kafka.sasl.mechanism" , "PLAIN")
        .option("kafka.sasl.jaas.config", 
"org.apache.kafka.common.security.plain.PlainLoginModule required username=\"username\" password=\"" + "password" + "\";")
        .option("kafka.ssl.protocol", "TLSv1.2")
        .option("kafka.ssl.enabled.protocols", "TLSv1.2")
        .option("kafka.ssl.endpoint.identification.algorithm", "HTTPS")
        .load()

这是我的 build.sbt 文件:

    name := "kafka-streaming"
    version := "1.0"

    scalaVersion := "2.11.12"

    // still want to be able to run in sbt
    // https://github.com/sbt/sbt-assembly#-provided-configuration
    run in Compile <<= Defaults.runTask(fullClasspath in Compile, mainClass in (Compile, run), runner in (Compile, run))

    fork in run := true
    javaOptions in run ++= Seq(
        "-Dlog4j.debug=true",
        "-Dlog4j.configuration=log4j.properties")

    assemblyMergeStrategy in assembly := {
        case "META-INF/services/org.apache.spark.sql.sources.DataSourceRegister" => MergeStrategy.concat
        case PathList("META-INF", _*) => MergeStrategy.discard
        case _ => MergeStrategy.first
    }

    libraryDependencies ++= Seq(
        "org.apache.spark" %% "spark-core" % "2.4.0" % "provided",
        "org.apache.spark" %% "spark-sql" % "2.4.0" % "provided", //If not then this Exception in thread "streaming-job-executor-0" java.lang.NoClassDefFoundError: org/apache/spark/sql/Dataset
        "org.apache.spark" %% "spark-streaming" % "2.4.0" % "provided",
        "org.apache.spark" %% "spark-streaming-kafka-0-10" % "2.4.0" % "provided",
        "org.apache.spark" %% "spark-sql-kafka-0-10" % "2.4.0" % "provided"
    )

如果我从 libraryDependencies 中删除 provided,我就能成功 运行 IntelliJ IDEA 中的 scala 代码。

现在我执行 sbt assembly,然后当我尝试 运行 使用以下命令在 spark-submit 中运行相同的程序时:

spark-submit --class com.ibm.kafkasparkintegration.executables.WeatherDataStream hdfs://<some address>:8020/user/clsadmin/consumer-example.jar \
             --packages org.apache.spark:spark-core:2.4.0,org.apache.spark:spark-sql:2.4.0,org.apache.spark:spark-sql-kafka-0-10:2.4.0,org.apache.spark:spark-streaming:2.4.0,org.apache.spark:spark-streaming-kafka-0-10:2.4.0

(PS。consumer-example.jar 是我在 sbt assembly)

之后得到的 JAR

我收到这个错误:

Exception in thread "main" java.lang.ClassNotFoundException: Failed to find data source: kafka. Please find packages at http://spark.apache.org/third-party-projects.html
    at org.apache.spark.sql.execution.datasources.DataSource$.lookupDataSource(DataSource.scala:635)
    at org.apache.spark.sql.streaming.DataStreamReader.load(DataStreamReader.scala:159)
    >> at com.ibm.kafkasparkintegration.executables.WeatherDataStream$.getRawDataFrame(WeatherDataStream.scala:73) 
    at com.ibm.kafkasparkintegration.executables.WeatherDataStream$.main(WeatherDataStream.scala:23)
    at com.ibm.kafkasparkintegration.executables.WeatherDataStream.main(WeatherDataStream.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.JavaMainApplication.start(SparkApplication.scala:52)
    at org.apache.spark.deploy.SparkSubmit$.org$apache$spark$deploy$SparkSubmit$$runMain(SparkSubmit.scala:906)
    at org.apache.spark.deploy.SparkSubmit$.doRunMain(SparkSubmit.scala:197)
    at org.apache.spark.deploy.SparkSubmit$.submit(SparkSubmit.scala:227)
    at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:136)
    at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala)
Caused by: java.lang.ClassNotFoundException: kafka.DefaultSource
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at org.apache.spark.sql.execution.datasources.DataSource$$anonfun$$anonfun$apply.apply(DataSource.scala:618)
    at org.apache.spark.sql.execution.datasources.DataSource$$anonfun$$anonfun$apply.apply(DataSource.scala:618)
    at scala.util.Try$.apply(Try.scala:192)
    at org.apache.spark.sql.execution.datasources.DataSource$$anonfun.apply(DataSource.scala:618)
    at org.apache.spark.sql.execution.datasources.DataSource$$anonfun.apply(DataSource.scala:618)
    at scala.util.Try.orElse(Try.scala:84)
    at org.apache.spark.sql.execution.datasources.DataSource$.lookupDataSource(DataSource.scala:618)
    ... 14 more

从错误日志中可以看出,WeatherDataStream.scala:73指的是上面代码中写的load()当此代码在 IntelliJ 中工作时,我不明白为什么它在 spark-submit 中不起作用?

这是由于这里的这一行:

"org.apache.spark" %% "spark-sql-kafka-0-10" % "2.4.0" % "provided"

"provided" 意味着这种依赖性是,好吧...... "provided" 由你使用的任何机器 运行 编译的 jar。在我看来,您的机器似乎没有提供 kafka,因此请尝试删除 "provided" 和 re-assemble.