SparkStreaming

本文深入探讨了Spark Streaming的工作原理,介绍了其内部机制,包括如何通过后台线程接收数据流并将数据保存到BlockManager,以及如何按时间间隔封装成批量RDD进行处理。此外,还详细解释了BlockGenerator的作用及其实现细节。

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

Spark Streaming的内核机制。后台线程负责接收源数据的流,收到的数据保存到BlockManager,并按照时间间隔封装成批量RDD,在RDD上执行各种操作。

每种数据源提供自己的Receiver的实现,将接收的数据调用store方法保存下来,store方法调用ReceiverSupervisorImpl来保存真正的数据,ReceiverSupervisorImpl调用pushSingle等方法将数据添加到BlockGenerator。BlockGenerator启动RecurringTimer定时器保证按照程序设置的时间间隔触发spark streaming的任务执行,从BlockGenerator存储中取出已经接收到的所有数据并构造RDD数组。同时在这一系列过程中通过BlockGeneratorListener传递消息。

每种子实时数据源需要实现自己的DStream的子类。

    1. BlockGenerator

在reveiver/ BlockGenerator.scala中。负责接收DStream的数据并保存到blocksForPushing中。对于每种需要接收的实时流,都会启动一个BlockGenerator负责在后台默默的接收数据并保存到BlockManager中,同时产生Block并添加到blocksForPushing队列中。

BlockGernerator的默认的时间间隔是200ms,可以设置。定时器的代码如下:

/** Change the buffer to which single records are added to. */

入参timeperiod的整数倍,time-period就是实时流RDD批次的序号。

  private def updateCurrentBuffer(time: Long): Unit = {

    try {

      var newBlock: Block = null

      synchronized {

        if (currentBuffer.nonEmpty) {

          val newBlockBuffer = currentBuffer

          currentBuffer = new ArrayBuffer[Any]

          val blockId = StreamBlockId(receiverId, time - blockIntervalMs)

          listener.onGenerateBlock(blockId)

          newBlock = new Block(blockId, newBlockBuffer)

        }

      }

 

      if (newBlock != null) {

        blocksForPushing.put(newBlock// put is blocking when queue is full

      }

    } catch {

      case ie: InterruptedException =>

        logInfo("Block updating timer thread was interrupted")

      case e: Exception =>

        reportError("Error in block updating thread", e)

    }

  }

    1. ReceiverInputDStream

很多Receiver的基类,利用compute方法生成RDD[T],从receiverTracker中根据validTime获取当前batch时间的数据,一般存储在BlockManager中,包装成BlockRDD[T]。

转载于:https://my.oschina.net/u/778683/blog/1831255

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值