Can we connect sql server databases using SPN in databricks using scala?

Viewed 584

My use case is to connect to the SQL server using SPN credentials in Databricks notebook. Since I don't have username password and can't generate access token so I have to do it with the help of SPN only. I couldn't find anything until now. Please help how to do it if anyone has done it before or has an idea about it. Thanks in advance :)

1 Answers

Yes, it's possible to do that and it's described in the repository of sql-spark-connector connector and in documentation. The prerequisite is installation of the azure-activedirectory-library-for-java library. After both dependencies are installed, following code should work:

context = adal.AuthenticationContext(authority)
token = context.acquire_token_with_client_credentials(
      resource_app_id_url, service_principal_id, service_principal_secret)
access_token = token["accessToken"]

jdbc_db = spark.read \
        .format("com.microsoft.sqlserver.jdbc.spark") \
        .option("url", url) \
        .option("dbtable", table_name) \
        .option("accessToken", access_token) \
        .option("encrypt", "true") \
        .option("hostNameInCertificate", "*.database.windows.net") \
        .load()
Related