从Apache Spark Databricks读取Azure DB时,与主机端口1433的TCP/IP连接失败



我正试图从Azure SQLDB中读取,但不断收到错误消息

到主机的TCP/IP连接cp-retbihydr8config.database.windows.net数据库.windows.net,端口1433的失败

我设置了如下连接变量:

DBUser = 'techadmin'
DBPword = 'xxxxxxxx'
DBServer = 'xxxxxx.database.windows.net'
DBDatabase = 'xxxxxx'

从数据库中读取的代码如下:

def readFromDb(processId, query):
try:
jdbcDF = (spark.read
.format("jdbc")
.option("driver", "com.microsoft.sqlserver.jdbc.SQLServerDriver")
.option("url", f"jdbc:sqlserver://{DBServer}.database.windows.net;database={DBDatabase}")
.option("user", DBUser)
.option("query", query)
.option("password", DBPword)
.load()    
)
return jdbcDF
except Exception as e:
writeToLogs(processId,LogType.Error, EventType.FailReadFromDb, LogMessage.FailReadFromDb, errorType = ErrorType.FailReadFromDb)
raise Error(f"{LogMessage.FailReadFromDb.value} ERROR: {e}")
except:
writeToLogs(processId,LogType.FailReadFromDb, EventType.FailReadFromDb, LogMessage.FailReadFromDb, errorType = ErrorType.FailReadFromDb)
raise Error(f"{LogMessage.FailReadFromDb.value}")

当我用以下代码测试功能时:

readFromDb(1,5)

我得到完整的错误信息:

com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host xxxxxxxx.database.windows.net.database.windows.net, port 1433 has failed. Error: "xxxxxxxxxx.database.windows.net.database.windows.net. 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."

我100%知道问题不在于ip地址连接,因为我可以使用以下代码成功写入数据库:

jdbcUrl = "jdbc:sqlserver://xxxxxxxxx.windows.net:1433;database=xxxxxxxxxx;user=techadmin@cp-retbihydr8config;password=xxxxxxxxx;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;"

df.write.mode("overwrite") 
.format("jdbc") 
.option("url", jdbcUrl) 
.option("dbtable", 'UpdatedProducts') 
.save()

如果我使用,我也会收到错误消息

jdbcUrl = f"jdbc:sqlserver://{DBServer}.database.windows.net:1433;database={DBDatabase};user={DBUser};password={DBPword};encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;"

所以,我不知道为什么我要从数据库中读取函数(readFromDb(时出错?或者当我使用时:

jdbcUrl = f"jdbc:sqlserver://{DBServer}.database.windows.net:1433;database={DBDatabase};user={DBUser};password={DBPword};encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;"

问题是我传入了错误的变量

我路过:

DBServer = 'xxxxxx.database.windows.net'

而我应该刚刚通过:

DBServer = 'xxxxxx'

默认情况下,Azure上的SQL server会阻止所有传入连接,直到您明确允许它们。因此,您需要允许从防火墙上的Databricks集群访问SQL服务器。另一种可能性是将服务端点用于SQL server。

相关内容

  • 没有找到相关文章

最新更新