为什么spark submit会忽略我在spark会话配置中包含的包?

envsm3lx  于 2021-05-27  发布在  Spark
关注(0)|答案(1)|浏览(540)

我想把 org.apache.spark:spark-sql-kafka-0-10_2.11:2.4.3 包作为我的Spark代码的一部分(通过 SparkSession 建筑商)。我知道我可以自己下载jar并将其包含在内,但我想弄清楚为什么下面的内容不能按预期工作:

from pyspark.sql import SparkSession
import pyspark
import json

if __name__ == "__main__":
    spark = SparkSession.builder \
        .master("local") \
        .appName("App Name") \
        .config("spark.jars.packages", "org.apache.spark:spark-sql-kafka-0-10_2.11:2.4.3") \
        .getOrCreate()

    df = spark \
        .readStream \
        .format("kafka") \
        .option("kafka.bootstrap.servers", "localhost:9092") \
        .option("subscribe", "first_topic") \
        .load() \
        .selectExpr("CAST(key AS STRING)", "CAST(value AS STRING)")

    query = df \
        .writeStream \
        .format("console") \
        .outputMode("update") \
        .start()

当我运行作业时:

spark-submit main.py

我收到以下错误:

py4j.protocol.Py4JJavaError: An error occurred while calling o48.load.
: 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".;
    at org.apache.spark.sql.execution.datasources.DataSource$.lookupDataSource(DataSource.scala:652)
    at org.apache.spark.sql.streaming.DataStreamReader.load(DataStreamReader.scala:161)
    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 py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244)
    at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
    at py4j.Gateway.invoke(Gateway.java:282)
    at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
    at py4j.commands.CallCommand.execute(CallCommand.java:79)
    at py4j.GatewayConnection.run(GatewayConnection.java:238)
    at java.lang.Thread.run(Thread.java:748)

如果我通过 --packages 标志时,将下载依赖项并按预期运行代码:

spark-submit --packages org.apache.spark:spark-sql-kafka-0-10_2.11:2.4.3 main.py

如果我打开pyspark shell并粘贴上面的代码,代码也可以工作。有没有什么原因 spark-submit 忽略配置?

k3bvogb1

k3bvogb11#

我认为对于像“spark.jars.packages”这样的配置,应该在spark默认值中或者通过命令行参数进行配置,在运行时设置它不应该起作用。
反对更好的判断
我记得有些人声称这样的东西对他们有用,但我要说的是,依赖性已经在那里的某个地方(安装在本地repo中),刚刚加载。

conf = pyspark.SparkConf()
conf.set("spark.jars.packages", "org.apache.spark:spark-sql-kafka-0-10_2.11:2.4.3")

spark = SparkSession.builder \
        .master("local") \
        .appName("App Name") \
        .config(conf = conf) \
        .getOrCreate()

相关问题