spark 操作map中执行self.方法报错

python 复制代码
class model(object):
    def __init__(self):
        self.data = sc.textFile('path/to/data.csv')
        # other misc setup
    def run_model(self):
        self.data = self.data.map(self.transformation_function)
    def transformation_function(self,row):
        row = row.split(',')
        return row[0]+row[1]
test = model()
test.run_model()
test.data.take(10)

在pyspark中调用类方法,报错

Could not serialize object: Exception: It appears that you are attempting to broadcast an RDD or reference an RDD from an action or transformation. RDD transformations and actions can only be invoked by the driver, not inside of other transformations; for example, rdd1.map(lambda x: rdd2.values.count() * x) is invalid because the values transformation and count action cannot be performed inside of the rdd1.map transformation. For more information, see SPARK-5063.
原因

spark不允许在action或transformation中访问SparkContext,如果你的action或transformation中引用了self,那么spark会将整个对象进行序列化,并将其发到工作节点上,来确保每个执行任务的节点都能够访问到该方法以及它所依赖的类实例状态,但是序列化有一个限制,那就是不是所有的对象都可以被序列化。例如,SparkContext、SparkSession、打开的文件句柄、数据库连接等都不能被序列化,因为它们通常绑定到特定的运行环境,无法在网络上传输或在远程节点上复原。spark把对象序列化时这其中就保留了SparkContext,即使没有显式的访问它,它也会在闭包内被引用,所以会出错。
解决

将调用的类方法定义为静态方法 @staticmethod

python 复制代码
class model(object):
    @staticmethod
    def transformation_function(row):
        row = row.split(',')
        return row[0]+row[1]

    def __init__(self):
        self.data = sc.textFile('some.csv')

    def run_model(self):
        self.data = self.data.map(model.transformation_function)
相关推荐
lllsure14 分钟前
Python基础语法
开发语言·python
winfredzhang2 小时前
使用Python 打造多格式文件预览工具 — 图、PDF、Word、Excel 一站式查看
python·pdf·word·excel·照片·查看,zip,复制
浩皓素2 小时前
Python连接云端服务器:基于Paramiko库的实践与问题剖析
python
致于数据科学家的小陈2 小时前
Go 层级菜单树转 json 处理
python·go·json·菜单树·菜单权限·children
伊织code2 小时前
MixTeX - 支持CPU推理的多模态LaTeX OCR
python·ai·ocr·latex·mixtex
jardonwang12 小时前
DeepInjectSQL - 基于 AI 生成对抗网络(GAN)的下一代 SQL 注入自动化漏洞猎手
python·测试工具·生成对抗网络·安全性测试
大G哥2 小时前
加速LLM大模型推理,KV缓存技术详解与PyTorch实现
人工智能·pytorch·python·深度学习·缓存
Python×CATIA工业智造3 小时前
深入解析多线程与多进程:从理论到Python实践
python·pycharm
qq_263_tohua3 小时前
第99期 dropout防止过拟合
pytorch·python·深度学习
Amo Xiang3 小时前
Python 常用内置函数详解(十):help()函数——查看对象的帮助信息
python·内置函数·help