I am new to Spark and was trying to handle bad records. I found the below while searching
val df = spark.read
.option("badRecordsPath", "/tmp/badRecordsPath")
.schema("Country String, Rank Integer")
.csv("/tmp/inputFile.csv")
We are reading multiple files and need to validate the schema. But the problem here is that in each input CSV file we have some column names with a specific pattern and it can be dynamic in numbers in each file. For example, consider the below headers in the first CSV file
ID, POL_1, POL_2, POL_3, . . . . POL500
The second CSV file have below headers
ID, POL_1, POL_2, POL_3, ...,POL_10
Now for each file we have a different number of headers and with a similar pattern. So in that case I can not hard code the schema String or Struct fields. Is there any way to achieve the same?
One solution I can think of preload the input file each time and extract schema and then apply regex and assign the data type. But in this case, we have to load twice the file.
Can anyone please help me with this because I have been struggling with this.