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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
rui_mtcarvalho
Regular Visitor

Dataframe write to Warehouse through Notebook. Is possible?

Hi, 

 

I believe is not possible to save the content from a dataframe directly to a Warehouse. Since I can only run Notebooks on Lakehouse.

 

Is this true? Or theres a way to do it that I´m not seeing, 

 

Thanks in advance!

1 ACCEPTED SOLUTION
puneetvijwani
Resolver IV
Resolver IV

@rui_mtcarvalho you can write the data to lake house and use it with three part name in warehouse 
Here is the Session where pawel showed this in our community session , skip to 1:02
https://www.youtube.com/watch?v=u7peyrNhYgw

View solution in original post

4 REPLIES 4
leisun
Microsoft Employee
Microsoft Employee

we can use spark jdbc way , but the perfoamce is pretty bad.  the easiest way is to use adf copy activity.

 

import os

from pyspark.sql.types import StringType

 

# acquire access token

access_token = mssparkutils.credentials.getToken("pbi")

# define JDBC URL and connection properties

jdbc_url = "jdbc:sqlserver://<dwhost>:1433;database=<dwName>;encrypt=true;trustServerCertificate=true;hostNameInCertificate=*.database.windows.net;loginTimeout=30"

 

df = spark.sql("select 1 as id , 'james' as name, 1.3 as cost, 'jack' as name2 ")

 

def map_to_sql_type(data_type):

    if isinstance(data_type, StringType):

        return "VARCHAR(255)"

    return ""  # Return an empty string for non-StringType

 

# Get the schema of the DataFrame

schema = df.schema

 

# Generate the SQL schema string, skipping non-StringType columns

sql_schema = ", ".join([f"{field.name} {map_to_sql_type(field.dataType)}" for field in schema if isinstance(field.dataType, StringType)])

 

connection_properties = {

    "accessToken": access_token,

    "driver": "com.microsoft.sqlserver.jdbc.SQLServerDriver"

}

 

# read data from sql DW

df.write.option("createTableColumnTypes", sql_schema).mode("overwrite").jdbc(

    url=jdbc_url,

    table="test6",

    properties=connection_properties

)

 

Please change the red part accordingly in above code,

Dwhost can be found in

leisun_0-1725529564277.png

 

BryanCarmichael
Advocate I
Advocate I

Do you need it in the Warehouse given you can consume it in the Warehosue from the Lakehouse with no perceivable performance impact ?

 

If you do then why not use a datapipeline to run your notebook to load to the warehouse and then use a on success copy data activity to move to from lakehouse to warehouse ?

puneetvijwani
Resolver IV
Resolver IV

@rui_mtcarvalho you can write the data to lake house and use it with three part name in warehouse 
Here is the Session where pawel showed this in our community session , skip to 1:02
https://www.youtube.com/watch?v=u7peyrNhYgw

DennesTorres
Impactful Individual
Impactful Individual

Hi,

If the lakehouse has a shortcut to the datawarehouse, I believe you may be able to save the dataframe to the warehouse through the shortcut.

Kind Regards,

 

Dennes

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.