从 Azure Databricks 读取 Excel 文件

新手上路,请多包涵

我正在尝试从 Azure Databricks 准备好 Excel 文件( .xlsx ),文件位于 ADLS Gen 2 中。

例子:

 srcPathforParquet = "wasbs://hyxxxx@xxxxdatalakedev.blob.core.windows.net//1_Raw//abc.parquet"
srcPathforExcel = "wasbs://hyxxxx@xxxxdatalakedev.blob.core.windows.net//1_Raw//src.xlsx"

从路径中读取镶木地板文件工作正常。

 srcparquetDF = spark.read.parquet(srcPathforParquet )

从路径中读取 excel 文件抛出错误: 没有这样的文件或目录

srcexcelDF = pd.read_excel(srcPathforExcel , keep_default_na=False, na_values=[''])

原文由 Sreedhar 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.5k
2 个回答

方法 pandas.read_excel 不支持使用 wasbsabfss 方案URL访问文件。详情请参考 这里

因此,如果您想使用 pandas 访问文件,我建议您创建一个 sas 令牌并使用 https 带有 sas 令牌的方案来访问该文件或将文件下载为流,然后使用 pandas 读取它。同时,您还可以将存储帐户挂载为文件系统,然后按照@CHEEKATLAPRADEEP-MSFT 所说的那样访问文件。

例如

  • 使用 sas 令牌访问
  1. 通过 Azure 门户创建 sas 令牌 在此处输入图像描述

  2. 代码

pdf=pd.read_excel('https://<account name>.dfs.core.windows.net/<file system>/<path>?<sas token>')
print(pdf)

在此处输入图像描述

  • 将文件下载为流并读取文件
  1. 在数据块中使用 pip 安装包 azure-storage-file-datalakexlrd

  2. 代码

import io

import pandas as pd
from azure.storage.filedatalake import BlobServiceClient
from azure.storage.filedatalake import DataLakeServiceClient

blob_service_client = DataLakeServiceClient(account_url='https://<account name>.dfs.core.windows.net/', credential='<account key>')

file_client = blob_service_client.get_file_client(file_system='test', file_path='data/sample.xlsx')
with io.BytesIO() as f:
  downloader =file_client.download_file()
  b=downloader.readinto(f)
  print(b)
  df=pd.read_excel(f)
  print(df)

在此处输入图像描述

此外,我们还可以使用 pyspark 读取 excel 文件。但是我们需要在我们的环境中添加 jar com.crealytics:spark-excel 。有关更多详细信息,请参阅 此处此处

例如

  1. 添加包 com.crealytics:spark-excel_2.12:0.13.1 通过 Maven。此外, 请注意,如果您使用 scala 2.11,请添加包 com.crealytics:spark-excel_2.11:0.13.1

  2. 代码

spark._jsc.hadoopConfiguration().set("fs.azure.account.key.<account name>.dfs.core.windows.net",'<account key>')

print("use spark")
df=sqlContext.read.format("com.crealytics.spark.excel") \
        .option("header", "true") \
        .load('abfss://test@testadls05.dfs.core.windows.net/data/sample.xlsx')

df.show()

在此处输入图像描述

原文由 Jim Xu 发布,翻译遵循 CC BY-SA 4.0 许可协议

根据我的 repro,无法使用存储帐户访问密钥直接访问从 ADLS gen2 读取 excel 文件。当我尝试通过 ADLS gen2 URL 读取 excel 文件时,我收到与 FileNotFoundError: [Errno 2] No such file or directory: 'abfss://filesystem@chepragen2.dfs.core.windows.net/flightdata/drivers.xlsx' 相同的错误消息。

在此处输入图像描述

从 Azure Databricks 读取 Excel 文件 ( .xlsx ) 的步骤,文件位于 ADLS Gen 2 中:

第一步: 挂载 ADLS Gen2 存储账户。

 configs = {"fs.azure.account.auth.type": "OAuth",
           "fs.azure.account.oauth.provider.type": "org.apache.hadoop.fs.azurebfs.oauth2.ClientCredsTokenProvider",
           "fs.azure.account.oauth2.client.id": "<application-id>",
           "fs.azure.account.oauth2.client.secret": dbutils.secrets.get(scope="<scope-name>",key="<service-credential-key-name>"),
           "fs.azure.account.oauth2.client.endpoint": "https://login.microsoftonline.com/<directory-id>/oauth2/token"}

# Optionally, you can add <directory-name> to the source URI of your mount point.
dbutils.fs.mount(
  source = "abfss://<file-system-name>@<storage-account-name>.dfs.core.windows.net/",
  mount_point = "/mnt/<mount-name>",
  extra_configs = configs)

Step2: 使用挂载路径读取excel文件。

在此处输入图像描述

参考: Azure Databricks - Azure Data Lake Storage Gen2

原文由 CHEEKATLAPRADEEP-MSFT 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进