我有一个简单的星火计划。我想知道为什么所有数据都在一个分区中结束。
val l = List((30002,30000), (50006,50000), (80006,80000),
(4,0), (60012,60000), (70006,70000),
(40006,40000), (30012,30000), (30000,30000),
(60018,60000), (30020,30000), (20010,20000),
(20014,20000), (90008,90000), (14,0), (90012,90000),
(50010,50000), (100008,100000), (80012,80000),
(20000,20000), (30010,30000), (20012,20000),
(90016,90000), (18,0), (12,0), (70016,70000),
(20,0), (80020,80000), (100016,100000), (70014,70000),
(60002,60000), (40000,40000), (60006,60000),
(80000,80000), (50008,50000), (60008,60000),
(10002,10000), (30014,30000), (70002,70000),
(40010,40000), (100010,100000), (40002,40000),
(20004,20000),
(10018,10000), (50018,50000), (70004,70000),
(90004,90000), (100004,100000), (20016,20000))
val l_rdd = sc.parallelize(l, 2)
// print each item and index of the partition it belongs to
l_rdd.mapPartitionsWithIndex((index, iter) => {
iter.toList.map(x => (index, x)).iterator
}).collect.foreach(println)
// reduce on the second element of the list.
// alternatively you can use aggregateByKey
val l_reduced = l_rdd.map(x => {
(x._2, List(x._1))
}).reduceByKey((a, b) => {b ::: a})
// print the reduced results along with its partition index
l_reduced.mapPartitionsWithIndex((index, iter) => {
iter.toList.map(x => (index, x._1, x._2.size)).iterator
}).collect.foreach(println)
当您运行它时,您将看到数据(l_rdd
)被分发到两个分区中。一旦减少,结果RDD (l_reduced
)也有两个分区,但所有数据都在一个分区(索引0)中,另一个分区是空的。即使数据很大(几个GBs)也会发生这种情况。不应该将l_reduced
也分发到两个分区中。
发布于 2017-02-06 22:29:38
除非另有规定,否则分区将基于相关键的哈希代码完成,并假定哈希码将导致相对均匀的分布。在这种情况下,您的哈希代码都是偶数,因此都将进入分区0。
如果这确实代表了您的数据集,那么reduceByKey
就会有一个过载,它会占用分区程序和reduce函数。我建议为这样的数据集提供另一种分区算法。
https://stackoverflow.com/questions/42077477
复制相似问题