I am trying to create a Spark-UDF inside of a python class. Meaning, one of the methods in a class is the UDF. I am getting an error named " PicklingError: Could not serialize object: TypeError: can't pickle _MovedItems objects "
Environment : Azure Databricks . (DBR version 6.1 Beta) Code execution : In the built in Notebook. Python version : 3.5 Spark version : 2.4.4
I have tried defining the UDF outside of the class in a separate cell, and the UDF works. I do not want to write code like that, I need to follow OOP principles and would like to keep it structured. I have tried everything on Google, did not help. In fact I did not even get the information about the error I am getting. " PicklingError: Could not serialize object: TypeError: can't pickle _MovedItems objects "
class phases():
def __init__(self, each_mp_pair_df_as_arg, unique_mp_pair_df_as_arg):
print("Inside the constructor of Class phases ")
#I need the below 2 variables to be used in my UDF, so i am trying to put
them in a class
self.each_mp_pair_phases_df = each_mp_pair_df_as_arg
self.unique_mp_pair_phases_df = unique_mp_pair_df_as_arg
#This is the UDF.
def phases_commence(self,each_row):
print(a)
return 1
#This is the function that registers the UDF,
def initiate_the_phases_on_the_major_track_segment(self):
print("Inside the 'initiate_the_phases_on_the_major_track_segment()'")
#registering the UDF
self.phases_udf = udf(self.phases_commence,LongType())
new_df = self.each_mp_pair_phases_df.withColumn("status", self.phases_udf((struct([self.each_mp_pair_phases_df[x] for x in self.each_mp_pair_phases_df.columns]))))
display(new_df)
#This is a method in a different notebook that creates an object for the above shown class and calls the methods that registers the UDF.
def getting_ready_for_the_phases(each_mp_pair_df_as_arg, unique_mp_pair_df_as_arg):
phase_obj = phases(each_mp_pair_df_as_arg, unique_mp_pair_df_as_arg)
phase_obj.initiate_the_phases_on_the_major_track_segment()
The error message is: PicklingError: Could not serialize object: TypeError: can't pickle _MovedItems objects