Sparksteaming每批次中到底有多少个RDD

本文深入探讨了Spark Streaming中foreachRDD操作符的工作原理,解答了关于一个batch interval内产生的RDD数量的疑惑,并通过源码分析确认了foreachRDD针对的是每个时间间隔产生的单一RDD,而非一个时间间隔内的多个RDD。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对DStream.foreachRDD的理解

转载:https://blog.youkuaiyun.com/odailidong/article/details/78487803

 

最近在使用Spark Streaming过程中,对foreachRDD有点疑问,查阅资料后记录如下:

 

foreachRDD(func)的官方解释为

The most generic output operator that applies a function, func, to each RDD generated from the stream. This function should push the data in each RDD to an external system, such as saving the RDD to files, or writing it over the network to a database. Note that the function func is executed in the driver process running the streaming application, and will usually have RDD actions in it that will force the computation of the streaming RDDs.

对于这个定义会产生一个疑问:在一个batch interval里面会产生几个RDD? 结论:有且只有一个

那么定义里面所说的“each RDD”应该如何理解呢?

DStream可以理解为是基于时间的,即每个interval产生一个RDD,所以如果以时间为轴,每隔一段时间就会产生一个RDD,那么定义中的“each RDD”应该理解为每个interval的RDD,而不是一个interval中的每个RDD

从spark的源码分析

DStream中的foreachRDD方法最终会调用如下的代码

 
  1. private def foreachRDD(

  2. foreachFunc: (RDD[T], Time) => Unit,

  3. displayInnerRDDOps: Boolean): Unit = {

  4. new ForEachDStream(this,

  5. context.sparkContext.clean(foreachFunc, false), displayInnerRDDOps).register()

  6. }

可以看到这个方法里面并没有任何的Iterator,可以对比一下RDD中的foreachPartitionforeach方法,这两个方法是会遍历RDD,所以才会有Iterator类型的引用

 
  1. def foreach(f: T => Unit): Unit = withScope {

  2. val cleanF = sc.clean(f)

  3. sc.runJob(this, (iter: Iterator[T]) => iter.foreach(cleanF))

  4. }

  5.  
  6. def foreachPartition(f: Iterator[T] => Unit): Unit = withScope {

  7. val cleanF = sc.clean(f)

  8. sc.runJob(this, (iter: Iterator[T]) => cleanF(iter))

  9. }

而如果每个interval中有多个RDD,那么DStream中的foreachRDD也一定会有Iterator类型的引用,但是从上述的代码中并没有。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值