问题描述
我想从MS sql数据库(不在Azure上托管)检索数据到Microsoft Azure Databricks笔记本。 这是我所做的步骤:
- 在azure门户上进入并创建资源组
- 创建Azure Databricks服务(但我不使用“在您自己的虚拟网络(VNet)中部署Azure Databricks工作区”选项→也许我应该...)
- 一旦Azure Databricks服务准备就绪,我将启动它并创建没有特定配置的群集
- 然后我使用此脚本创建一个笔记本(在先前的群集上运行)
mssqlServer = "jdbc:sqlserver://xxx.xxx.xxx.xxx:1433;ApplicationIntent=readonly;databaseName=" + mssqlDatabase
query = """(select * from mytable)foo"""
df = (
spark.read.format("jdbc")
.option("url",mssqlServer)
.option("dbtable",query)
.option("user",mssqlUser)
.option("password",mssqlPassword)
.load()
)
我得到这个错误:
com.microsoft.sqlserver.jdbc.sqlServerException: The TCP/IP connection to the host xxx.xxx.xxx.xxx,port 1433 has Failed. Error: "connect timed out. Verify the connection properties. Make sure that an instance of sql Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
在询问StackoverFlow之前,我已经联系了我的公司网络和DBA团队。 DBA表示连接正常,但立即断开连接。”
为您提供信息,我已按照本教程https://docs.microsoft.com/en-us/azure/databricks/data/data-sources/sql-databases
也许有些东西需要配置,但是我根本不在网络中(我只是一个小数据科学家,他想在天蓝色的数据块上使用笔记本电脑并访问他的公司数据库)。例如,我如何Make sure that TCP connections to the port are not blocked by a firewall
?
如果您有任何想法或已经遇到了此问题,请随时分享。 :)
如果您需要更多信息,请告诉我。
解决方法
如果已经将Azure SQL数据库配置为侦听端口1433上的TCP / IP通信,则可能是以下三个原因之一:
- 正确的JDBC连接字符串。
- 防火墙阻止了传入连接。
- Azure SQL数据库未运行。
从Azure门户获取Azure SQL数据库JDBC连接字符串。
使用python和JDBC的SQL数据库:
jdbcHostname = "chepra.database.windows.net"
jdbcDatabase = "chepra"
jdbcPort = "1433"
username = "chepra"
password = "XXXXXXXXXX"
jdbcUrl = "jdbc:sqlserver://{0}:{1};database={2}".format(jdbcHostname,jdbcPort,jdbcDatabase)
connectionProperties = {
"user" : username,"password" : password,"driver" : "com.microsoft.sqlserver.jdbc.SQLServerDriver"
}
pushdown_query = "(Select * from customers where CustomerID = 2) CustomerID"
df = spark.read.jdbc(url=jdbcUrl,table=pushdown_query,properties=connectionProperties)
display(df)
使用Scala使用JDBC的SQL数据库:
val jdbcHostname = "chepra.database.windows.net"
val jdbcPort = 1433
val jdbcDatabase = "chepra"
// Create the JDBC URL without passing in the user and password parameters.
val jdbcUrl = s"jdbc:sqlserver://${jdbcHostname}:${jdbcPort};database=${jdbcDatabase}"
// Create a Properties() object to hold the parameters.
import java.util.Properties
val connectionProperties = new Properties()
connectionProperties.put("user",s"chepra")
connectionProperties.put("password",s"XXXXXXXXXX")
val employees_table = spark.read.jdbc(jdbcUrl,"customers",connectionProperties)
employees_table.show()