Skip to content

Instantly share code, notes, and snippets.

@DhyanRathore
Last active July 26, 2023 14:03
Show Gist options
  • Save DhyanRathore/a9a029bded41c5736f6e376f7ae3f6b4 to your computer and use it in GitHub Desktop.
Save DhyanRathore/a9a029bded41c5736f6e376f7ae3f6b4 to your computer and use it in GitHub Desktop.
Connecting to Azure SQL Databases from Azure Databricks with Screts Scope
# Python code to connect to Azure SQL Databases from Azure Databricks with Screts Scope
# Author: Dhyanendra Singh Rathore
# Declare variables for creating JDBC URL
jdbcHostname = "sql-csv-data-server.database.windows.net" # Replace with your SQL Server name
jdbcPort = 1433 # Replace with your SQL Server port number
jdbcDatabase = "syn-csv-data-dw" # Replace with your database name
# Connection secrets from vault
jdbcUsername = dbutils.secrets.get(scope="CSVProjectKeyVault",key="SQLAdmin") # Replace the scope and key accordingly
jdbcPassword = dbutils.secrets.get(scope="CSVProjectKeyVault",key="SQLAdminPwd") # Replace the scope and key accordingly
# Create JDBC URL
jdbcUrl = "jdbc:sqlserver://{0}:{1};database={2}".format(jdbcHostname, jdbcPort, jdbcDatabase)
connectionProperties = {
"user" : jdbcUsername,
"password" : jdbcPassword,
"driver" : "com.microsoft.sqlserver.jdbc.SQLServerDriver"
}
pushdown_query = "(SELECT 'Hello World' AS TEXTCOL) t"
df = spark.read.jdbc(url=jdbcUrl, table=pushdown_query, properties=connectionProperties)
display(df)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment