1

I want to connect Azure SQL Database with Azure Databricks. There is no option given. Whats the technique for connectivity. Anyone can help me. Much appreciated.

1

1 Answer 1

2
Answer recommended by Microsoft Azure Collective

Using SQL Server authentication and the Spark connector try the following code:

val config = Config(Map(
  "url"            -> "kkk-server.database.windows.net:1433",
  "databaseName"   -> "MyDatabase",
  "dbTable"        -> "dbo.Clients",
  "user"           -> "login",
  "password"       -> "xxxxxxxx",
  "connectTimeout" -> "5", //seconds
  "queryTimeout"   -> "5"  //seconds
))

Using Active Directory Authentication you can try code below:

import com.microsoft.azure.sqldb.spark.config.Config
import com.microsoft.azure.sqldb.spark.connect._

val config = Config(Map(
  "url"            -> "kkk-server.database.windows.net:1433",
  "databaseName"   -> "MyDatabase",
  "dbTable"        -> "dbo.Clients",
  "user"           -> "AD-account",
  "password"       -> "xxxxxxxx",
  "connectTimeout" -> "5", //seconds
  "queryTimeout"   -> "5"  //seconds
))

val collection = spark.read.sqlDB(config)
collection.show()

If you are interested in AD authentication using a token, please visit this article.

If you are using Python and Azure Databricks, try below code with JDBC:

jdbcHostname = "xxxxxxx.database.windows.net"
jdbcDatabase = "yyyyyy"
jdbcPort = 1433
#jdbcUrl = "jdbc:sqlserver://{0}:{1};database={2};user={3};password={4}".format(jdbcHostname, jdbcPort, jdbcDatabase, username, password)

jdbcUrl = "jdbc:sqlserver://{0}:{1};database={2}".format(jdbcHostname, jdbcPort, jdbcDatabase)
connectionProperties = {
  "user" : jdbcUsername,
  "password" : jdbcPassword,
  "driver" : "com.microsoft.sqlserver.jdbc.SQLServerDriver"
}

pushdown_query = "(INSERT INTO test (a, b) VALUES ('val_a', 'val_b')) insert_test" 

This tutorial may be useful to connect to a database using JDBC.

Sign up to request clarification or add additional context in comments.

2 Comments

Getting this error : java.lang.IllegalArgumentException: Secret does not exist with scope: key-vault-secrets and key . I have added Secret Username and Password.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.