|
| 1 | +/* |
| 2 | + * Copyright 2022 Martin Mauch (@nightscape) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.crealytics.spark.excel.v2 |
| 18 | + |
| 19 | +import org.apache.hadoop.conf.Configuration |
| 20 | +import org.apache.spark.broadcast.Broadcast |
| 21 | +import org.apache.spark.sql.catalyst.{InternalRow, FileSourceOptions} |
| 22 | +import org.apache.spark.sql.connector.read.PartitionReader |
| 23 | +import org.apache.spark.sql.execution.datasources.PartitionedFile |
| 24 | +import org.apache.spark.sql.execution.datasources.v2._ |
| 25 | +import org.apache.spark.sql.internal.SQLConf |
| 26 | +import org.apache.spark.sql.sources.Filter |
| 27 | +import org.apache.spark.sql.types.StructType |
| 28 | +import org.apache.spark.util.SerializableConfiguration |
| 29 | + |
| 30 | +import java.net.URI |
| 31 | +import scala.util.control.NonFatal |
| 32 | + |
| 33 | +/** A factory used to create Excel readers. |
| 34 | + * |
| 35 | + * @param sqlConf |
| 36 | + * SQL configuration. |
| 37 | + * @param broadcastedConf |
| 38 | + * Broadcasted serializable Hadoop Configuration. |
| 39 | + * @param dataSchema |
| 40 | + * Schema of Excel files. |
| 41 | + * @param readDataSchema |
| 42 | + * Required data schema in the batch scan. |
| 43 | + * @param partitionSchema |
| 44 | + * Schema of partitions. |
| 45 | + * @param parsedOptions |
| 46 | + * Options for parsing Excel files. |
| 47 | + */ |
| 48 | +case class ExcelPartitionReaderFactory( |
| 49 | + sqlConf: SQLConf, |
| 50 | + broadcastedConf: Broadcast[SerializableConfiguration], |
| 51 | + dataSchema: StructType, |
| 52 | + readDataSchema: StructType, |
| 53 | + partitionSchema: StructType, |
| 54 | + parsedOptions: ExcelOptions, |
| 55 | + filters: Seq[Filter] |
| 56 | +) extends FilePartitionReaderFactory { |
| 57 | + protected def options: FileSourceOptions = new FileSourceOptions(Map( |
| 58 | + FileSourceOptions.IGNORE_CORRUPT_FILES -> "true", |
| 59 | + FileSourceOptions.IGNORE_MISSING_FILES -> "true" |
| 60 | + )) |
| 61 | + override def buildReader(file: PartitionedFile): PartitionReader[InternalRow] = { |
| 62 | + val conf = broadcastedConf.value.value |
| 63 | + val actualDataSchema = |
| 64 | + StructType(dataSchema.filterNot(_.name == parsedOptions.columnNameOfCorruptRecord)) |
| 65 | + val actualReadDataSchema = |
| 66 | + StructType(readDataSchema.filterNot(_.name == parsedOptions.columnNameOfCorruptRecord)) |
| 67 | + val parser = new ExcelParser(actualDataSchema, actualReadDataSchema, parsedOptions, filters) |
| 68 | + val headerChecker = |
| 69 | + new ExcelHeaderChecker(actualReadDataSchema, parsedOptions, source = s"Excel file: ${file.filePath}") |
| 70 | + val iter = readFile(conf, file, parser, headerChecker, readDataSchema) |
| 71 | + val partitionReader = new SparkExcelPartitionReaderFromIterator(iter) |
| 72 | + new PartitionReaderWithPartitionValues(partitionReader, readDataSchema, partitionSchema, file.partitionValues) |
| 73 | + } |
| 74 | + |
| 75 | + private def readFile( |
| 76 | + conf: Configuration, |
| 77 | + file: PartitionedFile, |
| 78 | + parser: ExcelParser, |
| 79 | + headerChecker: ExcelHeaderChecker, |
| 80 | + requiredSchema: StructType |
| 81 | + ): SheetData[InternalRow] = { |
| 82 | + val excelHelper = ExcelHelper(parsedOptions) |
| 83 | + val sheetData = excelHelper.getSheetData(conf, URI.create(file.filePath.toString)) |
| 84 | + try { |
| 85 | + SheetData( |
| 86 | + ExcelParser.parseIterator(sheetData.rowIterator, parser, headerChecker, requiredSchema), |
| 87 | + sheetData.resourcesToClose |
| 88 | + ) |
| 89 | + } catch { |
| 90 | + case NonFatal(t) => { |
| 91 | + sheetData.close() |
| 92 | + throw t |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | +} |
| 98 | + |
| 99 | +private class SparkExcelPartitionReaderFromIterator(sheetData: SheetData[InternalRow]) |
| 100 | + extends PartitionReaderFromIterator[InternalRow](sheetData.rowIterator) { |
| 101 | + override def close(): Unit = { |
| 102 | + super.close() |
| 103 | + sheetData.close() |
| 104 | + } |
| 105 | +} |
0 commit comments