【问题标题】:Unable to load the data from a MS SQL database from Microsoft Azure Databricks notebook无法从 Microsoft Azure Databricks 笔记本中加载 MS SQL 数据库中的数据
【发布时间】:2020-09-03 14:33:58
【问题描述】:

我想将数据从 MS SQL 数据库(未托管在 Azure 上)检索到 Microsoft Azure Databricks 笔记本。 以下是我所做的步骤:

  1. 进入天蓝色的门户,创建资源组
  2. 创建 Azure Databricks 服务(但我不使用“在您自己的虚拟网络 (VNet) 中部署 Azure Databricks 工作区”选项 → 也许我应该...)
  3. Azure Databricks 服务准备就绪后,我启动它并创建一个没有特定配置的集群
  4. 然后我用这个脚本创建一个笔记本(在之前的集群上运行)
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 上玩 notebook 并访问他的公司数据库)。比如我怎么Make sure that TCP connections to the port are not blocked by a firewall

如果您有一些想法或者您已经遇到过这个问题,请随时分享。 :)

如果您需要更多信息,请告诉我。

【问题讨论】:

  • 你能发布你的步骤吗?或者将您正在使用的代码放入响应中。删除您的特定凭据
  • 感谢您的回复。我已经添加了一些信息,希望对您有所帮助。

标签: sql-server apache-spark azure-databricks


【解决方案1】:

如果您已将 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)

使用 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()

【讨论】:

  • 如果我的回答对您有帮助,您可以接受它作为答案(单击答案旁边的复选标记,将其从灰色切换为已填充。)。这对其他社区成员可能是有益的。谢谢。
  • 对不起,我生病了,我会试试你推荐的。顺便谢谢你:)
  • 但正如我所说,数据库不是托管在 Auzre 上:/
猜你喜欢
  • 1970-01-01
  • 2019-03-03
  • 1970-01-01
  • 2016-02-07
  • 2022-07-15
  • 2019-06-23
  • 1970-01-01
  • 2020-09-23
  • 2020-03-15
相关资源
最近更新 更多