34、Spark实现读取XLS文件

需求背景: 有一些xls大文件数据。使用spark-excel(spark-excel)来读取时,文件太大会oom;工具提供的流式读取参数:maxRowsInMemory 也只支持xlsx类型文件。搜索了poi流式读取xls的方案,HSSFEventFactory提供了HSSFListener进行逐条处理数据。所以编写了spark读取xls的简易source。代码如下:

spark.read.format("xls").option("path", logPath).load()能够跑通。但是对应xls大文件还是会oom。具体了解后得到原因:SSTRecord存储了整个excel中所有字符串去重后结果,LabelSSTRecord只是存储了该字符串值在SSTRecord中的索引位置。所以在逐条处理xls文件数据的时候遇到SSTRecord还是会oom。

结论:没实现成功,失败;找不到其它实习方案,只能python脚本提前将xls文件转为csv。

scala 复制代码
package cn.keytop.source.xls

import org.apache.hadoop.fs.{FileSystem, Path}
import org.apache.poi.hssf.eventusermodel._
import org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord
import org.apache.poi.hssf.record._
import org.apache.poi.hssf.usermodel.HSSFDataFormatter
import org.apache.poi.poifs.filesystem.POIFSFileSystem
import org.apache.spark.sql.types._
import org.apache.spark.sql.{DataFrame, Row, SQLContext}

import scala.collection.mutable.ArrayBuffer

/**
 * @author: 王建成
 * @since: 2025/4/18 13:46
 * @description: coding需求和地址 编写一个spark source plugin来读取xls大文件数据
 */              
class XLSReader {

  def read(pathStr: String, sqlContext: SQLContext): org.apache.spark.sql.DataFrame = {
    val hadoopConf = sqlContext.sparkContext.hadoopConfiguration
    val fsPath = new Path(pathStr)
    val fs = fsPath.getFileSystem(hadoopConf)

    // 获取所有 .xls 文件
    val allFiles: Array[Path] = {
      if (fs.isDirectory(fsPath)) {
        fs.listStatus(fsPath)
          .filter(f => f.isFile && f.getPath.getName.toLowerCase.endsWith(".xls"))
          .map(_.getPath)
      } else {
        Array(fsPath)
      }
    }

    // 每个文件读取出一个 DataFrame,然后合并
    val dfs = allFiles.map { filePath =>
      println(s"Reading XLS file: $filePath")
      readSingleXLS(filePath, fs, sqlContext)
    }

    dfs.reduceOption(_.union(_)).getOrElse {
      // 如果目录下没有任何 xls 文件
      sqlContext.createDataFrame(sqlContext.sparkContext.emptyRDD[Row], StructType(Nil))
    }
  }

  private def readSingleXLS(path: Path, fs: FileSystem, sqlContext: SQLContext): DataFrame = {
    val inputStream = fs.open(path)
    val fsPOI = new POIFSFileSystem(inputStream)

    val rowsBuffer = ArrayBuffer[ArrayBuffer[String]]()
    var sstRecord: SSTRecord = null
    var headers: ArrayBuffer[String] = ArrayBuffer()
    var currentRow = ArrayBuffer[String]()
    var currentRowNum = -1

    val listener = new HSSFListener {
      val formatter = new HSSFDataFormatter()

      override def processRecord(record: Record): Unit = {
        record match {
          case sst: SSTRecord =>
            sstRecord = sst
          case label: LabelSSTRecord =>
            val value = sstRecord.getString(label.getSSTIndex).toString
            ensureSize(currentRow, label.getColumn + 1, "")
            currentRow(label.getColumn) = value
            currentRowNum = label.getRow
          case number: NumberRecord =>
            val value = number.getValue.toString
            ensureSize(currentRow, number.getColumn + 1, "")
            currentRow(number.getColumn) = value
            currentRowNum = number.getRow
          case _: LastCellOfRowDummyRecord =>
            if (currentRow.nonEmpty) {
              if (currentRowNum == 0 && headers.isEmpty) {
                headers = currentRow.clone()
              } else {
                rowsBuffer += currentRow.clone()
              }
            }
            currentRow.clear()
            currentRowNum = -1
          case _ =>
        }
      }

      def ensureSize(buffer: ArrayBuffer[String], size: Int, default: String): Unit = {
        while (buffer.size < size) {
          buffer += default
        }
      }
    }

    val factory = new HSSFEventFactory()
    val request = new HSSFRequest()
    val listener1 = new MissingRecordAwareHSSFListener(listener)
    val listener2 = new FormatTrackingHSSFListener(listener1)
    request.addListenerForAllRecords(listener2)
    factory.processWorkbookEvents(request, fsPOI)

    val schema = StructType(headers.map(name => StructField(name, StringType, nullable = true)))
    val rows = rowsBuffer.map(Row.fromSeq)
    sqlContext.createDataFrame(sqlContext.sparkContext.parallelize(rows), schema)
  }

}
scala 复制代码
package cn.keytop.source.xls

import org.apache.spark.rdd.RDD
import org.apache.spark.sql.sources.{BaseRelation, DataSourceRegister, RelationProvider, TableScan}
import org.apache.spark.sql.types.StructType
import org.apache.spark.sql.{Row, SQLContext}

import java.io.Serializable
/**
 * @author: 王建成
 * @since: 2025/4/18 13:46
 * @description: coding需求和地址 编写一个spark source plugin来读取xls大文件数据
 */
class DefaultSource extends RelationProvider with DataSourceRegister with Serializable{

  override def shortName(): String = "xls"

  override def createRelation(sqlContext: SQLContext, parameters: Map[String, String]): BaseRelation = {
    val path = parameters.getOrElse("path", throw new IllegalArgumentException("Missing path"))
    val reader = new XLSReader()
    val df = reader.read(path, sqlContext)

    new BaseRelation with TableScan {
      override def sqlContext: SQLContext = sqlContext
      override def schema: StructType = df.schema
      override def buildScan(): RDD[Row] = df.rdd
    }
  }
}
相关推荐
SelectDB8 小时前
易车 × Apache Doris:构建湖仓一体新架构,加速 AI 业务融合实践
大数据·agent·mcp
武子康15 小时前
大数据-241 离线数仓 - 实战:电商核心交易数据模型与 MySQL 源表设计(订单/商品/品类/店铺/支付)
大数据·后端·mysql
茶杯梦轩15 小时前
从零起步学习RabbitMQ || 第三章:RabbitMQ的生产者、Broker、消费者如何保证消息不丢失(可靠性)详解
分布式·后端·面试
IvanCodes15 小时前
一、消息队列理论基础与Kafka架构价值解析
大数据·后端·kafka
武子康2 天前
大数据-240 离线数仓 - 广告业务 Hive ADS 实战:DataX 将 HDFS 分区表导出到 MySQL
大数据·后端·apache hive
回家路上绕了弯2 天前
深入解析Agent Subagent架构:原理、协同逻辑与实战落地指南
分布式·后端
字节跳动数据平台2 天前
5000 字技术向拆解 | 火山引擎多模态数据湖如何释放模思智能的算法生产力
大数据
武子康3 天前
大数据-239 离线数仓 - 广告业务实战:Flume 导入日志到 HDFS,并完成 Hive ODS/DWD 分层加载
大数据·后端·apache hive
字节跳动数据平台4 天前
代码量减少 70%、GPU 利用率达 95%:火山引擎多模态数据湖如何释放模思智能的算法生产力
大数据
得物技术4 天前
深入剖析Spark UI界面:参数与界面详解|得物技术
大数据·后端·spark