1

I have a Serverless SQL pool which contains external tables/Views write against ADLS Gen2 parquet files.

I want to call external table / views from synapse notebook using python.

FYI. Server less SQL Pool Identity is Azure Active directory.

Can you please let me know how can achieve this.

Using Scala it is working the below code. Need same in the python using active directory authentication.

// Define connection:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver")

val hostname = "demo-da-synapse-test-ondemand.sql.azuresynapse.net"
val port = 1433
val database = "Demo" // If needed, change the database 
val jdbcUrl = s"jdbc:sqlserver://${hostname}:${port};database=${database}"

// Define connection properties:
import java.util.Properties

val props = new Properties()
props.put("user", "testuser")
props.put("password", "test202XXX!")

// Assign driver to connection:
val driverClass = "com.microsoft.sqlserver.jdbc.SQLServerDriver"
props.setProperty("Driver", driverClass)

val tsqlQuery ="select top 100 * from  [demo].[tblEmp]"

val df = spark.read.jdbc(jdbcUrl, s"(${tsqlQuery}) res", props)

df.show(10)


Thank you, Sunny

Sunny
  • 13
  • 4

1 Answers1

0

To connect Serverless SQL pool with Synapse python notebook with Azure AD authentication. you need to install ADAL4j library on your spark pool.

You can download ADAL4j library from here

After downloading it extract and upload it in workspace

Goto Manage >> Workspace packages >> Upload >>select jar files you downloaded.

enter image description here

Then add this package in your spark pool as below.

Go to Manage >> Apache Spark pool >> 3 dots beside saprk pool name >> packages.

enter image description here

Click on select from workspace packages:

enter image description here

select all packages and click apply the changes.

enter image description here

Then try below code in your synapse notebook:

jdbcHostname = "servername-ondemand.sql.azuresynapse.net"
jdbcDatabase = "dbname"
jdbcPort = "1433"
username = "Adusername"
password = "AD password"
jdbcUrl = "jdbc:sqlserver://{0}:{1};database={2};encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.sql.azuresynapse.net;loginTimeout=30;authentication=ActiveDirectoryPassword".format(jdbcHostname, jdbcPort, jdbcDatabase)
connectionProperties = {
"user" : username,
"password" : password,
"driver" : "com.microsoft.sqlserver.jdbc.SQLServerDriver"
}
pushdown_query = "select * from ext1"
df = spark.read.jdbc(url=jdbcUrl, table=pushdown_query, properties=connectionProperties)
display(df)

If you receive any authentication error error-code Make sure you disable Security Defaults by visiting > Azure Active Directory > Security > Security Defaults > Disable

If the error persists after disabling Security Defaults, Check if there's an MFA or any Conditional Access Policy applied to the user by visiting > Azure Active Directory > Security > Conditional Access > Remove Policy or exclude the user from the Policy.

Pratik Lad
  • 4,343
  • 2
  • 3
  • 11
  • Thanks Pratik, I did all the steps. i am getting the below error.Py4JJavaError: An error occurred while calling o3867.jdbc. : com.microsoft.sqlserver.jdbc.SQLServerException: Failed to load ADAL4J Java library for performing ActiveDirectoryPassword authentication. at com.microsoft.sqlserver.jdbc.SQLServerConnection.getFedAuthToken(SQLServerConnection.java:4440) – Sunny Jun 23 '23 at 16:14