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.

John
  • 75
  • 1
  • 5
  • does this part of the documentation not cover it? https://learn.microsoft.com/en-us/azure/databricks/data/data-sources/sql-databases-azure – arboc7 Dec 01 '19 at 07:57

1 Answers1

2

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Alberto Morillo
  • 13,893
  • 2
  • 24
  • 30
  • Getting this error : java.lang.IllegalArgumentException: Secret does not exist with scope: key-vault-secrets and key . I have added Secret Username and Password. – John Dec 02 '19 at 00:50
  • Create the secret for your app https://blogs.technet.microsoft.com/stefan_stranger/2018/06/06/connect-to-azure-sql-database-by-obtaining-a-token-from-azure-active-directory-aad/?wt.mc_id=MVP – Alberto Morillo Dec 02 '19 at 02:33