Join Spark数据帧还原键
我使用的是Spark 1.5/1.6,我想在DataFrame中执行reduceByKey操作,我不想将df转换为rdd 每一行看起来像,我有多个id1行Join Spark数据帧还原键,join,apache-spark,apache-spark-sql,reduce,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的原因是因
id1, id2, score, time
我想要一些像:
id1, [ (id21, score21, time21) , ((id22, score22, time22)) , ((id23, score23, time23)) ]
因此,对于每个“id1”,我希望所有记录都在一个列表中
顺便说一句,我不想将df转换为rdd的原因是因为我必须将这个(简化的)数据帧连接到另一个数据帧,并且我正在对连接键进行重新分区,这使得它更快,我想rdd也不能这样做
任何帮助都将不胜感激。要简单地保留已实现的分区,请在
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