Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does spark-submit ignore the package that I include as part of the configuration of my spark session?

I am trying to include the org.apache.spark:spark-sql-kafka-0-10_2.11:2.4.3 package as part of my spark code (via the SparkSession Builder). I understand that I can download the JAR myself and include it but I would like to figure out why the following is not working as expected:

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()

When I run the job:

spark-submit main.py

I receive the following error:

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)

If I instead include the packages via the --packages flag, the dependencies are downloaded and the code runs as expected:

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

The code also works if I open the PySpark shell and paste the code above. Is there a reason that the spark-submit ignores the configuration?

like image 879
Brandon Avatar asked Oct 19 '25 14:10

Brandon


2 Answers

I think that for configurations like "spark.jars.packages", these should be configured either in spark-defaults or passed by command-line arguments, setting it in the runtime shouldn't work.


Against better judgement

I remember some people claimed something like this worked for them, but I would say that the dependency is already somewhere there (installed in local repo), just loaded.

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()
like image 163
Saša Zejnilović Avatar answered Oct 21 '25 13:10

Saša Zejnilović


When you run spark-submit, it already creates a SparkSession that is reused by your code - thus you have to provide everything through spark-submit.

However, you do not need to actually use spark-submit to run your Spark code. Assuming your main method looks like this:

def main():
    spark = SparkSession.builder.config(...).getOrCreate()
    # your spark code below
    ...

You can run this code just via python:

> python ./my_spark_script.py

This will run your program correctly

like image 28
George Avatar answered Oct 21 '25 13:10

George



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!