SparkStreaming 状态计算 (updateStateByKey mapWithState)区别

SparkStreaming 状态计算 (updateStateByKey mapWithState) 优缺点

updateStateByKey 算子

返回的是带有状态的DStream,在这个DStream里面,每一个key的状态是可以被更新的,通过一个给定的函数,把之前的key的状态和当前key新的状态联合起来计算,用于维持每个key的任意状态。说白了也就是说每一次的老的计算状态都给记录下来,新的计算状态用于去更新老的状态。

也即是说它会统计全局的key的状态,就算没有数据输入,它也会在每一个批次的时候返回之前的key的状态。
缺点:若数据量太大的话,需要checkpoint的数据会占用较大的存储,效率低下。

两步骤:

  1. Define the state - The state can be an arbitrary data type.
  2. Define the state update function - Specify with a function how to update the state using the previous state and the new values from an input stream.
object SparkStreamingState02 {

  val checkPointPath = "." // 将checkpoint 保存到当前目录

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


    /*
    根据checkpoint数据重新创建StreamingContext或创建新的StreamingContext。
    如果提供的`checkpointPath`中存在checkpoint数据,则将根据检查点数据重新创建StreamingContext。
    如果数据不存在,则将通过调用提供的 方法 来创建StreamingContext
     */
    val ssc = StreamingContext.getOrCreate(checkPointPath,CreatStreamingContext)

    ssc.start()
    ssc.awaitTermination()
  }


  def CreatStreamingContext() ={
    val sparkConf = new SparkConf().setMaster("local[2]").setAppName(this.getClass.getSimpleName)
    val ssc = new StreamingContext(sparkConf,Seconds(5))
    ssc.checkpoint(checkPointPath)

    val lines = ssc.socketTextStream("hadoop001",9999)
    val wc = lines.flatMap(_.split(",")).map((_,1)).updateStateByKey(updateFunction)

    wc.print()
    ssc
  }


  /**
    *
    * @param newValues  当前批次的值
    *         key对应的新值 可能有多个 所以就是一个 seq
    *
    * @param oldValue   之前批次的值
    *         key已经存在的值  有可能没有,有可能有,所以定义成 Option
    * @return
    */
  def updateFunction(newValues: Seq[Int], oldValue: Option[Int]): Option[Int] = {
    var newCount = newValues.sum

    newCount += oldValue.getOrElse(0)

    Some(newCount)
  }

}

不论是否重启,程序的运行状态都保存完好

-------------------------------------------
Time: 1572245440000 ms
-------------------------------------------
(d,7)
(b,6)
(f,4)
(e,4)
(a,6)
(g,7)
(c,6)
-------------------------------------------
Time: 1572245445000 ms
-------------------------------------------
(d,7)
(b,6)
(f,4)
(e,4)
(a,6)
(g,7)
(c,6)

mapWithState (生产中推荐使用)

mapWithState:也是用于全局统计key的状态,但是它如果没有数据输入,便不会返回之前的key的状态,有一点增量的感觉。效率更高,生产中建议使用

优点:我们可以只是关心那些已经发生的变化的key,对于没有数据输入,则不会返回那些没有变化的key的数据。这样的话,即使数据量很大,checkpoint也不会像updateStateByKey那样,占用太多的存储

object SparkStreamingMapState {

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

    val sparkConf = new SparkConf().setMaster("local[2]").setAppName(this.getClass.getSimpleName)
    val ssc = new StreamingContext(sparkConf,Seconds(5))
    ssc.checkpoint(".")

	// 初始状态RDD
    val initialRDD = ssc.sparkContext.parallelize(List(("hello", 1), ("world", 1)))



    val lines = ssc.socketTextStream("tool",9999)
    val wc = lines.flatMap(_.split(",")).map((_,1))


    val spec = StateSpec.function(mappingFunction _)//.numPartitions(ssc.sparkContext.defaultParallelism)
    val stateDstream = wc.mapWithState(spec.initialState(initialRDD)).stateSnapshots()

    stateDstream.print()


    ssc.start()
    ssc.awaitTermination()
  }


  /**
    *
    * @param key
    * @param value 当前值
    * @param state 历史状态
    * @return mapWithState 函数最终返回的格式
    */
  def mappingFunction(key: String, value: Option[Int], state: State[Int]) = {
    // Use state.exists(), state.get(), state.update() and state.remove()
    // to manage state, and return the necessary string
    val sum = value.getOrElse(0) + state.getOption().getOrElse(0)
    // 更新 state
    state.update(sum)
    // 返回 mappingdata
    Some(key)
  }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冬瓜螺旋雪碧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值