Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get Fabric certified for FREE! Don't miss your chance! Learn more

Reply
P_work
New Contributor III

Spark reading writing to SQL Database

The following blog post illustrates reading/writing to Fabric Warehouse from Spark notebook.

@https://community.fabric.microsoft.com/t5/Data-Engineering-Community-Blog/Spark-Connector-for-Fabric-Warehouse-Unified-Analytics/ba-p/4611309

 

Are there examples to read and write to SQL Database(preview) using Spark within Fabric notebook? Say even this (currently failing): 

 df.write.jdbc(...)

 

2 ACCEPTED SOLUTIONS
v-lgarikapat
Honored Contributor

Hi @P_work ,

Thanks for reaching out to the Microsoft fabric community forum.

 

@nilendraFabric  

Thanks for your prompt response

@P_work , I have attached the official document and relevant solved threads related to your use case. I hope you find them helpful

 

Spark connector for Microsoft Fabric Data Warehouse - Microsoft Fabric | Microsoft Learn

Solved: Re: Fabric Notebook Outputs to Azure SQL Database ... - Microsoft Fabric Community

Solved: Re: Write df to DataWarehouse tables - Microsoft Fabric Community

 

If this post helped resolve your issue, please consider giving it Kudos and marking it as the Accepted Solution. This not only acknowledges the support provided but also helps other community members find relevant solutions more easily.

We appreciate your engagement and thank you for being an active part of the community.

Best regards,
LakshmiNarayana
.

 

 

 

View solution in original post

g3kuser
New Contributor III

Here is how you connect to SQL DB from Fabric Notebook using SPN credentials

host = ''
database = ''
sql_query = ''
jdbc_url = f'jdbc:sqlserver://{host};database={database};authentication=ActiveDirectoryServicePrincipal;encrypt=true;trustServerCertificate=true;'
db_properties = {
"user": f"{client_id}",
"password": f"{client_secret}",
"driver": "com.microsoft.sqlserver.jdbc.SQLServerDriver"
}
#For Reading
spark.read.jdbc(url=jdbc_url, table=f"({sql_query}) AS query_alias", properties=db_properties)
#For Writing data
df.write.jdbc(url=jdbc_url, table=table_name, mode=mode, properties=db_properties)


View solution in original post

7 REPLIES 7
nilendraFabric
Honored Contributor

Hi @P_work 

 

try this

 

df.write.mode("append").option("mergeSchema", "true").synapsesql("DW1.Core.CustomerData")

 

 

I believe the main issue was this:

notebookutils.credentials.getToken("pbi")
 
The examples provided were of little use.
 
Also, ensuring using runtime 1.3 appeared to help.
v-lgarikapat
Honored Contributor

Hi @P_work ,

Thanks for reaching out to the Microsoft fabric community forum.

 

@nilendraFabric  

Thanks for your prompt response

@P_work , I have attached the official document and relevant solved threads related to your use case. I hope you find them helpful

 

Spark connector for Microsoft Fabric Data Warehouse - Microsoft Fabric | Microsoft Learn

Solved: Re: Fabric Notebook Outputs to Azure SQL Database ... - Microsoft Fabric Community

Solved: Re: Write df to DataWarehouse tables - Microsoft Fabric Community

 

If this post helped resolve your issue, please consider giving it Kudos and marking it as the Accepted Solution. This not only acknowledges the support provided but also helps other community members find relevant solutions more easily.

We appreciate your engagement and thank you for being an active part of the community.

Best regards,
LakshmiNarayana
.

 

 

 

Hi @P_work ,

If your issue has been resolved, please consider marking the most helpful reply as the accepted solution. This helps other community members who may encounter the same issue to find answers more efficiently.

If you're still facing challenges, feel free to let us know we’ll be glad to assist you further.

Looking forward to your response.

Best regards,
LakshmiNarayana.

Hi @P_work ,

 

If your issue has been resolved, please consider marking the most helpful reply as the accepted solution. This helps other community members who may encounter the same issue to find answers more efficiently.

If you're still facing challenges, feel free to let us know we’ll be glad to assist you further.

Looking forward to your response.

Best regards,
LakshmiNarayana.

Hi @P_work ,

 

As we haven't heard back from you, we are closing this thread. If you are still experiencing the issue, please feel free to create a new thread we’ll be happy to assist you further.

Thank you for your patience and support.

If you found our response helpful, please mark it as Accepted Solution so others with similar queries can find it easily.

 

Best Regards,

Lakshmi Narayana

g3kuser
New Contributor III

Here is how you connect to SQL DB from Fabric Notebook using SPN credentials

host = ''
database = ''
sql_query = ''
jdbc_url = f'jdbc:sqlserver://{host};database={database};authentication=ActiveDirectoryServicePrincipal;encrypt=true;trustServerCertificate=true;'
db_properties = {
"user": f"{client_id}",
"password": f"{client_secret}",
"driver": "com.microsoft.sqlserver.jdbc.SQLServerDriver"
}
#For Reading
spark.read.jdbc(url=jdbc_url, table=f"({sql_query}) AS query_alias", properties=db_properties)
#For Writing data
df.write.jdbc(url=jdbc_url, table=table_name, mode=mode, properties=db_properties)


Helpful resources

Announcements
Sticker Challenge 2026 Carousel

Join our Community Sticker Challenge 2026

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

Free Fabric Certifications

Free Fabric Certifications

Get Fabric certified for free! Don't miss your chance.

January Fabric Update Carousel

Fabric Monthly Update - January 2026

Check out the January 2026 Fabric update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Users online (78)