cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

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?

2 REPLIES 2

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

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group