问题:
在pyspark中自定义udf函数,在自定义函数中使用round()方法,代码如下:
def get_rent_sale_ratio(num,total):
return str(round(num/total,3))
在运行PySpark程序的时候,报错为: AttributeError: ‘NoneType’ object has no attribute ‘_jvm’
解决办法:
使用from pyspark.sql.functions import * 倒入pyspark函数时,覆盖了python的round()方法导致的,因此在自定义函数中重新引入round函数就可以了。代码如下
def get_rent_sale_ratio(num,total):
builtin = __import__('__builtin__')
round = builtin.round
return str(round(num/total,3))
from pyspark.sql.functions import *这句话覆盖了很多python自带的函数,因此,不只round函数,其他函数也可能会有被覆盖的情况出现。