【问题标题】:Read multiple excel files from azure blob storage in PySpark efficiently有效地从 PySpark 中的 azure blob 存储中读取多个 excel 文件
【发布时间】:2021-04-25 07:28:38
【问题描述】:

我正在使用以下 pyspark 脚本从数据块中的 azure blob 存储中读取多个 excel 文件

schema1 = StructType([
 StructField("c1", StringType(), True)
,StructField("c2", StringType(), True)
...
,StructField("c19", StringType(), True)
,StructField("c20", StringType(), True)])

df_full = None
objects = dbutils.fs.ls(mount_pt + "/" + blob)

for x in objects:   # reading each file
  value = list(x)
  file_name = value[1]
  sample_df=spark.read.format("com.crealytics.spark.excel").schema(schema1).option("header", "false").option("dataAddress", "0!A3").load(mount_pt + "/" + blob + "/" + file_name)
  if sample_df.count() == 1: continue #skip empty files
  if df_full is None: #first non empty file
      df_full = sample_df.drop_duplicates()
  else:
      df_full  = df_full.union(sample_df).drop_duplicates()

我正在计算每个 sample_df,如果它不包含任何行,则跳过 union。但是,执行上面的代码需要 2 个多小时(甚至更多,因为我不得不在两者之间停止执行)。有 appx 450 个文件,其行数从 0 到 65K appx 不等。大约 60% 的 Excel 文件是空的。

即使我读取一个包含大约 30K 行的文件,显示前 1000 条记录也需要大约 2 分钟。

是否有任何其他方式可以让我更快地读取数据并将其保存在单个数据帧中,或者可以通过任何方式优化现有代码以更快地读取数据。

谢谢!

【问题讨论】:

  • 包含 excel 文件的文件夹总大小约为 2.4GB

标签: excel pyspark databricks pyspark-dataframes


【解决方案1】:

您可以将所有工作表转换为 CSV 文件,可以使用 pandas 自动执行此操作,以便我们可以同时快速读取所有文件。无需循环执行,也可以跳过文件名。

【讨论】:

  • 如果excel某列有逗号分隔的数据会失败。
猜你喜欢
  • 2022-01-19
  • 1970-01-01
  • 2018-11-04
  • 2012-06-16
  • 1970-01-01
  • 2019-07-29
  • 1970-01-01
  • 2021-01-31
  • 2021-04-01
相关资源
最近更新 更多