Spark dataframe reduceByKey

3

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

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接