cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Community Platform Discussions
Connect with fellow community members to discuss general topics related to the Databricks platform, industry trends, and best practices. Share experiences, ask questions, and foster collaboration within the community.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

RDD Issue in Unity Catalog

sambgp
New Contributor

 

################################################ New Code as per UC ################################################

 

def parse_json(df, *cols, clean=True๐Ÿ˜ž    
    res = df
    for i in cols:  
        if clean:
            res = (
                res.withColumn(
                    i,
                    F.concat(F.lit('{"data": '), i, F.lit('}'))
                )
            )  

 

        data_str = res.limit(100).collect()[0].asDict()[i]
        schema = spark.sql(f"select schema_of_json('{data_str}')").collect()[0][0]
        res = res.withColumn(i, F.from_json(F.col(i), schema))
       
        if clean:
            res = res.withColumn(i, F.col(i).data)
    return res
 
 

################################################   OLD Code as per Non-UC  ################################################


# def parse_json(df, *cols, clean=True):    
#     res = df
#     for i in cols:  
#         if clean:
#             res = (
#                 res.withColumn(
#                     i,
#                     F.concat(F.lit('{"data": '), i, F.lit('}'))
#                 )
#             )        
#         schema = spark.read.json(res.rdd.map(lambda x: x[i])).schema
#         res = res.withColumn(i, F.from_json(F.col(i), schema))
       
#         if clean:
#             res = res.withColumn(i, F.col(i).data)
#     return res


previously we were using rdd.flatmap , but as rdd.flatmap doesnt supports in UC. so we modified the code and now new code we have put limit as 100 to read json data as if i dont put it throws memory error. and if i put limit many elements we get as null which shouldnt happen . its nested JSON. pls help




2 REPLIES 2

iakshaykr
New Contributor III

Rishabh_Tiwari
Databricks Employee
Databricks Employee

Hi @sambgp ,

Thank you for reaching out to our community! We're here to help you. 

To ensure we provide you with the best support, could you please take a moment to review the response and choose the one that best answers your question? Your feedback not only helps us assist you better but also benefits other community members who may have similar questions in the future.

If you found the answer helpful, consider giving it a kudo. If the response fully addresses your question, please mark it as the accepted solution. This will help us close the thread and ensure your question is resolved.

We appreciate your participation and are here to assist you further if you need it!

Thanks,

Rishabh

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