pyspark如何在键上连接,但也包括其他列?

pyspark如何在键上连接,但也包括其他列?,pyspark,Pyspark,我想加入两个RDD RDD1 ((a, b, c, d, e), 5) ((a, b, c, d1, e), 12) ((a, b, c, d2, e), 29) RDD2 ((a, b, c, f, e), 100) 我希望最终联接结果如下所示: ((a, b, c, d, e), 5, 100) ((a, b, c, d1, e), 12, 100) ((a, b, c, d2, e), 29, 100) 所以连接键是'a,b,c,e'。只要键匹配,我想忽略第4列 然后左键将总数100

我想加入两个RDD

RDD1
((a, b, c, d, e), 5)
((a, b, c, d1, e), 12)
((a, b, c, d2, e), 29)

RDD2
((a, b, c, f, e), 100)
我希望最终联接结果如下所示:

((a, b, c, d, e), 5, 100)
((a, b, c, d1, e), 12, 100)
((a, b, c, d2, e), 29, 100)
所以连接键是'a,b,c,e'。只要键匹配,我想忽略第4列 然后左键将总数100连接回RDD1

我知道如何在sql中实现,但不知道如何在pyspark中实现。这是我的进展,但我不能得到结果,因为我不知道如何加入关键,也有第四列回到上面

rdd1 = sc.parallelize((a, b, c, d, e), 5),((a, b, c, d1, e), 12),((a, b, c, d2, e), 29))
rdd2 = sc.parallelize(((a, b, c, f, e), 100))

rdd1.coalesce(50).map(lambda x: [x[0][0], x[0][1], x[0][2], x[0][4], x[1]]) \
.join(rdd2.map(lambda x: [x[0][0], x[0][1], x[0][2], x[0][4], x[1]])

(a, b, c, e, 5, 100)
(a, b, c, e, 12, 100)
(a, b, c, e, 29, 100)

有什么提示吗?

我稍微更改了您的输入(假设a、b、c是字符串),并添加了一些括号

对于解决方案,您可以将整个rdd1保留在第一个映射中,以便在连接后重新创建密钥:

rdd1 = sc.parallelize([(("a", "b", "c", "d", "e"), 5), (("a", "b", "c", "d1", "e"), 12), (("a", "b", "c", "d2", "e"), 29)])
rdd2 = sc.parallelize([(("a", "b", "c", "f", "e"), 100)])

rdd_res = (rdd1.map(lambda x: ((x[0][0], x[0][1], x[0][2], x[0][4]), x))  # take the whole thing since we want to keep it
           .join(rdd2.map(lambda x: ((x[0][0], x[0][1], x[0][2], x[0][4]), x[1]))) # take just the last part to append
           .map(lambda x: (x[1][0][0], x[1][0][1], x[1][1])) # get rid of the temporary key and reformat so last two parts are flat
           )

rdd_res.collect()
#[(('a', 'b', 'c', 'd', 'e'), 5, 100), (('a', 'b', 'c', 'd1', 'e'), 12, 100), (('a', 'b', 'c', 'd2', 'e'), 29, 100)]

您使用rdd而不是dataframe的原因是什么?您是否可以添加一个输入数据的工作示例,现在是
a
b
c
。。。未定义,代码崩溃