重要
旧查询联合文档已停用,可能不会更新。 此内容中提到的产品、服务或技术未经 Databricks 正式认可或测试。 请参见什么是 Lakehouse 联盟?
此示例使用其 JDBC 驱动程序查询 MySQL。 有关读取、写入、配置并行度和查询下推的更多详细信息,请参阅 使用 JDBC 的查询数据库。
重要
本文所述的配置为试验性配置。 试验性功能按原样提供,Databricks 不会通过客户技术支持为它提供支持。 为了获得完整的查询联邦支持,您应该改为使用 Lakehouse Federation,这使 Azure Databricks 的用户能够利用 Unity Catalog 语法和数据治理工具。
使用 JDBC
Python语言
driver = "com.mysql.cj.jdbc.Driver"
database_host = "<database-host-url>"
database_port = "3306" # update if you use a non-default port
database_name = "<database-name>"
table = "<table-name>"
user = "<username>"
password = "<password>"
url = f"jdbc:mysql://{database_host}:{database_port}/{database_name}"
remote_table = (spark.read
.format("jdbc")
.option("driver", driver)
.option("url", url)
.option("dbtable", table)
.option("user", user)
.option("password", password)
.load()
)
Scala(编程语言)
val driver = "com.mysql.cj.jdbc.Driver"
val database_host = "<database-host-url>"
val database_port = "3306" # update if you use a non-default port
val database_name = "<database-name>"
val table = "<table-name>"
val user = "<username>"
val password = "<password>"
val url = s"jdbc:mysql://${database_host}:${database_port}/${database_name}"
val remote_table = spark.read
.format("jdbc")
.option("driver", driver)
.option("url", url)
.option("dbtable", table)
.option("user", user)
.option("password", password)
.load()
在 Databricks Runtime 中使用 MySQL 连接器
使用 Databricks Runtime 11.3 LTS 及更高版本,可以使用命名连接器查询 MySQL。 请参阅以下示例:
Python语言
remote_table = (spark.read
.format("mysql")
.option("dbtable", "table_name")
.option("host", "database_hostname")
.option("port", "3306") # Optional - will use default port 3306 if not specified.
.option("database", "database_name")
.option("user", "username")
.option("password", "password")
.load()
)
SQL
DROP TABLE IF EXISTS mysql_table;
CREATE TABLE mysql_table
USING mysql
OPTIONS (
dbtable '<table-name>',
host '<database-host-url>',
port '3306', /* Optional - will use default port 3306 if not specified. */
database '<database-name>',
user '<username>',
password '<password>'
);
SELECT * from mysql_table;
Scala(编程语言)
val remote_table = spark.read
.format("mysql")
.option("dbtable", "table_name")
.option("host", "database_hostname")
.option("port", "3306") # Optional - will use default port 3306 if not specified.
.option("database", "database_name")
.option("user", "username")
.option("password", "password")
.load()