【发布时间】:2021-02-24 16:16:42
【问题描述】:
我对重新分区操作感到困惑。请看下面的代码
import org.apache.spark._
import org.apache.log4j._
object FriendsByAge {
def parseLine(line: String)={
val fields = line.split(",")
val age = fields(2).toInt
val numFriends = fields(3).toInt
(age, numFriends)
}
def main(args: Array[String]) = {
Logger.getLogger("org").setLevel(Level.ERROR)
val sc = new SparkContext("local[*]", "FriendsByAge")
val lines = sc.textFile("./data/fakefriends-noheader.csv").repartition(1000)
val rdd = lines.map(parseLine)
println(rdd.getNumPartitions)
val totalsByAge = rdd.mapValues(x=> (x,1)).reduceByKey((x, y) => (x._1+y._1, x._2 + y._2))
println(totalsByAge.getNumPartitions)
val averagesByAges = totalsByAge.mapValues(x => x._1/x._2)
println(averagesByAges.getNumPartitions)
val results = averagesByAges.collect()
results.sortWith(_._2> _._2).foreach(println)
}
}
这里我在将文件读入 1000 个分区后对 rdd 进行重新分区。由于 map 操作会创建新的 RDD,并且不会保留分区。我仍然看到相同数量的分区。
问题是我如何知道子 RDD 是否会保留父 RDD 分区?子RDD使repartition失效的条件是什么。
【问题讨论】:
-
你实际上有多少条记录?
-
@thebluephantom 我有 1856 条记录。我正在尝试了解 spark 分区,所以我使用了小数据。
-
请添加每次迭代的分区数。
标签: apache-spark rdd partitioning