1、reduceByKeyAndWindow(_+_,Seconds(3), Seconds(2))

    可以看到我们定义的window窗口大小Seconds(3s) ,是指每2s滑动时,需要统计前3s内所有的数据。

2、对于他的重载函数reduceByKeyAndWindow(_+_,_-_,Seconds(3s),seconds(2))
     设计理念是,当 滑动窗口的时间Seconds(2) < Seconds(3)(窗口大小)时,两个统计的部分会有重复,那么我们就可以
     不用重新获取或者计算,而是通过获取旧信息来更新新的信息,这样即节省了空间又节省了内容,并且效率也大幅提升。
    
     如上图所示,2次统计重复的部分为time3对用的时间片内的数据,这样对于window1,和window2的计算可以如下所示
     win1 = time1 + time2 + time3
     win2 = time3 + time4 + time5
     
     更新为
     win1 = time1 + time2 + time3
     win2 = win1+ time4 + time5 - time2 - time3
     
     这样就理解了吧,  _+_是对新产生的时间分片(time4,time5内RDD)进行统计,而_-_是对上一个窗口中,过时的时间分片
     (time1,time2) 进行统计   

3、注意事项

/**
* Return a new DStream by applying incremental `reduceByKey` over a sliding window.
* The reduced value of over a new window is calculated using the old window's reduced value :
* 1. reduce the new values that entered the window (e.g., adding new counts)
*
* 2. "inverse reduce" the old values that left the window (e.g., subtracting old counts)
*
* This is more efficient than reduceByKeyAndWindow without "inverse reduce" function.
* However, it is applicable to only "invertible reduce functions".
* Hash partitioning is used to generate the RDDs with Spark's default number of partitions.
* @param reduceFunc associative reduce function
* @param invReduceFunc inverse reduce function
* @param windowDuration width of the window; must be a multiple of this DStream's
* batching interval
* @param slideDuration sliding interval of the window (i.e., the interval after which
* the new DStream will generate RDDs); must be a multiple of this
* DStream's batching interval
* @param filterFunc Optional function to filter expired key-value pairs;
* only pairs that satisfy the function are retained
*/
def reduceByKeyAndWindow(
reduceFunc: (V, V) => V,
invReduceFunc: (V, V) => V,
windowDuration: Duration,
slideDuration: Duration = self.slideDuration,
numPartitions: Int = ssc.sc.defaultParallelism,
filterFunc: ((K, V)) => Boolean = null
): DStream[(K, V)] = ssc.withScope {
reduceByKeyAndWindow(
reduceFunc, invReduceFunc, windowDuration,
slideDuration, defaultPartitioner(numPartitions), filterFunc
)
}

                                                                                                                                                                                          


     

SparkStreaming--reduceByKeyAndWindow的更多相关文章

  1. 【Spark篇】---SparkStreaming中算子中OutPutOperator类算子

    一.前述 SparkStreaming中的算子分为两类,一类是Transformation类算子,一类是OutPutOperator类算子. Transformation类算子updateStateB ...

  2. 【Spark篇】---SparkStreaming算子操作transform和updateStateByKey

    一.前述 今天分享一篇SparkStreaming常用的算子transform和updateStateByKey. 可以通过transform算子,对Dstream做RDD到RDD的任意操作.其实就是 ...

  3. 【SparkStreaming学习之二】 SparkStreaming算子操作

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk1.8 scala-2.10.4(依赖jdk1.8) spark ...

  4. 069 在SparkStreaming的窗口分析

    一:说明 1.图例说明 ---------------------------------------------------------------------------------------- ...

  5. SparkStreaming基本架构及使用

    1.简介 Spark Streaming处理的数据流图: Spark Streaming在内部的处理机制是,接收实时流的数据,并根据一定的时间间隔拆分成一批批的数据,然后通过Spark Engine处 ...

  6. SparkStreaming实时日志分析--实时热搜词

    Overview 整个项目的整体架构如下: 关于SparkStreaming的部分: Flume传数据到SparkStreaming:为了简单使用的是push-based的方式.这种方式可能会丢失数据 ...

  7. SparkStreaming整合kafka编程

    1.下载spark-streaming-kafka插件包 由于Linux集群环境我使用spark是spark-2.1.1-bin-hadoop2.7,kafka是kafka_2.11-0.8.2.1, ...

  8. 【大数据】SparkStreaming学习笔记

    第1章 Spark Streaming概述 1.1 Spark Streaming是什么 Spark Streaming用于流式数据的处理.Spark Streaming支持的数据输入源很多,例如:K ...

  9. Spark-Streaming总结

    文章出处:http://www.cnblogs.com/haozhengfei/p/e353daff460b01a5be13688fe1f8c952.html Spark_总结五 1.Storm 和 ...

  10. Spark之 Spark Streaming整合kafka(并演示reduceByKeyAndWindow、updateStateByKey算子使用)

    Kafka0.8版本基于receiver接受器去接受kafka topic中的数据(并演示reduceByKeyAndWindow的使用) 依赖 <dependency> <grou ...

随机推荐

  1. break

    public class b {   public static void main(String[] args) {  int i=0;  for(;i<=10;i++){   if (i&g ...

  2. 使用fckeditor上传多张图片

    流程: 1.使用fck上传图片到后台 2.后台上传图片到服务器端 3.服务器端返回上传信息 1.jsp页面 <script type="text/javascript"> ...

  3. feign的hystrix不起作用.

    在springCloud中使用feign内嵌的断路器hystrix时.feign中的hystrix不起作用.这可能是由于springCloud的版本原因造成的.需要在application.prope ...

  4. 第四章 栈与队列(c3)栈应用:栈混洗

  5. TOJ4127: Root of String

    传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=4127 4127: Root of ...

  6. js,JavaScript,a标签onclick传递参数不对,A标签调用js函数写法总结

    错误示例: <a href="javascript:waterLineEdit(${goods.goods_id})" >修改 </a> <!-- 浏 ...

  7. ES5之函数的间接调用 ( call、apply )、绑定 ( bind )

    call().apply()的第一个实参是函数调用的上下文,在函数体内通过this来获得对它的引用. call()将实参用逗号分隔:apply ()将实参放入数组.类数组对象中. function h ...

  8. expect命令自动登录ssh

    expect是简单的工具原因,依赖于tcl. 直接apt安装就行. 四个关键字: spawn,派生出新进程. expect,期待得到的字符串,可以模式匹配. send,向进程发送字符串. intera ...

  9. static 构造函数的认识

    最近,看到一道面试题,如下 class Class1 { ; static Class1() { count++; } public Class1() { count++; } } Class1 on ...

  10. RBAC 继完善代码之后的,再一次完善

    在上一篇文章中,我的中间件是 保存在我的web 业务app 中的.但是rbac我想要完成的是一个 组件的功能, 所以这个验证的 中间件,  何不放到rbac的app之中: 为了太乱先放一个项目的目录图 ...