You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何批量将ADLS RawData目录下50张表加载至Azure Databricks SQL仓库?

批量加载ADLS RawData目录下的表到Azure Databricks SQL仓库

当然有可行的批量实现方案,以下是几种实用的方法:

方法1:利用Databricks批量读取+注册表

通过遍历目录的逻辑,一次性处理所有子目录(假设每个子目录对应一张表),将数据加载后直接注册为SQL仓库可见的表:

# 定义ADLS RawData根路径
raw_data_path = "abfss://<container-name>@<storage-account-name>.dfs.core.windows.net/RawData/"

# 获取所有表的子目录(过滤掉文件,只保留目录)
table_dirs = [dir.path for dir in dbutils.fs.ls(raw_data_path) if dir.isDir()]

for table_dir in table_dirs:
    # 从路径提取表名(取最后一段目录名)
    table_name = table_dir.strip("/").split("/")[-1]
    # 读取目录下的数据(根据实际文件格式调整,比如csv、json、parquet)
    df = spark.read.format("parquet").load(table_dir)
    
    # 将数据写入并注册为SQL仓库的托管表/外部表
    df.write.mode("overwrite").saveAsTable(f"<catalog>.<schema>.{table_name}")

如果需要自动推断Schema并处理增量数据,也可以结合Auto Loader实现:

from pyspark.sql.functions import input_file_name

for table_dir in table_dirs:
    table_name = table_dir.strip("/").split("/")[-1]
    # Auto Loader自动发现文件、推断Schema
    df = spark.readStream \
        .format("cloudFiles") \
        .option("cloudFiles.format", "parquet") \
        .option("cloudFiles.schemaLocation", f"/tmp/schemas/{table_name}") \
        .load(table_dir)
    
    # 写入Delta表并注册到SQL仓库
    df.writeStream \
        .option("checkpointLocation", f"/tmp/checkpoints/{table_name}") \
        .toTable(f"<catalog>.<schema>.{table_name}")

方法2:动态生成SQL脚本批量创建外部表

如果数据是结构化存储(如Parquet、CSV),可以批量生成CREATE EXTERNAL TABLE语句并执行,直接将ADLS路径映射为SQL仓库的外部表:

raw_data_path = "abfss://<container-name>@<storage-account-name>.dfs.core.windows.net/RawData/"
table_dirs = [dir.path for dir in dbutils.fs.ls(raw_data_path) if dir.isDir()]
catalog = "<your-catalog>"
schema_name = "<your-schema>"

for table_dir in table_dirs:
    table_name = table_dir.strip("/").split("/")[-1]
    # 读取样本数据推断Schema,生成SQL字段定义
    sample_df = spark.read.format("parquet").load(table_dir).limit(1)
    schema_sql = ", ".join([f"`{col.name}` {col.dataType.simpleString()}" for col in sample_df.schema])
    
    # 构建创建外部表的SQL语句
    create_table_sql = f"""
    CREATE EXTERNAL TABLE IF NOT EXISTS {catalog}.{schema_name}.{table_name} (
        {schema_sql}
    )
    LOCATION '{table_dir}'
    FORMAT PARQUET
    """
    
    # 执行SQL完成表注册
    spark.sql(create_table_sql)

方法3:使用Delta Live Tables (DLT) 批量构建管线

如果需要附带数据质量校验或持续同步能力,DLT的声明式语法可以批量定义表,部署后自动完成加载与注册:

import dlt

raw_data_path = "abfss://<container-name>@<storage-account-name>.dfs.core.windows.net/RawData/"
table_dirs = [dir.path for dir in dbutils.fs.ls(raw_data_path) if dir.isDir()]

for table_dir in table_dirs:
    table_name = table_dir.strip("/").split("/")[-1]
    
    @dlt.table(name=table_name, catalog="<your-catalog>", schema="<your-schema>")
    def load_table():
        return spark.read.format("parquet").load(table_dir)

将这段代码作为DLT管线的代码文件,部署后DLT会自动加载所有表并同步到SQL仓库。


内容的提问来源于stack exchange,提问作者RK.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 21:22:35