cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to enforce schema on data read from jsonl file in Azure Databricks using pyspark

prabhu26
New Contributor

I'm tring to build a ETL pipeline in which I'm reading the jsonl files from the azure blob storage, then trying to transform and load it to delta tables in databricks. I have created the below schema for loading my data :

 

 

schema = StructType([
    StructField("restaurantId", IntegerType(), nullable=False),
    StructField("reviewId", IntegerType(), nullable=False),
    StructField("text", StringType(), nullable=False),
    StructField("rating", DoubleType(), nullable=False),
    StructField("publishedAt", TimestampType(), nullable=False),
    StructField("_corrupt_record", StringType(), nullable=True)
])

 

 

I'm reading the jsonl files from the below code :

 

 

df = spark.read \
    .option("mode", "PERMISSIVE") \
    .option("columnNameOfCorruptRecord", "_corrupt_record") \
    .schema(schema) \
    .json(raw_file_location).cache()

 

 

But I get no results from the below code :

 

 

display(df.select(col("_corrupt_record")).where(col("_corrupt_record").isNotNull()))

 

 

There are many column which are null in my raw data and these are inserted to the table as null and the _corrupt_record column is null for that case. Please let me know how to resolve this issue

My expectation is to see the corrupt record (the record which does not math the defined schema) populated for the failed records , for this I have also tried SQL queries to manully create the schema and load the data but still doesn't works

1 REPLY 1

DataEngineer
New Contributor II

Try this.

Add option("multiline","true")

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group