Unable to Read Collection/Files from MongoDB using Azure Databricks

simha08
New Contributor II

Hi there,

Can someone help to read data from MongoDB using Azure Databricks? Surprisingly, I am able to connect from Jupyter Notebook and read data, but not from the Azure Databricks.

1) I have install the required spark-connector packages in the cluster. 2) I have a valid MongoDB Atlas Cluster. 3) I have Spark cluster 13.3 LTS.

I am not even able to read the local database which I have created in Mongo DB Compass which has port 27017.

Can someone please help?

Alberto_Umana
Databricks Employee
Databricks Employee

Hi @simha08,

Can you test connectivity first to the MongoDB? using nc -vz hostname port.

Also please share the code you are using to establish the connection and read data.

simha08
New Contributor II

I am using following code to read the data from mongoDB using Databricks

from pyspark.sql import SparkSession
spark = SparkSession \
.builder \
.appName("myApp") \
.config("spark.mongodb.connection.uri", "mongodb+srv://username:password@cluster.xxxx.mongodb.net/database.collection") \
.getOrCreate()

df = spark.read.format("mongo").load()