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 /否
你也可以使用
TaskContext.getPartitionId()
Run Code Online (Sandbox Code Playgroud)
例如,代替目前缺少的foreachPartitionWithIndex()
https://github.com/apache/spark/pull/5927#issuecomment-99697229
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)
| 归档时间: |
|
| 查看次数: |
6995 次 |
| 最近记录: |