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

How to pass list of RDDs to groupWith in Pyspark

我正在尝试将 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 接受可变参数,你所要做的就是解压参数:

w.groupWith(*m[1:])