概述

StreamingListener 是针对spark streaming的各个阶段的事件监听机制。

StreamingListener接口

//需要监听spark streaming中各个阶段的事件只需实现这个特质中对应的事件函数即可
//本身既有注释说明
trait StreamingListener { /** Called when the streaming has been started */
/** streaming 启动的事件 */
def onStreamingStarted(streamingStarted: StreamingListenerStreamingStarted) { } /** Called when a receiver has been started */
/** 接收启动事件 */
def onReceiverStarted(receiverStarted: StreamingListenerReceiverStarted) { } /** Called when a receiver has reported an error */
def onReceiverError(receiverError: StreamingListenerReceiverError) { } /** Called when a receiver has been stopped */
def onReceiverStopped(receiverStopped: StreamingListenerReceiverStopped) { } /** Called when a batch of jobs has been submitted for processing. */
/** 每个批次提交的事件 */
def onBatchSubmitted(batchSubmitted: StreamingListenerBatchSubmitted) { } /** Called when processing of a batch of jobs has started. */
/** 每个批次启动的事件 */
def onBatchStarted(batchStarted: StreamingListenerBatchStarted) { } /** Called when processing of a batch of jobs has completed. */
/** 每个批次完成的事件 */
def onBatchCompleted(batchCompleted: StreamingListenerBatchCompleted) { } /** Called when processing of a job of a batch has started. */
def onOutputOperationStarted(
outputOperationStarted: StreamingListenerOutputOperationStarted) { } /** Called when processing of a job of a batch has completed. */
def onOutputOperationCompleted(
outputOperationCompleted: StreamingListenerOutputOperationCompleted) { }
}

自定义StreamingListener

功能:监控批次处理时间,若超过阈值则告警,每次告警间隔2分钟

class SparkStreamingDelayListener(private val appName:String, private val duration: Int,private val times: Int) extends StreamingListener{

  private val logger = LoggerFactory.getLogger("SparkStreamingDelayListener")

//每个批次完成时执行
override def onBatchCompleted(batchCompleted: StreamingListenerBatchCompleted): Unit = {
val batchInfo = batchCompleted.batchInfo
val processingStartTime = batchCompleted.batchInfo.processingStartTime
val numRecords = batchCompleted.batchInfo.numRecords
val processingEndTime = batchInfo.processingEndTime
val processingDelay = batchInfo.processingDelay
val totalDelay = batchInfo.totalDelay //将每次告警时间写入redis,用以判断告警间隔大于2分钟
val jedis = RedisClusterClient.getJedisClusterClient()
val current_time = (System.currentTimeMillis / 1000).toInt
val redis_time = jedis.get(appName)
var flag = false
if(redis_time==null || current_time-redis_time.toInt>120){
jedis.set(appName,current_time.toString)
flag = true
} //若批次处理延迟大于批次时长指定倍数,并且告警间隔大约2分钟,则告警
if(totalDelay.get >= times * duration * 1000 && flag){
val monitorContent = appName+": numRecords ->"+numRecords+",processingDelay ->"+processingDelay.get/1000+" s,totalDelay -> "+totalDelay.get/1000+"s"
println(monitorContent)
val msg = "Streaming_"+appName+"_DelayTime:"+totalDelay.get/1000+"S"
val getURL = "http://node1:8002/message/weixin?msg="+msg
HttpClient.doGet(getURL)
}
}
}

应用

//streamingListener不需要在配置中设置,可以直接添加到streamingContext中
object My{
def main(args : Array[String]) : Unit = {
val sparkConf = new SparkConf()
val ssc = new StreamingContext(sparkConf,Seconds(20))
ssc.addStreamingListener(new SparkStreamingDelayListener("Userid2Redis", duration,times)) ....
}
}

订阅关注微信公众号《大数据技术进阶》,及时获取更多大数据架构和应用相关技术文章!

