-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from bp/dazfuller/implement-failuresafe-parser
Implement FailureSafeParser
- Loading branch information
Showing
11 changed files
with
111 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,5 +44,5 @@ val commonSettings = Seq( | |
"2.12.10" | ||
} | ||
}, | ||
scalaTestVersion := "3.2.2" | ||
scalaTestVersion := "3.2.5" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
sbt.version=1.3.13 | ||
sbt.version=1.4.7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,31 @@ | ||
package com.bp.sds.cef | ||
|
||
import org.apache.hadoop.conf.Configuration | ||
import org.apache.hadoop.io.Text | ||
import org.apache.spark.internal.Logging | ||
import org.apache.spark.sql.catalyst.InternalRow | ||
import org.apache.spark.sql.catalyst.expressions.GenericInternalRow | ||
import org.apache.spark.sql.catalyst.util.{BadRecordException, FailFastMode, PermissiveMode} | ||
import org.apache.spark.sql.catalyst.util.{BadRecordException, FailFastMode, FailureSafeParser, PermissiveMode} | ||
import org.apache.spark.sql.execution.datasources.{HadoopFileLinesReader, PartitionedFile} | ||
import org.apache.spark.sql.types.StructType | ||
import org.apache.spark.{SparkException, TaskContext} | ||
|
||
final class CefDataIterator(conf: Configuration, file: PartitionedFile, dataSchema: StructType, requiredSchema: StructType, cefOptions: CefParserOptions) extends Iterator[InternalRow] with Logging { | ||
private val parser = new CefRecordParser(cefOptions) | ||
final class CefDataIterator(cefOptions: CefParserOptions) extends Logging { | ||
|
||
private val bufferedReader = new HadoopFileLinesReader(file, conf) | ||
Option(TaskContext.get()).foreach(_.addTaskCompletionListener[Unit](_ => bufferedReader.close())) | ||
def readFile(conf: Configuration, file: PartitionedFile, schema: StructType): Iterator[InternalRow] = { | ||
val parser = new CefRecordParser(cefOptions) | ||
|
||
override def hasNext: Boolean = bufferedReader.hasNext | ||
val linesReader = new HadoopFileLinesReader(file, conf) | ||
Option(TaskContext.get()).foreach(_.addTaskCompletionListener[Unit](_ => linesReader.close())) | ||
|
||
override def next(): InternalRow = { | ||
try { | ||
parser.parseToInternalRow(bufferedReader.next().toString, requiredSchema) | ||
} catch { | ||
case e: BadRecordException => | ||
logInfo(s"Invalid record found in file '${file.filePath}': ${e.getMessage}", e) | ||
cefOptions.mode match { | ||
case PermissiveMode => | ||
e.partialResult() match { | ||
case Some(row) => row | ||
case None => new GenericInternalRow(requiredSchema.fields.length) | ||
} | ||
case _ => | ||
logError(s"Failed to read file because of invalid record in file '${file.filePath}': ${e.getMessage}") | ||
throw new SparkException( | ||
s"""Invalid rows detected in ${file.filePath}. Parse mode ${FailFastMode.name}. | ||
|To process malformed records as null try setting 'mode' to 'PERMISSIVE'.""".stripMargin, e) | ||
} | ||
} | ||
val safeParser = new FailureSafeParser[Text]( | ||
input => Seq(parser.parseToInternalRow(input.toString, schema)), | ||
cefOptions.mode, | ||
schema, | ||
cefOptions.corruptColumnName | ||
) | ||
|
||
linesReader.flatMap(safeParser.parse) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters