我正在尝试.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=[''])
该方法 pandas.read_excel
不支持使用URLwasbs
或abfss
URL方案来访问文件。有关更多详细信息,请参阅此处
因此,如果要使用pandas访问文件,建议您创建一个sas令牌,并使用https
带有sas token的方案来访问文件或将文件下载为流,然后使用pandas进行读取。同时,您还将存储帐户安装为文件系统,然后按照@ CHEEKATLAPRADEEP-MSFT的说明访问文件。
例如
pdf=pd.read_excel('https://<account name>.dfs.core.windows.net/<file system>/<path>?<sas token>')
print(pdf)
安装软件包azure-storage-file-datalake
并xlrd
在数据块中使用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 。有关更多详细信息,请参阅此处和此处
例如
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()