Issue with customrecordparser-
- 主要语言
- Scala
- 星标
- 170
- 派生
- 96
- 平均合并
- 57 分钟
- 30 天内合并 PR
- 2
描述
Hi,
We have a file with variable length and length is determined by first 6 bytes-(has multiple copybook so merged all of them-)
I am using custom record parser for this file, below are the sample parser and main class which is used-
**Issue:**
Nothing is matching with that 6 bytes and all are throwing an error-
and also tried to read the rejected records given size as 1000 as a sample but all the records are blank/nulls-
In the below code I have given 5 sample patterns for the file, however we have around 22 different patterns-
Can you please give us a suggestion, whether we are on the right path and how to achieve this?
Appreciate your cooperation-
Thanks-
-------------------------------------------------------------------------------------------------------------------
package com.example.spark.cobol.app
import za.co.absa.cobrix.cobol.parser.common.Constants
import za.co.absa.cobrix.cobol.parser.headerparsers.{RecordHeaderParser, RecordMetadata}
class CustomRecordHeadersParser extends Serializable with RecordHeaderParser {
/** RDW header is a 5 byte header */
override def getHeaderLength: Int = 6
override def isHeaderDefinedInCopybook: Boolean = true
override def getRecordMetadata(header: Array[Byte], fileOffset: Long, fileSize: Long, recordNum: Long): RecordMetadata = {
val rdwHeaderBlock = getHeaderLength
if (header.length < rdwHeaderBlock) {
RecordMetadata(-1, isValid = false)
}
else if(header.map(_ & 0xFF).mkString("") == "01KJUG"){
RecordMetadata(8034, isValid = true)
}
else if(header.map(_ & 0xFF).mkString("") == "40NJHY"){
RecordMetadata(18034, isValid = true)
}
else if(header.map(_ & 0xFF).mkString("") == "87BGHO"){
RecordMetadata(6575, isValid = true)
}
else if(header.map(_ & 0xFF).mkString("") == "09GHGT"){
RecordMetadata(2678, isValid = true)
}
else if(header.map(_ & 0xFF).mkString("").take(3) == "HDR"){
RecordMetadata(65, isValid = false)
}
else {
//throw new IllegalStateException(s"Custom RDW headers is not matching with any of the patterns-")
RecordMetadata(1000, isValid = true)
}
}
}
------------------------------------------------------------------------------------------------------------------
package com.example.spark.cobol.app
import org.apache.log4j.{Level, Logger}
import org.apache.spark.sql.SparkSession
object SparkCodecApp {
def main(args: Array[String]): Unit = {
// Switch logging level to WARN
Logger.getLogger("org").setLevel(Level.WARN)
Logger.getLogger("akka").setLevel(Level.WARN)
val spark = SparkSession
.builder()
.appName("Spark-Cobol Custom header parser example")
.getOrCreate()
val df = spark
.read
.format("cobol")
.option("copybook", "../example_data/copybook_codec.cob")
.option("is_record_sequence", "true")
.option("generate_record_id", true)
.option("schema_retention_policy", "collapse_root")
.option("record_header_parser", "com.example.spark.cobol.app.CustomRecordHeadersParser") // Custom record header parser class
.load("../example_data/data_codec/somefile.dat")
df.printSchema()
df.show
}
}
----------------------------------------------------------------------------------------------
贡献指南
这个仓库没有索引到贡献指南
评估
这个 Issue 还没有评估数据。