无法读取 Azure Databricks 上的 .xlsx 文件
Can't read .xlsx file on Azure Databricks
我在 Azure databricks notebooks 上使用 Python,我在读取 excel 文件并将其放入 spark 数据帧时遇到问题。
我看到有相同问题的题目,但他们似乎不适合我。
我尝试了以下解决方案:
https://sauget-ch.fr/2019/06/databricks-charger-des-fichiers-excel-at-scale/
我确实添加了凭据以访问我在 Azure Data Lake 上的文件。
安装我需要的所有库后,我正在执行以下代码:
import xlrd
import azure.datalake.store
filePathBsp = projectFullPath + "BalanceShipmentPlan_20190724_19h31m37s.xlsx";
bspDf = pd.read_excel(AzureDLFileSystem.open(filePathBsp))
在那里,我使用:
"AzureDLFileSystem.open"
获取 Azure Data Lake 中的文件,因为:
"pd.read_excel"
不让我把我的文件送到湖上。
问题是,它给了我这个错误:
TypeError: open() missing 1 required positional argument: 'path'
我确定我可以访问此文件,因为当我尝试时:
spark.read.csv(filePathBsp)
他可以找到我的文件。
有什么想法吗?
好的,经过长时间的研究,我终于找到了解决方案。
在这里!
首先,您必须在集群中导入库 "spark-Excel"。
这是该库的页面:https://github.com/crealytics/spark-excel
您还需要库 "spark_hadoopOffice",否则稍后会出现以下异常:
java.io.IOException: org/apache/commons/collections4/IteratorUtils
下载库时请注意集群中的 Scala 版本,这很重要。
然后,您必须以这种方式安装 Azure Data Lake Storage (ADLS) 的凭据:
# Mount point
udbRoot = "****"
configs = {
"dfs.adls.oauth2.access.token.provider.type": "ClientCredential",
"dfs.adls.oauth2.client.id": "****",
"dfs.adls.oauth2.credential": "****",
"dfs.adls.oauth2.refresh.url": "https://login.microsoftonline.com/****/oauth2/token"
}
# unmount
#dbutils.fs.unmount(udbRoot)
# Mounting
dbutils.fs.mount(
source = "adl://****",
mount_point = udbRoot,
extra_configs = configs
)
您只需执行一次挂载命令。
然后,您可以执行以下代码行:
testDf = spark.read.format("com.crealytics.spark.excel").option("useHeader", True).load(fileTest)
display(testDf)
给你!您有来自 Azure Data Lake Storage 中 Excel 文件的 Spark Dataframe!
它对我有用,希望它能帮助其他人。
我在 Azure databricks notebooks 上使用 Python,我在读取 excel 文件并将其放入 spark 数据帧时遇到问题。
我看到有相同问题的题目,但他们似乎不适合我。
我尝试了以下解决方案:
https://sauget-ch.fr/2019/06/databricks-charger-des-fichiers-excel-at-scale/
我确实添加了凭据以访问我在 Azure Data Lake 上的文件。
安装我需要的所有库后,我正在执行以下代码:
import xlrd
import azure.datalake.store
filePathBsp = projectFullPath + "BalanceShipmentPlan_20190724_19h31m37s.xlsx";
bspDf = pd.read_excel(AzureDLFileSystem.open(filePathBsp))
在那里,我使用:
"AzureDLFileSystem.open"
获取 Azure Data Lake 中的文件,因为:
"pd.read_excel"
不让我把我的文件送到湖上。
问题是,它给了我这个错误:
TypeError: open() missing 1 required positional argument: 'path'
我确定我可以访问此文件,因为当我尝试时:
spark.read.csv(filePathBsp)
他可以找到我的文件。
有什么想法吗?
好的,经过长时间的研究,我终于找到了解决方案。
在这里!
首先,您必须在集群中导入库 "spark-Excel"。 这是该库的页面:https://github.com/crealytics/spark-excel
您还需要库 "spark_hadoopOffice",否则稍后会出现以下异常:
java.io.IOException: org/apache/commons/collections4/IteratorUtils
下载库时请注意集群中的 Scala 版本,这很重要。
然后,您必须以这种方式安装 Azure Data Lake Storage (ADLS) 的凭据:
# Mount point
udbRoot = "****"
configs = {
"dfs.adls.oauth2.access.token.provider.type": "ClientCredential",
"dfs.adls.oauth2.client.id": "****",
"dfs.adls.oauth2.credential": "****",
"dfs.adls.oauth2.refresh.url": "https://login.microsoftonline.com/****/oauth2/token"
}
# unmount
#dbutils.fs.unmount(udbRoot)
# Mounting
dbutils.fs.mount(
source = "adl://****",
mount_point = udbRoot,
extra_configs = configs
)
您只需执行一次挂载命令。
然后,您可以执行以下代码行:
testDf = spark.read.format("com.crealytics.spark.excel").option("useHeader", True).load(fileTest)
display(testDf)
给你!您有来自 Azure Data Lake Storage 中 Excel 文件的 Spark Dataframe!
它对我有用,希望它能帮助其他人。