synapse pyspark write dataframe to sql azure

Simon Zhang 26 Reputation points Microsoft Employee
2024-07-26T23:35:52.9533333+00:00

I want to use Pyspark datawrite.write method to write dataframe to sql azure database with linked service. How can I achieve it?

Azure SQL Database
Azure Synapse Analytics
Azure Synapse Analytics
An Azure analytics service that brings together data integration, enterprise data warehousing, and big data analytics. Previously known as Azure SQL Data Warehouse.
4,998 questions
{count} votes

Accepted answer
  1. Chandra Boorla 2,990 Reputation points Microsoft Vendor
    2024-07-31T13:43:45.0033333+00:00

    Hi @Simon Zhang

    I'm glad that you were able to resolve your issue and thank you for posting your solution so that others experiencing the same thing can easily reference this! Since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others ", I'll repost your solution in case you'd like to accept the answer.

    Issue: I want to use Pyspark datawrite.write method to write dataframe to sql azure database with linked service. How can I achieve it?

    Solution:

    Here is the answer I got:

    server = 'sql server name'

    Port = 1433

    Database = "databaseName"

    jdbcUrl = f"jdbc:sqlserver://{server}:{Port};databaseName={Database};encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30"

    sql_access_token=TokenLibrary.getConnectionString("linked service name")

    conn_Prop = {

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

    "accessToken" : sql_access_token

    }

    restapiJson.write\

    .mode("overwrite")\
    
    .option("truncae","true")\
    
    .option("url",jdbcUrl)\
    
    .option("dbtable",dbtable)\
    
    .jdbc(jdbcUrl, dbtable, properties={"driver" : "com.microsoft.sqlserver.jdbc.SQLServerDriver","accessToken" : sql_access_token})
    

    If I missed anything please let me know and I'd be happy to add it to my answer, or feel free to comment below with any additional information.

    Hope this helps. Do let us know if you any further queries.


    If this answers your query, do click Accept Answer and **Yes **for was this answer helpful. And, if you have any further query do let us know.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Simon Zhang 26 Reputation points Microsoft Employee
    2024-07-30T16:11:44.3166667+00:00

    My answer is here:

    server = 'sql server name'

    Port = 1433

    Database = "databaseName"

    jdbcUrl = f"jdbc:sqlserver://{server}:{Port};databaseName={Database};encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30"

    sql_access_token=TokenLibrary.getConnectionString("linked service name")

    conn_Prop = {

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

    "accessToken" : sql_access_token

    }

    restapiJson.write\

    .mode("overwrite")\
    
    .option("truncae","true")\
    
    .option("url",jdbcUrl)\
    
    .option("dbtable",dbtable)\
    
    .jdbc(jdbcUrl, dbtable, properties={"driver" : "com.microsoft.sqlserver.jdbc.SQLServerDriver","accessToken" : sql_access_token})
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.