首页 文章

Spark数据帧reduceByKey

提问于
浏览
2

我正在使用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 回答

  • 3

    要简单地保留已经实现的分区,请在 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 .

相关问题