如何在 Pyspark 中将 RDD 列表传递给 groupWith



我正在尝试将RDD列表传递给groupWith,而不是按索引手动指定它们。

以下是示例数据

w = sc.parallelize([("1", 5), ("3", 6)])
x = sc.parallelize([("1", 1), ("3", 4)])
y = sc.parallelize([("2", 2), ("4", 3)])
z = sc.parallelize([("2", 42), ("4", 43), ("5", 12)])

现在我已经创建了一个这样的数组。

m = [w,x,y,z]

手动硬编码方式是

[(x, tuple(map(list, y))) for x, y in sorted(list(m[0].groupWith(m[1],m[2],m[3]).collect()))]

打印以下结果

[('1', ([5], [1], [], [])), 
('2', ([], [], [2], [42])), 
('3', ([6], [4], [], ])),
 ('4', ([], [], [3], [43])), 
('5', ([], [], [], [12]))]

但我想做一些类似传递m[1:]而不是手动传递的事情。

[(x, tuple(map(list, y))) for x, y in sorted(list(m[0].groupWith(m[1:]).collect()))]

试图删除括号,但它必须转换为字符串,我得到以下错误

AttributeError: 'list' object has no attribute 'mapValues'
    AttributeError: 'str' object has no attribute 'mapValues'

由于groupWith接受 varargs,您所要做的就是解压缩参数:

w.groupWith(*m[1:])

最新更新