Note
此功能仅支持转换使用 HMS 和 Glue 联邦联合的外部表。
该 ALTER TABLE ... SET EXTERNAL 命令将外部表转换为Unity Catalog的外部表,保留表的历史和配置,包括名称、设置、权限和视图。 它支持 Databricks 运行时 17.3 及以上版本中的 Delta Lake、Parquet、ORC、Avro、JSON、CSV 和 TEXT 源表。
有关将外部表转换为托管表的详细信息,请参阅 使用 SQL 的外部表
有关将外部表转换为托管表的详细信息,请参阅 将外部或外来的 Delta Lake 表转换为 Unity Catalog 托管表
SET EXTERNAL 概述
使用 SET EXTERNAL 功能将外部表转换为 Azure Databricks 中的 Unity Catalog EXTERNAL 表。
SET EXTERNAL 的优势如下:
保留表历史记录
保留相同的表配置,包括相同的名称、设置、权限和视图。
Prerequisites
数据格式:外部表的数据格式必须是下列格式之一:
Delta
Parquet
ORC
Avro
JSON
CSV
TEXT
表类型:HMS 表类型必须是外部 HMS 表。 如果表是托管 HMS 表,该命令将失败。
运行时:Databricks Runtime 17.3 或更高版本
权限:
OWNER或MANAGE对表的权限,和CREATE对EXTERNAL LOCATION的权限
警告
不支持向源表和 Unity 目录进行并发写入。 你负责禁用对外部目录中源表的读取和写入,并确保工作负荷在执行转换之前已迁移到新目录。
Syntax
若要将 Unity Catalog 外部表转换为 Unity Catalog 外部的请运行以下命令:
ALTER TABLE source_table SET EXTERNAL [DRY RUN]
Parameters
source_table
Unity Catalog中的现有外部表。 外部表包含由外部目录管理的数据和元数据。 在转换之前,如果在外部目录中删除源表,则外部表也会在 Unity 目录中删除。 将表转换为外部表后,删除外部目录中的源表不会影响 Unity 目录外部表。
DRY RUN指定后,检查是否可以升级源表,而无需升级目标表。 如果可以升级表,则该命令返回
DRY_RUN_SUCCESS。
回退
若要回滚表迁移,请删除该表,然后在下一次目录同步中将其重新设定为外部数据库表。
DROP TABLE catalog.schema.my_external_table;
验证转换
您可以通过在目录浏览器中检查来确认您的外部数据表已转换为外部表。 在转换之前,表显示为 Foreign,转换后将显示为 External。
Note
运行 DESCRIBE EXTENDED 显示表类型在转换前后均为 EXTERNAL。 这是由于联合的工作原理,它模拟了在hive_metastore目录中运行此命令的行为。 若要准确验证转换,请使用目录资源管理器。
FAQ
是否可以在外部目录中创建表以及转换表?
是的,可以在外部目录中创建外部表或托管表。 行为取决于架构配置:
对于 Glue 或 eHMS 架构,或者 对于 Unity 目录中设置了托管位置的架构:如果运行
CREATE TABLE foreign_catalog.schema.table,这将创建 Unity 目录托管表或外部表。 该表未推送或同步到外部目录。对于来自内部 Hive 元存储连接的模式:如果尝试在外部模式中创建表,它仍然会创建一个外表,并在 Hive 内部架构
hive_metastore创建一个表。对于旧工作区 Hive 元存储:由于它是读取和写入联合的,因此如果在外部目录中创建表,它还会在内部 Hive 元存储中创建一个表。
如果我的外部表采用 SerDe 格式,该怎么办?
对于 AWS Glue 中的 Parquet SerDe 表,必须在转换为外部表之前修改表元数据。 以下 Python 脚本使用 AWS boto3 库更新必要的属性:
import boto3
import json
# Initialize boto3 Glue client with your AWS region
glue_client = boto3.client('glue', region_name='<your-aws-region>') # Example: 'us-west-2'
# Configure your table details
DATABASE_NAME = '<your-database-name>' # Example: 'my_database'
TABLE_NAME = '<your-table-name>' # Example: 'my_parquet_table'
SPARK_PROVIDER = 'PARQUET'
SPARK_PARTITION_PROVIDER = 'filesystem'
# Step 1: Get the current table definition
print(f"Retrieving current table definition for {DATABASE_NAME}.{TABLE_NAME}...")
response = glue_client.get_table(DatabaseName=DATABASE_NAME, Name=TABLE_NAME)
# Extract the table's current definition
table_definition = response['Table']
# Step 2: Verify if the table is a Parquet SerDe
serde_library = table_definition['StorageDescriptor']['SerdeInfo'].get('SerializationLibrary', '')
is_parquet_serde = serde_library == "org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe"
if not is_parquet_serde:
print(f"The table {TABLE_NAME} does not use a Parquet SerDe. Found: {serde_library}")
else:
print(f"Table {TABLE_NAME} is using a Parquet SerDe.")
# Step 3: Extract the S3 path dynamically from the Location field
s3_path = table_definition['StorageDescriptor']['Location']
print(f"S3 Path found: {s3_path}")
# Step 4: Modify the SerDe and table properties
# Modify SerDe parameters
if 'SerdeInfo' in table_definition['StorageDescriptor']:
if 'Parameters' not in table_definition['StorageDescriptor']['SerdeInfo']:
table_definition['StorageDescriptor']['SerdeInfo']['Parameters'] = {}
table_definition['StorageDescriptor']['SerdeInfo']['Parameters']['path'] = s3_path
# Modify table properties
if 'Parameters' not in table_definition:
table_definition['Parameters'] = {}
# Set both spark.sql.sources.provider and spark.sql.partitionProvider
table_definition['Parameters']['spark.sql.sources.provider'] = SPARK_PROVIDER
table_definition['Parameters']['spark.sql.partitionProvider'] = SPARK_PARTITION_PROVIDER
# Remove metadata fields that are not accepted by update_table API
table_definition.pop('CreateTime', None)
table_definition.pop('UpdateTime', None)
table_definition.pop('LastAccessTime', None)
table_definition.pop('Retention', None)
table_definition.pop("DatabaseName", None)
table_definition.pop('CreatedBy', None)
table_definition.pop('IsRegisteredWithLakeFormation', None)
table_definition.pop('CatalogId', None)
table_definition.pop('VersionId', None)
# Step 5: Update the table with the modified properties
print(f"Updating the table {TABLE_NAME} in Glue...")
response = glue_client.update_table(
DatabaseName=DATABASE_NAME, # Correct use of DatabaseName
TableInput=table_definition,
)
print(f"Table {TABLE_NAME} updated successfully!")
如果我的外部表是基于 DBFS,应该怎么处理?
在转换一个由 DBFS 支持的表时,我们将当前的 DBFS 路径映射存储为外部表的云路径位置。
可以在模式或目录级别进行转换吗?
可以逐一浏览架构中的表以进行单独转换,或使用 discoverx labs 项目将整个模式或目录一次性转换。
df = (dx.from_tables("prod.*.*")
.with_sql("ALTER TABLE {full_table_name} SET EXTERNAL;")
.apply()) # dry run with .explain()