外部表

适用于:check marked yes Databricks SQL check marked yes Databricks Runtime

Unity Catalog 和内置 Azure Databricks Hive 元存储使用托管表的默认位置。 Unity Catalog 引入了几个新的安全对象来授予云对象存储数据权限。

外部表

外部表是使用 LOCATION 子句引用外部存储路径的

存储路径应包含在你已被授予访问权限的现有外部位置中。

或者,你可以引用已被授予其访问权限的存储凭据

使用外部表可以抽象出被授予外部表访问权限的用户的存储路径、外部位置和存储凭据。

警告

如果在工作区级别的 Hive 元存储中注册了架构(数据库),则使用 CASCADE 选项删除该架构会导致递归删除该架构位置中的所有文件,而不管表类型(托管或外部)如何。

如果架构已注册到 Unity Catalog 元存储,Unity Catalog 托管表的文件将被递归删除。 但是,不会删除外部表的文件。 必须直接使用云存储提供商来管理这些文件。

因此,为避免数据意外丢失,永远不应将 Hive 元存储中的架构注册到具有现有数据的位置。 也不应在由 Hive 元存储架构管理的位置或包含 Unity Catalog 托管表的位置创建新的外部表。

关系的图形表示

下图描述了它们之间的关系:

  • 存储凭据
  • 外部位置
  • 外部表
  • 存储路径
  • IAM 实体
  • Azure 服务帐户

External location ER diagram

示例

-- `finance` can create an external table over specific object within the `finance_loc` location
> CREATE TABLE sec_filings LOCATION 'abfss://container@storageaccount.dfs.core.windows.net/depts/finance/sec_filings';

-- Create or replace an external table from a query
> CREATE OR REPLACE TABLE sec_filings
  LOCATION 'abfss://container@storageaccount.dfs.core.windows.net/depts/finance/sec_filings'
  AS (SELECT * FROM current_filings);

-- Cannot list files under an external table without permissions on it
> LIST `abfss://container@storageaccount.dfs.core.windows.net/depts/finance/sec_filings`
  Error
> LIST `abfss://container@storageaccount.dfs.core.windows.net/depts/finance/sec_filings/_delta_log`
  Error

-- Grant access to sec_filings to all employees
> GRANT SELECT ON TABLE sec_filings TO employee;

-- Any member of the `employee` group can securely read sec_filings
> SELECT count(1) FROM sec_filings;
 20

-- Any member of the `employee` group can list files under the sec_filings table
> LIST `abfss://container@storageaccount.dfs.core.windows.net/depts/finance/sec_filings`
  _delta_log
> LIST `abfss://container@storageaccount.dfs.core.windows.net/depts/finance/sec_filings/_delta_log`
  00000.json