SparkStraming之Checkpoint

SparkStraming之Checkpoint

checkpoint介绍

一个流式应用必须7*24小时正常在线,而且能够容忍一些与逻辑处理无关的失败(比如:系统崩溃、JVM的宕掉)

为了达到以上这些,SparkStreaming为程序设置足够的信息作为保存点,记录计算的状态、结果等,同时将信息保存在一个容错的存储系统如HDFS,以至于能从失败中恢复,这里有2种数据将会被自动设置检查点

  • 元数据的checkpoint
    • 将定义数据计算的信息(元数据)保存在容错系统如HDFS上,这被用作恢复Streaming应用的Driver进程所在节点的failure
    • 元数据包括
      • 1、配置信息:创建流应用的配置信息
      • 2、算子操作:在流中定义的一些算子操作
      • 3、批次信息(当前批次的jobs虽进入到调度队列,但是还没完成计算)
  • 数据checkpoint
    • 将生成的RDD保存在可靠的存储,这对于那些有状态的转换,比如将不同批次的数据进行combine操作是必要的,比如updataStateByKey(),这些算子转换中,生成的RDD会依赖之前的RDD的数据批次数据,这就会造成RDD的依赖链随着时间增长,为了避免这种恢复时间随着时间无限制的增长,SparkStreaming中带状态转换算子的RDD会周期性的通过checkpoint将数据保存在可靠的存储系统如HDFS,去切断任务链,减少恢复时间

=>>> 总的来说

1.从驱动程序故障中恢复时,主要需要metadata-checkpoint

2、而如果使用有状态transformation算子,即使是基本功能,必须设置Data-Checkpoint

可以在执行配置中配置checkpoint的时间间隔

ssc.checkpoint(checkpointDirectory) 

checkpoint的使用代码

具体代码参考以及逻辑判断可以参考:

官方案例代码示意

package com.shufang.spark_streaming

import com.shufang.utils.SparkUtil
import org.apache.spark.SparkContext
import org.apache.spark.streaming.dstream.ReceiverInputDStream
import org.apache.spark.streaming.{Durations, StreamingContext}

object CreateStreamingContextByCheckpointDir {


  def functionToCreateAStreamingContext(): StreamingContext = {

    println("这是第一次创建一个StreamingContext")
    val ssc = new StreamingContext(SparkUtil.getLocalSC(), Durations.seconds(5))

    // 创建一个DStream
    val dStream: ReceiverInputDStream[String] = ssc.socketTextStream("localhost", 9999)

    // 进行逻辑操作
    dStream.flatMap(_.split("\\s"))
        .map((_,1))
        .reduceByKey(_+_)
        .print()

    // 进行checkpoint操作
    ssc.checkpoint("src/main/data/checkpoint_dir")

    // 方法返回值
    ssc
  }


  def main(args: Array[String]): Unit = {

    //创建一个StreamingContext对象
    //假如是第一次启动,那么就通过调用functionToCreateAStreamingContext的形式创建一个实例
    //假如不是,就会从checkpoint的目录进行恢复创建一个StreamingContext的对象
    val ssc: StreamingContext = StreamingContext.getOrCreate("src/main/data/checkpoint_dir", functionToCreateAStreamingContext)

    ssc.start()
    ssc.awaitTermination()
  }
}

checkpoint的使用注意事项

注意:checkpoint的时间间隔应该把控好,如果设置的太小那么会导致性能低下,吞吐量下降;如果设置的时间间隔太大,那么会导致task的大小血缘关系过于庞大,如果需要用到窗口操作或者状态编程,那么我们就得设置好checkpoint的时间间隔,默认是不能小于10s,

dstream.checkpoint(checkpointInterval) //通常给checkpointInterval设置为(5-10)* batchDuration of DStream

部署

除了使用getOrCreate()创建一个实例外,我们还需要去确保Driver进程在failure失败之后自动重启,实现这个只能在deploy部署的时候进行配置

Configuring automatic restart of the application driver - To automatically recover from a driver failure, the deployment infrastructure that is used to run the streaming application must monitor the driver process and relaunch the driver if it fails. Different cluster managers have different tools to achieve this.

  • Spark Standalone - A Spark application driver can be submitted to run within the Spark Standalone cluster (see cluster deploy mode), that is, the application driver itself runs on one of the worker nodes. Furthermore, the Standalone cluster manager can be instructed to supervise the driver, and relaunch it if the driver fails either due to non-zero exit code, or due to failure of the node running the driver. See cluster mode and supervise in the Spark Standalone guide for more details.
  • YARN - Yarn supports a similar mechanism for automatically restarting an application. Please refer to YARN documentation for more details.
  • Mesos - Marathon has been used to achieve this with Mesos.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值