同盟查詢 (Lakehouse 同盟)
適用於: Databricks SQL
Databricks Runtime 13.3 LTS 和更新版本,
Unity Catalog
查詢聯邦允許 Azure Databricks 不僅可以針對自身中繼存放區,還可以針對由其他 Azure Databricks 中繼存放區服務的資料,以及許多第三方資料庫管理系統(例如 PostgreSQL、MySQL和 Snowflake)執行查詢。
若要從另一個系統查詢數據,您必須:
- 建立 外部連線。 這會向 Unity 目錄註冊特定的同盟伺服器,並建立與其通訊的方法,例如所使用的 URL、埠和認證。
- 向 Unity Catalog 註冊由聯邦伺服器 提供的外國目錄
- 授與 使用者對外部目錄的存取權。 這可以在目錄、架構或數據表層級完成,就像對一般安全物件所做的一樣。
您現在可以跨各種地方和對外關係發出查詢。
外部連線
外部連線是 Unity Catalog 的一個可被保護的物件,用於識別外部伺服器。 在 CREATE CONNECTION中,您可以指定可存取伺服器的 URL。
您也必須提供選項,例如使用者名稱和密碼或其他已接受的驗證,Azure Databricks 將用來通訊。
國外目錄
若存在支援三層命名空間的外部連接(catalog/database.schema.table
),您可以使用 CREATE FOREIGN CATALOG 命令,向 Unity Catalog 註冊整個目錄。
Azure Databricks 會讓目錄的架構及其關係與外部來源保持同步。
範例
-- Create a postgresql connection
> CREATE CONNECTION postgresql_connection
TYPE POSTGRESQL
OPTIONS (
host 'qf-postgresql-demo.xxxxxx.us-west-2.rds.amazonaws.com',
port '5432',
user 'postgresql_user',
password 'password123');
-- Alternatively create a postgresql connection with secret scope
> CREATE CONNECTION postgresql_connection
TYPE POSTGRESQL
OPTIONS (
host 'qf-postgresql-demo.xxxxxx.us-west-2.rds.amazonaws.com',
port '5432',
user secret('secrets.r.us', 'postgresUser'),
password secret('secrets.r.us', 'postgresPassword'));
-- Expose the "postgresdb" database with schemas and tables postgresql_user can access.
> CREATE FOREIGN CATALOG postgresql_catalog
USING CONNECTION postgresql_connection
OPTIONS (database 'postgresdb');
-- Execute a query across tables in the above catalog, schema, and table.
> SELECT * FROM postgresql_catalog.a_schema.table1
UNION ALL
SELECT * FROM default.postgresql_schema.table2
UNION ALL
SELECT * FROM default.postgresql.mytable
UNION ALL
SELECT local_table;
...