带参数的pyspark-udf

带参数的pyspark-udf,pyspark,user-defined-functions,Pyspark,User Defined Functions,需要将一个pyspark数据帧列checkin_time从毫秒传输到时区调整时间戳,时区信息在另一列tz_info中 尝试了以下内容: def tz_adjust(x,tz_info): if tz_info: y = col(x)+ col(tz_info) return from_unixtime(col(y)/1000) else: return from_unixtime(col(x)/1000) def udf

需要将一个pyspark数据帧列
checkin_time
从毫秒传输到时区调整时间戳,时区信息在另一列
tz_info

尝试了以下内容:

def tz_adjust(x,tz_info):
    if tz_info:
        y = col(x)+ col(tz_info) 
        return from_unixtime(col(y)/1000)
    else:
        return from_unixtime(col(x)/1000)
    
def udf_tz_adjust(tz_info):
    return udf(lambda l: tz_adjust(l, tz_info))     

使用此自定义项时,将

df.withColumn('checkin_time',udf_tz_adjust('time_zone')(col('checkin_time')))

got some error:
AttributeError: 'NoneType' object has no attribute '_jvm'

有没有想过将第二列作为参数传递给udf?
谢谢。

IMHO,您正在做的是UDF和部分函数的组合,这可能会变得很棘手。我认为您根本不需要为应用程序使用UDF。您可以执行以下操作

#未测试
从pyspark.sql.functions导入*
df.with column('checkin_time'),when(col(“tz_info”).isNotNull(),(from unixtime(col(“checkin_time”))+F.col(“tz_info”)/1000)。否则(from unixtime(col(“checkin_time”)/1000))

UDF有自己的
serde
低效率,与python一起使用时效率更低,因为它会增加将scala数据类型转换为python数据类型的额外开销。

是的,您的解决方案是正确的。这是我已经实现的一个解决方案。还有其他几个列需要相同的处理过程,所以我认为udf可能更具可伸缩性。