只是爲了學習的目的,我試圖設置一個字典作爲累加器中的全局變量添加功能效果很好,但我運行代碼並將字典放在地圖函數中,它總是返回空。pyspark中的累加器與字典作爲全局變量
但是設置列表作爲一個全局變量
class DictParam(AccumulatorParam):
def zero(self, value = ""):
return dict()
def addInPlace(self, acc1, acc2):
acc1.update(acc2)
if __name__== "__main__":
sc, sqlContext = init_spark("generate_score_summary", 40)
rdd = sc.textFile('input')
#print(rdd.take(5))
dict1 = sc.accumulator({}, DictParam())
def file_read(line):
global dict1
ls = re.split(',', line)
dict1+={ls[0]:ls[1]}
return line
rdd = rdd.map(lambda x: file_read(x)).cache()
print(dict1)
我的問題是地圖總是空的 – user3341953