是否可以将额外的参数传递给pySpark中的映射函数?
具体来说,我有以下代码配方:
raw_data_rdd = sc.textFile("data.json", use_unicode=True)
json_data_rdd = raw_data_rdd.map(lambda line: json.loads(line))
mapped_rdd = json_data_rdd.flatMap(processDataLine)
除了JSON对象外,函数
processDataLine
还接受其他参数,例如:def processDataLine(dataline, arg1, arg2)
如何将额外的参数
arg1
和arg2
传递给flaMap
函数? 最佳答案
flatMap
中使用匿名函数json_data_rdd.flatMap(lambda j: processDataLine(j, arg1, arg2))
或 curry
processDataLine
f = lambda j: processDataLine(dataline, arg1, arg2)
json_data_rdd.flatMap(f)
processDataLine
:def processDataLine(arg1, arg2):
def _processDataLine(dataline):
return ... # Do something with dataline, arg1, arg2
return _processDataLine
json_data_rdd.flatMap(processDataLine(arg1, arg2))
toolz
库提供了有用的curry
装饰器:from toolz.functoolz import curry
@curry
def processDataLine(arg1, arg2, dataline):
return ... # Do something with dataline, arg1, arg2
json_data_rdd.flatMap(processDataLine(arg1, arg2))
请注意,我已将
dataline
参数推到最后一个位置。这不是必需的,但是通过这种方式,我们不必使用关键字args。 functools.partial
在注释中已经提到了Avihoo Mamka。