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

求助:使用Python在Azure Databricks中读取QVD文件的详细方法

在Azure Databricks中读取QVD文件到DataFrame的Python实现

以下是完整的操作步骤,包含Azure Data Lake Storage (ADLS) 访问密钥认证、qvd_reader库的使用,以及将QVD数据转换为Spark DataFrame的细节:

1. ADLS Gen2 访问密钥认证

通过配置Spark参数,使用存储账户访问密钥实现对ADLS的读取权限:

# 替换为你的实际存储信息
storage_account_name = "your_storage_account_name"
storage_account_key = "your_storage_access_key"
container_name = "your_container_name"

# 配置Spark访问ADLS
spark.conf.set(
    f"fs.azure.account.key.{storage_account_name}.dfs.core.windows.net",
    storage_account_key
)

配置完成后,可通过abfs://{container_name}@{storage_account_name}.dfs.core.windows.net/路径访问ADLS内的文件。

2. 安装qvd_reader库

在Databricks Notebook中执行以下命令安装依赖(只需运行一次):

%pip install qvd-reader

3. 读取QVD文件并转换为Spark DataFrame

qvd_reader仅支持读取本地文件系统的QVD文件,因此需要先将ADLS中的文件复制到Databricks本地临时目录,再进行读取:

3.1 复制ADLS中的QVD文件到本地

# ADLS中的QVD文件路径
adls_qvd_path = f"abfs://{container_name}@{storage_account_name}.dfs.core.windows.net/path/to/your/data.qvd"
# Databricks本地临时存储路径
local_qvd_path = "/tmp/data.qvd"

# 执行文件复制
dbutils.fs.cp(adls_qvd_path, f"file:{local_qvd_path}")

3.2 使用qvd_reader读取并转换为DataFrame

from qvd_reader import read_qvd

# 读取QVD文件为pandas DataFrame
pandas_df = read_qvd(local_qvd_path)

# 转换为Spark DataFrame
spark_df = spark.createDataFrame(pandas_df)

# 查看数据结构和前5行数据
spark_df.printSchema()
spark_df.show(5)

完整示例代码

# 1. 配置ADLS访问权限
storage_account_name = "your_storage_account"
storage_account_key = "your_access_key"
container_name = "your_container"

spark.conf.set(
    f"fs.azure.account.key.{storage_account_name}.dfs.core.windows.net",
    storage_account_key
)

# 2. 安装qvd_reader依赖
%pip install qvd-reader

# 3. 复制ADLS中的QVD文件到本地
adls_qvd_path = f"abfs://{container_name}@{storage_account_name}.dfs.core.windows.net/sample.qvd"
local_qvd_path = "/tmp/sample.qvd"
dbutils.fs.cp(adls_qvd_path, f"file:{local_qvd_path}")

# 4. 读取并转换为Spark DataFrame
from qvd_reader import read_qvd

pandas_df = read_qvd(local_qvd_path)
spark_df = spark.createDataFrame(pandas_df)

# 验证结果
spark_df.printSchema()
spark_df.show(5)

注意事项

  • 确保你的存储访问密钥拥有ADLS容器的读取权限
  • 处理大体积QVD文件时,建议选择内存配置更高的Databricks集群
  • 若不想复制文件到本地,可结合fsspec库直接读取ABFS路径,但需要额外配置Azure文件系统适配器,对新手而言,本地复制的方式更易上手

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 20:15:42