RDD.foreachPartition/foreach这两个action的操作:
这两个action主要用于对每个partition中的iterator实行迭代的处理。通过用户传入的function对iterator进行内容的处理。
foreach的操作
由下面的源码中,foreach操作是直接调迭代rdd中每一条数据进行function操作。
/**
* Applies a function f to all elements of this RDD.
* 将函数应用在RDD的所有元素;
*/
def foreach(f: T => Unit): Unit = withScope {
val cleanF = sc.clean(f)
//runJob job的运行
sc.runJob(this, (iter: Iterator[T]) => iter.foreach(cleanF))
}
foreachPartition操作
这个函数也是根据传入的function进行处理,但不同处在于,这里function的传入参数是一个partition对应数据的iterator,而不是直接使用iterator的foreach。
/**
* Applies a function f to each partition of this RDD.
* 将函数f应用于该RDD的每个分区。
*/
def foreachPartition(f: Iterator[T] => Unit): Unit = withScope {
val cleanF = sc.clean(f)
sc.runJob(this, (iter: Iterator[T]) =&g