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

Join Us as a Local Community Builder!

Passionate about hosting events and connecting people? Help us grow a vibrant local communityโ€”sign up today to get started!

Sign Up Now