在SparkSQL中UDAF函数调用报错情况怎么办
Admin 2022-11-02 群英技术资讯 784 次浏览
今天就跟大家聊聊有关“在SparkSQL中UDAF函数调用报错情况怎么办”的内容,可能很多人都不太了解,为了让大家认识和更进一步的了解,小编给大家总结了以下内容,希望这篇“在SparkSQL中UDAF函数调用报错情况怎么办”文章能对大家有帮助。在SparkSQL中,因为需要用到自定义的UDAF函数,所以用pyspark自定义了一个,但是遇到了一个问题,就是自定义的UDAF函数一直报
AttributeError: 'NoneType' object has no attribute '_jvm'
在此将解决过程记录下来
在新建的py文件中,先自定义了一个UDAF函数,然后在 if __name__ == '__main__': 中调用,死活跑不起来,一遍又一遍的对源码,看起来自定义的函数也没错:过程如下:
import decimal
import os
import pandas as pd
from pyspark.sql import SparkSession
from pyspark.sql import functions as F
os.environ['SPARK_HOME'] = '/export/server/spark'
os.environ["PYSPARK_PYTHON"] = "/root/anaconda3/bin/python"
os.environ["PYSPARK_DRIVER_PYTHON"] = "/root/anaconda3/bin/python"
@F.pandas_udf('decimal(17,12)')
def udaf_lx(qx: pd.Series, lx: pd.Series) -> decimal:
# 初始值 也一定是decimal类型
tmp_qx = decimal.Decimal(0)
tmp_lx = decimal.Decimal(0)
for index in range(0, qx.size):
if index == 0:
tmp_qx = decimal.Decimal(qx[index])
tmp_lx = decimal.Decimal(lx[index])
else:
# 计算lx: 计算后,保证数据小数位为12位,与返回类型的设置小数位保持一致
tmp_lx = (tmp_lx * (1 - tmp_qx)).quantize(decimal.Decimal('0.000000000000'))
tmp_qx = decimal.Decimal(qx[index])
return tmp_lx
if __name__ == '__main__':
# 1) 创建 SparkSession 对象,此对象连接 hive
spark = SparkSession.builder.master('local[*]') \
.appName('insurance_main') \
.config('spark.sql.shuffle.partitions', 4) \
.config('spark.sql.warehouse.dir', 'hdfs://node1:8020/user/hive/warehouse') \
.config('hive.metastore.uris', 'thrift://node1:9083') \
.enableHiveSupport() \
.getOrCreate()
# 注册UDAF 支持在SQL中使用
spark.udf.register('udaf_lx', udaf_lx)
# 2) 编写SQL 执行
excuteSQLFile(spark, '_04_insurance_dw_prem_std.sql')
然后跑起来就报了以下错误:
Traceback (most recent call last):
File "/root/anaconda3/lib/python3.8/site-packages/pyspark/sql/types.py", line 835, in _parse_datatype_string
return from_ddl_datatype(s)
File "/root/anaconda3/lib/python3.8/site-packages/pyspark/sql/types.py", line 827, in from_ddl_datatype
sc._jvm.org.apache.spark.sql.api.python.PythonSQLUtils.parseDataType(type_str).json())
AttributeError: 'NoneType' object has no attribute '_jvm'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/root/anaconda3/lib/python3.8/site-packages/pyspark/sql/types.py", line 839, in _parse_datatype_string
return from_ddl_datatype("struct<%s>" % s.strip())
File "/root/anaconda3/lib/python3.8/site-packages/pyspark/sql/types.py", line 827, in from_ddl_datatype
sc._jvm.org.apache.spark.sql.api.python.PythonSQLUtils.parseDataType(type_str).json())
AttributeError: 'NoneType' object has no attribute '_jvm'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/root/anaconda3/lib/python3.8/site-packages/pyspark/sql/types.py", line 841, in _parse_datatype_string
raise e
File "/root/anaconda3/lib/python3.8/site-packages/pyspark/sql/types.py", line 831, in _parse_datatype_string
return from_ddl_schema(s)
File "/root/anaconda3/lib/python3.8/site-packages/pyspark/sql/types.py", line 823, in from_ddl_schema
sc._jvm.org.apache.spark.sql.types.StructType.fromDDL(type_str).json())
AttributeError: 'NoneType' object has no attribute '_jvm'
我左思右想,百思不得骑姐,嗐,跑去看 types.py里面的type类型,以为我的 udaf_lx 函数的装饰器里面的 ‘decimal(17,12)’ 类型错了,但是一看,好家伙,types.py 里面的774行
_FIXED_DECIMAL = re.compile(r"decimal\(\s*(\d+)\s*,\s*(-?\d+)\s*\)")
这是能匹配上的,没道理啊!
然后再往回看报错的信息的最后一行:
AttributeError: 'NoneType' object has no attribute '_jvm'
竟然是空对象没有_jvm这个属性!
一拍脑瓜子,得了,pyspark的SQL 在执行的时候,需要用到 JVM ,而运行pyspark的时候,需要先要为spark提供环境,也就说,内存中要有SparkSession对象,而python在执行的时候,是从上往下,将方法加载到内存中,在加载自定义的UDAF函数时,由于有装饰器@F.pandas_udf的存在 , F 则是pyspark.sql.functions, 此时加载自定义的UDAF到内存中,需要有SparkSession的环境提供JVM,而此时的内存中尚未有SparkSession环境!因此,将自定义的UDAF 函数挪到 if __name__ == '__main__': 创建完SparkSession的后面,如下:
import decimal
import os
import pandas as pd
from pyspark.sql import SparkSession
from pyspark.sql import functions as F
os.environ['SPARK_HOME'] = '/export/server/spark'
os.environ["PYSPARK_PYTHON"] = "/root/anaconda3/bin/python"
os.environ["PYSPARK_DRIVER_PYTHON"] = "/root/anaconda3/bin/python"
if __name__ == '__main__':
# 1) 创建 SparkSession 对象,此对象连接 hive
spark = SparkSession.builder.master('local[*]') \
.appName('insurance_main') \
.config('spark.sql.shuffle.partitions', 4) \
.config('spark.sql.warehouse.dir', 'hdfs://node1:8020/user/hive/warehouse') \
.config('hive.metastore.uris', 'thrift://node1:9083') \
.enableHiveSupport() \
.getOrCreate()
@F.pandas_udf('decimal(17,12)')
def udaf_lx(qx: pd.Series, lx: pd.Series) -> decimal:
# 初始值 也一定是decimal类型
tmp_qx = decimal.Decimal(0)
tmp_lx = decimal.Decimal(0)
for index in range(0, qx.size):
if index == 0:
tmp_qx = decimal.Decimal(qx[index])
tmp_lx = decimal.Decimal(lx[index])
else:
# 计算lx: 计算后,保证数据小数位为12位,与返回类型的设置小数位保持一致
tmp_lx = (tmp_lx * (1 - tmp_qx)).quantize(decimal.Decimal('0.000000000000'))
tmp_qx = decimal.Decimal(qx[index])
return tmp_lx
# 注册UDAF 支持在SQL中使用
spark.udf.register('udaf_lx', udaf_lx)
# 2) 编写SQL 执行
excuteSQLFile(spark, '_04_insurance_dw_prem_std.sql')
运行结果如图:

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要给大家分享python实现多线程的方法,下完实例有一定的借鉴价值,感兴趣的朋友可以参考一下,希望大家阅读完这篇文章能有所收获,下面我们一起来学习一下吧。
这篇文章主要介绍了python数据结构之递归讲解,递归是解决问题的一种方法,它将问题不断地分成更小的子问题,直到子问题可以用普通的方法解决。通常情况下,递归会使用一个不停调用自己的函数,下面来看看文章对此的详细介绍吧
遇到一个情况,需要进行递归操作,但是呢递归次数非常大,有一万多次。先不说一万多次递归,原来的测试代码是java的,没装jdk和编译环境,
这篇文章给大家分享的是Python中的__new__和__init__的区别,对于__new__和__init__两者的区别及关联,有一些朋友不是很清楚,对此这篇文章就给大家来介绍一下,有需要的朋友接下来一起跟随小编看看吧。
eval()函数是python中的内置函数,用于将字符串str当成有效的表达式来求值并返回计算结果。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008