如何优雅的关闭spark streaming实时任务

在分布式实时数据处理任务运行中,需要更新相关业务逻辑时,如何优雅的停止对应任务进程?
写个脚本将kill发送到对应任务运行节点上?这太暴力了,容易导致数据丢失问题。
在此通过监控hdfs的某个目录是否存在作为判断任务停止的依据,在需要停止时创建hdfs对应目录即可。为了下一次启动能够正常运行下去,关闭的时候还需要将对应hdfs目录删除。另外需要注意关闭标识路径需要一定的目录深度即标识度,避免与其他目录相冲突。
代码如下:

import java.net.URI

import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs.{FileSystem, Path}
import org.apache.spark.streaming.{StreamingContext, StreamingContextState}

class MonitorForGracefulStop(ssc: StreamingContext) extends Runnable {

  override def run(): Unit = {

    val fs: FileSystem = FileSystem.get(new URI("hdfs://chdp11:9000"), new Configuration(), "root")

    while (true) {
      try{
        //most case using 20 seconds rough, this just for test
        Thread.sleep(5000)
      }
      catch {
        case e: InterruptedException =>
          e.printStackTrace()
      }
      val state: StreamingContextState = ssc.getState

      val bool: Boolean = fs.exists(new Path("/spark/monitor/streaming/stopStreaming"))

      if (bool) {
        if (state == StreamingContextState.ACTIVE) {
          ssc.stop(stopSparkContext = true, stopGracefully = true)
          println("stop gracefull by MonitorForGracefullStop!")
          //delete tag directory for next start
          fs.delete(new Path( "/spark/monitor/streaming/stopStreaming"),true)
          System.exit(0)
        }
      }
    }
  }
}

使用时注意需要添加如下配置(注意代码位置)

 ssc.start()
 //invoke stop graceful thread
 new MonitorForGracefulStop(ssc).run()
 ssc.awaitTermination()

在这里插入图片描述
Spark会在JVM关闭时正常关闭StreamingContext,而不是立马关闭。
或者使用以下方法:
Kill 命令:yarn application -kill 后面跟 applicationid

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值