0

I am using below Python code for reading data from "MongoDB" in Spark and converting it DataFrame:

from pyspark.sql import SparkSession

# Initialize a Spark session
spark = SparkSession.builder \
    .appName("MongoDB Spark Connector Example") \
    .config("spark.mongodb.read.connection.uri", "mongodb://localhost:23017/") \
    .config("spark.mongodb.read.database", "db_name") \
    .config("spark.mongodb.read.collection", "coll_name") \
    .config("spark.sql.debug.maxToStringFields", 1000) \
    .getOrCreate()
df = spark.read.format("mongodb").load()
df.createOrReplaceTempView("temp")
sqlDf = spark.sql("SELECT id from temp")
sqlDf.show()

I am using:

  1. Spark Version: 3.2.4
  2. Mongo DB Version: 6
  3. Scala Version: 2.12.15
  4. Java 1.8
  5. Python 3.8
  6. Ubuntu 20.0.4
  7. mongo-spark-connector_2.12:10.2.0

I am using below command to run above code

spark-submit  --packages org.mongodb.spark:mongo-spark-connector_2.12:10.2.0 test.py

I am getting the below error:

py4j.protocol.Py4JJavaError: An error occurred while calling o67.showString.
: java.lang.NoSuchMethodError: org.apache.spark.sql.types.StructType.toAttributes()Lscala/collection/immutable/Seq;
    at com.mongodb.spark.sql.connector.schema.InternalRowToRowFunction.<init>(InternalRowToRowFunction.java:46)
    at com.mongodb.spark.sql.connector.schema.RowToBsonDocumentConverter.<init>(RowToBsonDocumentConverter.java:84)
    at com.mongodb.spark.sql.connector.read.MongoScanBuilder.<clinit>(MongoScanBuilder.java:72)
    at com.mongodb.spark.sql.connector.MongoTable.newScanBuilder(MongoTable.java:121)

1 Answer 1

0

This Is probably related a version conflict between scala, pyspark and the mongodb driver you're using.

You can check the installed versions of spark and scala and search for the corresponding mongo dependency.

You can also try to uninstall everything and redo the installation by choosing the good versions from the start.

Check the spark official doc here for installation details if needed.

Sign up to request clarification or add additional context in comments.

3 Comments

All version were correct but somehow the error was there, I tried the same thing inside virtual box and it worked. It means there was some version conflict in previous installation.
You can uninstall everything and restart from scratch
@GauravSrivastava do not hesitate to upvote and accept the answer if it was helpful. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.