Spark Streaming任务延迟监控及告警的更多相关文章

  1. 【转】Spark Streaming 实时计算在甜橙金融监控系统中的应用及优化

    系统架构介绍 整个实时监控系统的架构是先由 Flume 收集服务器产生的日志 Log 和前端埋点数据, 然后实时把这些信息发送到 Kafka 分布式发布订阅消息系统,接着由 Spark Streami ...

  2. Spark Streaming编程指南

    Overview A Quick Example Basic Concepts Linking Initializing StreamingContext Discretized Streams (D ...

  3. 大数据技术之_19_Spark学习_04_Spark Streaming 应用解析 + Spark Streaming 概述、运行、解析 + DStream 的输入、转换、输出 + 优化

    第1章 Spark Streaming 概述1.1 什么是 Spark Streaming1.2 为什么要学习 Spark Streaming1.3 Spark 与 Storm 的对比第2章 运行 S ...

  4. Spark Streaming核心概念与编程

    Spark Streaming核心概念与编程 1. 核心概念 StreamingContext Create StreamingContext import org.apache.spark._ im ...

  5. 4. Spark Streaming解析

    4.1 初始化StreamingContext import org.apache.spark._ import org.apache.spark.streaming._ val conf = new ...

  6. 大数据开发实战:Spark Streaming流计算开发

    1.背景介绍 Storm以及离线数据平台的MapReduce和Hive构成了Hadoop生态对实时和离线数据处理的一套完整处理解决方案.除了此套解决方案之外,还有一种非常流行的而且完整的离线和 实时数 ...

  7. 【Streaming】30分钟概览Spark Streaming 实时计算

    本文主要介绍四个问题: 什么是Spark Streaming实时计算? Spark实时计算原理流程是什么? Spark 2.X下一代实时计算框架Structured Streaming Spark S ...

  8. Spark(五) -- Spark Streaming介绍与基本执行过程

    Spark Streaming作为Spark上的四大子框架之一,肩负着实时流计算的重大责任 而相对于另外一个当下十分流行的实时流计算处理框架Storm,Spark Streaming有何优点?又有何不 ...

  9. Spark Streaming Listener 监控批次处理延迟进行告警

    概述 StreamingListener 是针对spark streaming的各个阶段的事件监听机制. StreamingListener接口 //需要监听spark streaming中各个阶段的 ...

随机推荐

  1. 关于小码哥kylin

    技术格言: 用双手改变人生,用代码改变世界! 个人网站:http://www.isainttech.com QQ:56619556 Email:dragonsaint@qq.com 微信:kylin1 ...

  2. appium自动化的一个实例

    实现appium的自动化,三步走,具体如下: 第一步:启动appium的服务端: 可以通过命令行的方式启动:cmd,然后输入appium,如下图 也可以打开桌面程序appium,点击右上角的运行按钮, ...

  3. javascript获取当前时间CurentTime

    function CurentTime(){ var now = new Date(); var year = now.getFullYear(); //年 var month = now.getMo ...

  4. uni-app之网络请求

    uni-app之网络请求 一,介绍 uni.request(OBJECT),发起网络请求,以下主要是一些特殊的参数说明,详细的可查看uni-app官网. 1,method的有效值必须是大写,默认GET ...

  5. C#程序编写高质量代码改善的157个建议【13-15】[为类型输出格式化字符串、实现浅拷贝和深拷贝、用dynamic来优化反射]

    前言 本文已更新至http://www.cnblogs.com/aehyok/p/3624579.html .本文主要学习记录以下内容: 建议13.为类型输出格式化字符串 建议14.正确实现浅拷贝和深 ...

  6. Python使用psutil模块,做你的电脑管家

    电脑管家 也许大家都有这样的感觉,优化完美的电脑系统,你把电脑借给一个电脑小白使用上几天,等你拿回来的时候会发现,开机各种慢,乱七八糟的软件装了一大堆.那么我们如何使用Python来获取电脑的相关数据 ...

  7. 《跟唐老师学习云网络》 -第5篇 Ping喂报文

    [摘要] 这一章节你的角色是国王,你要派一个小兵去对方打探一下.是站在你的角度看这个小兵.哦,对了,这个小兵的名字叫"喂". 一.Ping命令介绍 ping就是用来检测一下网络能不 ...

  8. [TimLinux] JavaScript 引用类型——Date

    1. Date var now = new Date(); // 不传参数,获取当前日期.时间. now.getDay(); // 日期 now.getMonth(); // 月份 now.getFu ...

  9. 常用eslint配置

    "off"或者0 //关闭规则关闭 "warn"或者1 //在打开的规则作为警告(不影响退出代码) "error"或者2 //把规则作为一个 ...

  10. hibernate mysql中文检出无效

    在学习ssh框架是发现,检索条件是英文时,sql就能按照条件过滤出数据,当我换成中文是,检索出来的数据就是空,最后发现没有设置数据库连接url的编码格式 1.数据库编码 COLLATE='utf8_g ...