cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

An example how to connect to SQL Server data using windows authentication

gillzer84
New Contributor

We use SQL Server to store data. I would like to connect to SQL to pull manipulate and sometimes push data back. I've seen some examples online of connecting but I cannot successfully re-create.

3 REPLIES 3

Junee
New Contributor III

You can use jTDS library from maven, add this to your cluster. Once installed, you can write the below code to connect to your Database.

Code in Scala will be:

import java.util.Properties
 
val driverClass = "net.sourceforge.jtds.jdbc.Driver"
val serverAdd = ""
val databaseName = ""
val domain = "" // use domain of your active directory
val database_port = ""
 
val jdbcUsername = "" 
val jdbcPassword = ""
 
val conStr = "jdbc:jtds:sqlserver://"+ serverAdd + ":" + database_port +"/" + databaseName + ";useNTLMv2=true;domain=" + domain  + ";"
 
val connectionProperties = new Properties()
connectionProperties.put("user",s"${jdbcUsername}")
connectionProperties.put("password",s"${jdbcPassword}")
connectionProperties.setProperty("Driver", driverClass)
 
 
val dataFrame = spark.read.jdbc(url = conStr,  
                                table = "(select * from table_name) a",
                                properties = connectionProperties)

Would someone provide example with IntegratedSecurity instead of user name and passworld?

*password*

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you wonโ€™t want to miss the chance to attend and share knowledge.

If there isnโ€™t a group near you, start one and help create a community that brings people together.

Request a New Group