找出分区号/ id

Raj*_*Raj 4 apache-spark

Spark中是否有方法(A方法)找出Parition ID/No

以这个例子为例

val input1 = sc.parallelize(List(8, 9, 10), 3)

val res = input1.reduce{ (x, y) => println("Inside partiton " + ???)

                               x + y)}
Run Code Online (Sandbox Code Playgroud)

我想放入一些代码???来打印分区ID /否

Hol*_*den 10

事实上,它mapParitionsWithIndex会给你一个迭代器和分区索引。(这当然与 reduce 不同,但您可以将其结果与 结合起来aggregate)。


ste*_*r25 9

你也可以使用

TaskContext.getPartitionId()
Run Code Online (Sandbox Code Playgroud)

例如,代替目​​前缺少的foreachPartitionWithIndex()

https://github.com/apache/spark/pull/5927#issuecomment-99697229


Raj*_*Raj 6

mapParitionsWithIndex根据@Holden 的建议在此处发布答案。

我创建了一个Input带有 3 个分区的 RDD( )。中的元素在调用input中用分区索引( index)标记mapPartitionsWithIndex

scala> val input = sc.parallelize(11 to 17, 3)
input: org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[9] at parallelize at <console>:21

scala> input.mapPartitionsWithIndex{ (index, itr) => itr.toList.map(x => x + "#" + index).iterator }.collect()
res8: Array[String] = Array(11#0, 12#0, 13#1, 14#1, 15#2, 16#2, 17#2)
Run Code Online (Sandbox Code Playgroud)