sql-server - 无法从 Microsoft Azure Databricks 笔记本中加载 MS SQL 数据库中的数据
问题描述
我想将数据从 MS SQL 数据库(未托管在 Azure 上)检索到 Microsoft Azure Databricks 笔记本。以下是我所做的步骤:
- 进入天蓝色的门户并创建资源组
- 创建 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
也许需要配置一些东西,但我根本不在网络中(我只是一个小数据科学家,想在 azure databricks 上玩笔记本并访问他的公司数据库)。例如我该怎么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 连接字符串。
使用 JDBC 和 Python 的 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)
使用 JDBC 和 Scala 的 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()
推荐阅读
- c# - 调试时解决方案没有错误
- synology - 如何在 Synology NAS 中导出用户列表和密码
- wpf - 建立连接时操作被中止
- spring-aop - Spring Framework中的加载时间编织未正确记录
- php - 无需重新加载 AJAX/PHP 即可更新页面
- python-3.x - 将 dtype float64 的图形转换为 float32
- encryption - 如何在 groovy 中缩短 URL?
- android - 如何以编程方式确定 Android 应用程序是否在三星 Galaxy 上进入睡眠状态?
- python - 测试的 sql 脚本上的 SQLite3 语法错误
- google-sheets - 如何用公式重塑输入数据集