在类的对象列表上运行 flatMap() 时出现错误。它适用于常规的 python 数据类型,如 int、list 等,但是当列表包含我的类的对象时,我会遇到错误。这是整个代码:
from pyspark import SparkContext
sc = SparkContext("local","WordCountBySparkKeyword")
def func(x):
if x==2:
return [2, 3, 4]
return [1]
rdd = sc.parallelize([2])
rdd = rdd.flatMap(func) # rdd.collect() now has [2, 3, 4]
rdd = rdd.flatMap(func) # rdd.collect() now has [2, 3, 4, 1, 1]
print rdd.collect() # gives expected output
# Class I'm defining
class node(object):
def __init__(self, value):
self.value = value
# Representation, for printing node
def __repr__(self):
return self.value
def foo(x):
if x.value==2:
return [node(2), node(3), node(4)]
return [node(1)]
rdd = sc.parallelize([node(2)])
rdd = rdd.flatMap(foo) #marker 2
print rdd.collect() # rdd.collect should contain nodes with values [2, 3, 4, 1, 1]
代码工作正常,直到标记 1(在代码中注释)。问题出现在标记 2 之后。我收到的具体错误消息是AttributeError: 'module' object has no attribute 'node'
如何解决此错误?
我正在使用 ubuntu,运行 pyspark 1.4.1