Issues while writing into bad_records path in Databricks

Alok Thampi 111 Reputation points
2024-06-05T01:13:41.47+00:00

Hello All,

I would like to get your inputs with a scenario that I see while writing into the bad_records file.

I am reading a ‘Ԓ’ delimited CSV file based on a schema that I have already defined. I have enabled error handling while reading the file to write the error rows into a badRecordsPath if I have a schema mismatch.

I have new line characters coming in from the source file because of which a few columns get moved to the next line and since those new rows do not align with the schema defined, it writes the rows into a file in the bad_records path that I have specified.

 

This works well for almost all the scenarios EXCEPT when I define the schema with DateType(). If I try to write a non date type value to this column, instead of writing the whole row to the bad_records path, it creates blank files in the bad_records folder. It also creates another folder named bad_files and creates another file in it which shows the error –

 "reason":"org.apache.spark.SparkUpgradeException: [INCONSISTENT_BEHAVIOR_CROSS_VERSION.PARSE_DATETIME_BY_NEW_PARSER] You may get a different result due to the upgrading to Spark >= 3.0:\nFail to parse '009-7-4-23     ' in the new parser. You can set "spark.sql.legacy.timeParserPolicy" to "LEGACY" to restore the behavior before Spark 3.0, or set to "CORRECTED" and treat it as an invalid datetime string."

 

I get this error only while defining the datatype as DateType(). For testing purposes, I tried replacing it with IntegerType/TimestampType/DoubleType,etc and all of them writes to the bad_records file as expected with the error data.

Any leads on why this happens?

modified_schema = StructType(
    [
        StructField(".....", StringType(), True),
		.....
        StructField("ENTRYDATE", DateType(), True),
		.....
        StructField(".....", IntegerType(), True)
    ]   
)

df = spark.read.format("csv").option("header","true").option("sep",” Ԓ”).schema(modified_schema).option("badRecordsPath",badRecordsPath).load(filepath)

Below are the 2 folders generated inside my badRecordsPath.

Alok1_0-1717548996735.png

Below are the files generated inside the bad_records folder and it contains no information on the erroneous rows.

Alok1_1-1717549044696.png

Azure Databricks
Azure Databricks
An Apache Spark-based analytics platform optimized for Azure.
2,045 questions
{count} votes

Accepted answer
  1. PRADEEPCHEEKATLA-MSFT 84,381 Reputation points Microsoft Employee
    2024-06-12T04:57:42.9366667+00:00

    @Alok Thampi - I understand your concern about the badRecordsPath option not logging the erroneous values for the DateType() column. It is possible that this is a bug or limitation in Spark's CSV reader implementation.

    Regarding your question about rows 3 and 4, when Spark encounters a new line character in a CSV file, it treats it as a new record. In your case, row 2 is being treated as a separate record because of the new line character at the end of the line. Rows 3 and 4 are also being treated as separate records because they are on separate lines. However, since rows 3 and 4 do not conform to the schema, they are being skipped by Spark.

    Regarding your test with the TimestampType() column, it is possible that this data type is more forgiving than the DateType() column when it comes to parsing invalid date formats. This could be why it is able to load the correct records and push the error row to the bad_records folder.

    In summary, it seems that the issue is related to the DateType() column and how Spark's CSV reader handles invalid date formats. Changing the data type to a more forgiving type, such as TimestampType(), or removing the new line characters from the source file may help in this case.

    I hope this helps! Let me know if you have any further questions.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful