当此代码在不同的执行程序上运行时,对于 n 种情况,在 Spark 的调用方法中实现循环的方法是什么?

What is the way to implement loop inside call method of Spark for n number of cases as this code runs on different executors?

我正在尝试在 n 种情况下在 Spark 的调用方法中实现一个循环:

int counter=0
while (counter <N)
{
 //do something

counter++;

}

这个我必须在Spark的调用方法里面做。然而,在 call 方法的情况下,计数器将在驱动程序中,而内部代码将在执行程序中。遇到火花怎么办?

谢谢

如果你真的想保持你的 "while" 逻辑,你可以使用 Spark Accumulator:

Accumulator<Integer> counter = sc.accumulator(0);

while (counter.value < N) {
     myRDD.foreach(x -> {
         counter.add(1); 
         // do something
     }
}