Spark数据帧reduceByKey

时间:2016-05-18 18:12:08

标签: join apache-spark apache-spark-sql reduce

我正在使用Spark 1.5 / 1.6,我想在DataFrame中执行reduceByKey操作,我不想将df转换为rdd。

每一行看起来都像id1那样有多行。

id1, id2, score, time

我希望有类似的东西:

id1, [ (id21, score21, time21) , ((id22, score22, time22)) , ((id23, score23, time23)) ]

因此,对于每个“id1”,我想要列表中的所有记录

顺便说一下,之所以不想将df转换为rdd是因为我必须将这个(简化的)数据帧加入另一个数据帧,而我正在对连接键进行重新分区,这使得它更快,我想用rdd

做同样的事情

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:4)

要简单地保留已经实现的分区,请在reduceByKey调用中重新使用父RDD分区:

 val rdd = df.toRdd
 val parentRdd = rdd.dependencies(0) // Assuming first parent has the 
                                     // desired partitioning: adjust as needed
 val parentPartitioner = parentRdd.partitioner
 val optimizedReducedRdd = rdd.reduceByKey(parentPartitioner, reduceFn)

如果您指定分区程序如下:

 df.toRdd.reduceByKey(reduceFn)  // This is non-optimized: uses full shuffle

然后你会注意到的行为 - 即发生完全洗牌。这是因为会改为使用HashPartitioner

相关问题