python - How to correctly use modules in rdd.map with pyspark
问题描述
As the title suggests, I'm trying to create external modules which are afterwards imported and invoked in a simple rdd.map
function. An example below:
## main.py ##
myrdd = spark.sparkContext.parallelize([1,2,3,4,5])
spark.sparkContext.addPyFile("myModule.py")
import myModule as mm
myrdd.map(lambda x: mm.Module.test(x)).collect()
## myModule.py ##
class Module():
def test(self,x):
return x * 2
When trying to run this with spark submit I get the following error:
test() missing 1 required positional argument: 'x'
Can someone point the error out?
Thank you very much
解决方案
test() 不是类方法,所以不能直接调用 Module.test(x) 。
而是创建一个 Module 对象并在其上调用 test() 方法,如下所示:
myrdd = spark.sparkContext.parallelize([1,2,3,4,5])
spark.sparkContext.addPyFile("myModule.py")
import myModule as mm
myrdd.map(lambda x: mm.Module().test(x)).collect()
[2, 4, 6, 8, 10]
推荐阅读
- javascript - GrapesJS - 将 HTML 作为表单的一部分发布
- c# - Xamarin.forms 自定义按钮控件
- sql - 从 nvarchar 中删除第一个新行和第一个字符。女士 SQL
- javascript -
使用 JS 在可见换行符上拆分文本 [不带 \n 或] - ios - 在视图控制器中自动调整文本大小
- html - 表单验证使用 oninvalid setCustomValidity 按钮在服务器上运行
- java - 是否每个“新”都会导致至少一个 Classloader.loadClass 调用
- powershell - PowerShell 哈希表更新适用于所有条目
- c# - 如何为从枚举生成的表创建外键?
- android - 导航栏下方的底部导航栏