简体   繁体   English

如何通过在python中添加2个RDD的对应元素来创建RDD

[英]How to create a RDD by adding the corresponding elements of 2 RDDs in python

So I have 2 RDDs (lets say RDD1 and RDD2), each with a list of numbers.所以我有 2 个 RDD(比如说 RDD1 和 RDD2),每个都有一个数字列表。 The two lists are of the same size.这两个列表大小相同。 I want to create a RDD3 where each element in RDD3 is the addition of the corresponding elements of RDD1 and RDD2.我想创建一个 RDD3,其中 RDD3 中的每个元素都是 RDD1 和 RDD2 的相应元素的加法。 How do I do this in python using pyspark functions?如何使用 pyspark 函数在 python 中执行此操作?

If the list are not too big then following can work.如果列表不是太大,那么以下可以工作。 Let me know if this works or if you have other suggestions让我知道这是否有效或您是否有其他建议

rdd1 = sc.parallelize([100,200,300])
rdd2 = sc.parallelize([101,202,303])
print(rdd1.collect())
print(rdd2.collect())
# [100, 200, 300]
# [101, 202, 303]
output = []
for i, element in enumerate(rdd1.collect()):
  output.append(element + rdd2.collect()[i])
rdd3 = sc.parallelize(output)
print(rdd3.collect())
# [201, 402, 603]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM