SparkStream:3)updateStateByKey详解

来源:互联网 发布:c语言qsort函数 编辑:程序博客网 时间:2024/06/04 23:18

SparkStream官方文档对updateStateByKey这个函数介绍过于粗略,在网上看见了一篇不错的文章就转载了过来


在实时计算的实际应用中,有时除了需要关心一个时间间隔内的数据,有时还可能会对整个实时计算的所有时间间隔内产生的相关数据进行统计。

比如: 对Nginx的access.log实时监控请求404时,有时除了需要统计某个时间间隔内出现的次数,有时还需要统计一整天出现了多少次404,也就是说404监控横跨多个时间间隔。

 

Spark Streaming的解决方案是累加器,工作原理是,定义一个类似全局的可更新的变量,每个时间窗口内得到的统计值都累加到上个时间窗口得到的值,这样这个累加值就是横跨多个时间间隔

 

Java代码  收藏代码
  1. package spark.examples.streaming  
  2.   
  3. import org.apache.spark.SparkConf  
  4. import org.apache.spark.HashPartitioner  
  5. import org.apache.spark.streaming._  
  6.   
  7. /** 
  8.  * Counts words cumulatively in UTF8 encoded, '\n' delimited text received from the network every 
  9.  * second starting with initial value of word count. 
  10.  * Usage: StatefulNetworkWordCount <hostname> <port> 
  11.  * <hostname> and <port> describe the TCP server that Spark Streaming would connect to receive 
  12.  * data. 
  13.  * 
  14.  * To run this on your local machine, you need to first run a Netcat server 
  15.  * `$ nc -lk 9999` 
  16.  * and then run the example 
  17.  * `$ bin/run-example 
  18.  * org.apache.spark.examples.streaming.StatefulNetworkWordCount localhost 9999` 
  19.  */  
  20. object StatefulNetworkWordCount {  
  21.   def main(args: Array[String]) {  
  22.   
  23.     ///函数常量定义,返回类型是Some(Int),表示的含义是最新状态  
  24.     ///函数的功能是将当前时间间隔内产生的Key的value集合,加到上一个状态中,得到最新状态  
  25.     val updateFunc = (values: Seq[Int], state: Option[Int]) => {  
  26.       val currentCount = values.sum  
  27.       val previousCount = state.getOrElse(0)  
  28.       Some(currentCount + previousCount)  
  29.     }  
  30.   
  31.     ///入参是三元组遍历器,三个元组分别表示Key、当前时间间隔内产生的对应于Key的Value集合、上一个时间点的状态  
  32.     ///newUpdateFunc的返回值要求是iterator[(String,Int)]类型的  
  33.     val newUpdateFunc = (iterator: Iterator[(String, Seq[Int], Option[Int])]) => {  
  34.       ///对每个Key调用updateFunc函数(入参是当前时间间隔内产生的对应于Key的Value集合、上一个时间点的状态)得到最新状态  
  35.       ///然后将最新状态映射为Key和最新状态  
  36.       iterator.flatMap(t => updateFunc(t._2, t._3).map(s => (t._1, s)))  
  37.     }  
  38.   
  39.     val sparkConf = new SparkConf().setAppName("StatefulNetworkWordCount").setMaster("local[3]")  
  40.     // Create the context with a 5 second batch size  
  41.     val ssc = new StreamingContext(sparkConf, Seconds(5))  
  42.   
  43.     ssc.checkpoint(".")  
  44.     // Initial RDD input to updateStateByKey  
  45.     val initialRDD = ssc.sparkContext.parallelize(List(("hello"1), ("world"1)))  
  46.     // Create a ReceiverInputDStream on target ip:port and count the  
  47.     // words in input stream of \n delimited test (eg. generated by 'nc')  
  48.     val lines = ssc.socketTextStream("192.168.26.140"9999)  
  49.     val words = lines.flatMap(_.split(" "))  
  50.     val wordDstream = words.map(x => (x, 1))  
  51.       
  52.     // Update the cumulative count using updateStateByKey  
  53.     // This will give a Dstream made of state (which is the cumulative count of the words)  
  54.     //注意updateStateByKey的四个参数,第一个参数是状态更新函数  
  55.     val stateDstream = wordDstream.updateStateByKey[Int](newUpdateFunc,  
  56.       new HashPartitioner(ssc.sparkContext.defaultParallelism), true, initialRDD)  
  57.     stateDstream.print()  
  58.     ssc.start()  
  59.     ssc.awaitTermination()  
  60.   }  
  61. }  

 

上面的核心操作时DStream的updateStateByKey函数操作,它接受四个参数,

 

2. DStream.updateStateByKey剖析

方法说明:

Java代码  收藏代码
  1. /** 
  2.   * Return a new "state" DStream where the state for each key is updated by applying 
  3.   * the given function on the previous state of the key and the new values of each key. 
  4.   * org.apache.spark.Partitioner is used to control the partitioning of each RDD. 
  5.   * @param updateFunc State update function. Note, that this function may generate a different 
  6.   *                   tuple with a different key than the input key. Therefore keys may be removed 
  7.   *                   or added in this way. It is up to the developer to decide whether to 
  8.   *                   remember the  partitioner despite the key being changed. 
  9.   * @param partitioner Partitioner for controlling the partitioning of each RDD in the new 
  10.   *                    DStream 
  11.   * @param rememberPartitioner Whether to remember the paritioner object in the generated RDDs. 
  12.   * @param initialRDD initial state value of each key. 
  13.   * @tparam S State type 
  14.   */  
  15.  def updateStateByKey[S: ClassTag](  
  16.      updateFunc: (Iterator[(K, Seq[V], Option[S])]) => Iterator[(K, S)],  
  17.      partitioner: Partitioner,   
  18.      rememberPartitioner: Boolean,  
  19.      initialRDD: RDD[(K, S)]  
  20.    ): DStream[(K, S)] = {  
  21.     new StateDStream(self, ssc.sc.clean(updateFunc), partitioner,  
  22.       rememberPartitioner, Some(initialRDD))  
  23.  }  
  • initialRDD是(K,S)类型的RDD,它表示一组Key的初始状态,每个(K,S)表示一个Key以及它对应的State状态。 K表示updateStateByKey的Key的类型,比如String,而S表示Key对应的状态(State)类型,在上例中,是Int
  • rememberPartitioner: 表示是否在接下来的Spark Streaming执行过程中产生的RDD使用相同的分区算法
  • partitioner: 分区算法,上例中使用的Hash分区算法,分区数为ssc.sparkContext.defaultParallelism
  • updateFunc是函数常量,类型为(Iterator[(K, Seq[V], Option[S])]) => Iterator[(K, S)],表示状态更新函数

(Iterator[(K, Seq[V], Option[S])]) => Iterator[(K, S)]如何解读?

入参: 三元组迭代器,三元组中K表示Key,Seq[V]表示一个时间间隔中产生的Key对应的Value集合(Seq类型,需要对这个集合定义累加函数逻辑进行累加),Option[S]表示上个时间间隔的累加值(表示这个Key上个时间点的状态)

出参:二元组迭代器,二元组中K表示Key,S表示当前时间点执行结束后,得到的累加值(即最新状态)


转载自:http://bit1129.iteye.com/blog/2198682

0 0