[英]Databricks JDBC Connections works but query fails
我正在嘗試通過 Azure Databricks 建立與 SQL 服務器數據庫的連接。 雖然有許多問題試圖緩解這里的問題,但我還沒有找到適合我的解決方案。
我能夠連接,至少看起來是因為 DataFrame (spark) object 從我的數據庫表中讀取了確切的模式,但在嘗試查看數據( display(df)
或df.show()
)時它會引發連接錯誤。
這是我的連接方式:
jdbcUrl = "jdbc:sqlserver://{}:{};database={}".format(jdbcHostname, jdbcPort, jdbcDatabase)
connectionProperties = {
'user': jdbcUsername,
'password': jdbcPassword,
"driver" : "com.microsoft.sqlserver.jdbc.SQLServerDriver"
}
pushdown_query = "(select * from persons where personid = 3040) Person"
df = spark.read.jdbc(url=jdbcUrl, table=pushdown_query, properties=connectionProperties)
display(df)
我可以看到 object df
並正確識別表中的所有 47 個字段(見下圖),但display(df)
引發以下錯誤:
SQLServerException: The TCP/IP connection to the host hornets-sql.westus.cloudapp.azure.com, 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.".
默認情況下,SQL 服務器啟用了防火牆,不允許從任意 IP 訪問。 在您的情況下,您可能在防火牆中允許驅動程序節點 - 這就是您獲得表模式的原因,但讀取本身發生在防火牆上可能未啟用的工作節點上,並且讀取失敗。
您可以嘗試通過以下方式解決:
最后兩項在 Databrick 的博客文章中得到了很好的介紹。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.