概述

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 Listener 监控批次处理延迟进行告警的更多相关文章

  1. Spark入门实战系列--7.Spark Streaming(下)--实时流计算Spark Streaming实战

    [注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 .实例演示 1.1 流数据模拟器 1.1.1 流数据说明 在实例演示中模拟实际情况,需要源源 ...

  2. Apache Spark 2.2.0 中文文档 - Spark Streaming 编程指南 | ApacheCN

    Spark Streaming 编程指南 概述 一个入门示例 基础概念 依赖 初始化 StreamingContext Discretized Streams (DStreams)(离散化流) Inp ...

  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初探

    1.  介绍 Spark Streaming是Spark生态系统中一个重要的框架,建立在Spark Core之上,与Spark SQL.GraphX.MLib相并列. Spark Streaming是 ...

  5. Apache Spark 2.2.0 中文文档 - Spark Streaming 编程指南

    Spark Streaming 编程指南 概述 一个入门示例 基础概念 依赖 初始化 StreamingContext Discretized Streams (DStreams)(离散化流) Inp ...

  6. Update(Stage4):Spark Streaming原理_运行过程_高级特性

    Spark Streaming 导读 介绍 入门 原理 操作 Table of Contents 1. Spark Streaming 介绍 2. Spark Streaming 入门 2. 原理 3 ...

  7. spark系列-8、Spark Streaming

    参考链接:http://spark.apache.org/docs/latest/streaming-programming-guide.html 一.Spark Streaming 介绍 Spark ...

  8. Spark学习之Spark Streaming

    一.简介 许多应用需要即时处理收到的数据,例如用来实时追踪页面访问统计的应用.训练机器学习模型的应用,还有自动检测异常的应用.Spark Streaming 是 Spark 为这些应用而设计的模型.它 ...

  9. Spark Streaming的简单介绍

    本文讲解Spark流数据处理之Spark Streaming.本文的写作时值Spark 1.6.2发布之际,Spark 2.0预览版也已发布,Spark发展如此迅速,请随时关注Spark Stream ...

随机推荐

  1. log4j日志打印的配置文件简单使用

    log4j.properties #将等级为DEBUG的日志信息输出到console和file这两个目的地,console和file的定义在下面的代码 log4j.rootLogger=DEBUG,c ...

  2. BZOJ2820/LG2257 YY的GCD 莫比乌斯反演

    问题描述 BZOJ2820 LG2257 题解 求 \(\sum\limits_{i=1}^{n}{\sum\limits_{j=1}^{m}{[gcd(i,j)==p]}}\) ,其中 \(p\)为 ...

  3. 在windows桌面上创建一个文件夹

    用dos命令创建 md [文件路径][文件名] C:\Users\admin>md  C:\Users\admin\desktop\test 刷新一下桌面,就可以看见桌面上创建了一个名为test ...

  4. 手把手教你制作Jlink-OB调试器(含原理图、PCB、外壳、固件)

    前言 好久没更新博客和公众号了,感谢大家还没取关哈,好吧,我承认是我太懒了,今天分享一个福利! 趁着前段时间嘉立创和捷配打价格战,一天之内,多次降价,看着真是热闹.捷配降到最低3元一款,而嘉立创降到最 ...

  5. 上海街头灵魂摄影师:勤劳de小懒熊

    上海中年大叔,街头摄影师,眼光比较独特,题材不限于: 酒吧晚上醉酒躺尸的.喝多亲嘴的.拉拉les的.流泪告别的.地铁露肉的.短裤露沟的. 尺度不大,但比较真实,艺术来源于生活,比那些摆拍的有意思. 大 ...

  6. .net core使用HttpClient发送代理请求_程序内抓包_Fiddler抓包

    前言:  通过Fiddler抓取浏览器请求数据,相信大家已经都会用了,我们知道Fiddler是通过在本机计算器添加一个默认的代理服务器来实现的抓包数据的,端口号为:8888. 其实当我们打开Fiddl ...

  7. GO语言介绍以及开发环境配置

    一.介绍 GO语言是静态强类型语言 静态也就是编译型语言 二.安装 1.下载地址 下载地址 https://golang.google.cn/dl/ 2.安装 Linux安装 1.下载二进制包:go1 ...

  8. PostgreSQL 表字段起别名

    使用Postgreq Sql 表字段起别名时注意要用双引号,使用单引号会出现语法错误,执行结果如图

  9. JavaScript基础6

    计时器 setInterval()   按照指定周期来调用函数或计算表达式     以毫秒计算 语法    setInterval(code,millisec[,“lang”]) code 要调用的函 ...

  10. [iOS开发]关于cocoapods的使用

    CocoaPods的使用 关于CocoaPods,相信做iOS开发的应该都比较熟悉了.Cocoapods是一个用来管理第三方库的比较好用的管理工具.关于Cocoapods的东西不再多说,接下来进入正题 ...