温馨提示:本文翻译自stackoverflow.com,查看原文请点击:python 3.x - Reading Excel file from Azure Databricks
azure-data-lake-gen2 azure-databricks excel python-3.x

python 3.x - 从Azure Databricks读取Excel文件

发布于 2021-01-10 03:08:57

我正在尝试.xlsx从Azure Databricks准备Excel文件(),该文件位于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
被浏览
0
Jim Xu 2020-09-08 10:05

该方法 pandas.read_excel 不支持使用URLwasbsabfssURL方案来访问文件。有关更多详细信息,请参阅此处

因此,如果要使用pandas访问文件,建议您创建一个sas令牌,并使用https带有sas token的方案来访问文件或将文件下载为流,然后使用pandas进行读取。同时,您还将存储帐户安装为文件系统,然后按照@ CHEEKATLAPRADEEP-MSFT的说明访问文件。

例如

  • 使用SAS令牌访问
  1. 通过Azure门户创建SAS令牌 在此处输入图片说明

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

在此处输入图片说明

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

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文件。但是我们需要com.crealytics:spark-excel在我们的环境中添加jar 有关更多详细信息,请参阅此处此处

例如

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

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()

在此处输入图片说明