简体   繁体   English

在python Spark中组合2个RDD

[英]Combining 2 RDDs in python Spark

I have 2 RDDs. 我有2个RDD。 Assume rdd1 = {'a','b','c', 'a', 'c', 'a'} and rdd2 is an output of KMeans with cluster assignment as follows -> rdd2={0,0,1,1,1,0}. 假设rdd1 = {'a','b','c','a','c','a'}并且rdd2是KMeans的输出,其簇分配如下-> rdd2 = {0,0,1 ,1,1,0}。 I want to eventually find out how many a's and b's are there in cluster 0 and 1. For example 0 has 2 a's so something like {0, a, 2} etc. Is there a way I combine these 2 RDDS to do such an operation? 我想最终找出簇0和1中有多少个a和b。例如0具有2个a,所以类似{0,a,2}等。有没有办法我将这两个RDDS结合起来做这样一个操作?

Thanks for your help. 谢谢你的帮助。

The below works. 下面的作品。 using tuples and list instead of set wherever appropriate. 使用tupleslist而不是在适当的地方set

rdd1 = sc.parallelize(['a','b','c', 'a', 'c', 'a'])
rdd2 = sc.parallelize([0, 0, 1, 1, 1, 0])
rdd = rdd1.zip(rdd2).map(lambda x: (x,1)).reduceByKey(lambda x,y: x+y).map(lambda ((x1,x2),y): (x1,x2,y))
rdd.collect()

Output: 输出:

[('a', 0, 2), ('b', 0, 1), ('c', 1, 2), ('a', 1, 1)]

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

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