spark streaming源码分析1 StreamingContext

首先看一个最简单的例子,了解大致的样子:

[java]  view plain  copy
  1. object NetworkWordCount {  
  2.   def main(args: Array[String]) {  
  3.     if (args.length < 2) {  
  4.       System.err.println("Usage: NetworkWordCount <hostname> <port>")  
  5.       System.exit(1)  
  6.     }  
  7.   
  8.     StreamingExamples.setStreamingLogLevels()  
  9.   
  10.     // Create the context with a 1 second batch size  
  11.     val sparkConf = new SparkConf().setAppName("NetworkWordCount")  
  12.     val ssc = new StreamingContext(sparkConf, Seconds(1))  
  13.   
  14.     // Create a socket stream on target ip:port and count the  
  15.     // words in input stream of \n delimited text (eg. generated by 'nc')  
  16.     // Note that no duplication in storage level only for running locally.  
  17.     // Replication necessary in distributed scenario for fault tolerance.  
  18.     val lines = ssc.socketTextStream(args(0), args(1).toInt, StorageLevel.MEMORY_AND_DISK_SER)  
  19.     val words = lines.flatMap(_.split(" "))  
  20.     val wordCounts = words.map(x => (x, 1)).reduceByKey(_ + _)  
  21.     wordCounts.print()  
  22.     ssc.start()  
  23.     ssc.awaitTermination()  
  24.   }  
  25. }  
本小节主要介绍StreamingContext的构造

[java]  view plain  copy
  1. class StreamingContext private[streaming] (  
  2.     sc_ : SparkContext,  
  3.     cp_ : Checkpoint,  
  4.     batchDur_ : Duration  
  5.   )  

一、API:

1、cp_为null

 
 
[java] view plain copy
  1. def this(sparkContext: SparkContext, batchDuration: Duration)  
2、方法内部也是通过conf自动创建一个sparkContext,cp_为null
  
  
[java] view plain copy
  1. def this(conf: SparkConf, batchDuration: Duration)  
3、conf由默认的和参数部分组合而成,cp_为null
   
   
[java] view plain copy
  1. def this(  
  2.     master: String,  
  3.     appName: String,  
  4.     batchDuration: Duration,  
  5.     sparkHome: String = null,  
  6.     jars: Seq[String] = Nil,  
  7.     environment: Map[String, String] = Map())  
4、从path目录下读取checkpoint的信息来重建streamingContext,也就不需要sparkContext和Duration参数
    
    
[java] view plain copy
  1. def this(path: String, hadoopConf: Configuration)  
[java] view plain copy
  1. def this(path: String)//hadoopConf使用默认的hadoop配置文件自动构造  
5、使用存在的sparkContext和checkpoint路径来构造
     
     
[java] view plain copy
  1. def this(path: String, sparkContext: SparkContext)  
6、需要注意的是,streamingContext对象内部有一个getOrCreate方法,指明如果在checkpointPath路径下读取不到,则调用creatingFunc创建新的streamingContext
       
       
[java] view plain copy
  1. def getOrCreate(  
  2.     checkpointPath: String,  
  3.     creatingFunc: () => StreamingContext,  
  4.     hadoopConf: Configuration = new Configuration(),  
  5.     createOnError: Boolean = false  
  6.   ): StreamingContext  
二、StreamingContext主要的构造逻辑(checkpoint暂不讨论)
1、构造一个graph: DStreamGraph
作用于DStream上的operation分成两类 1. Transformation,2. Output 表示将输出结果。DStreamGraph 有输入就要有输出,如果没有输出,则前面所做的所有动作全部没有意义,那么如何将这些输入和输出绑定起来呢?这个问题的解决就依赖于DStreamGraph,DStreamGraph记录输入的Stream和输出的Stream。
2、构造一个JobScheduler
JobScheduler内部会构造一个jobGenerator,它用于按我们设定的批处理间隔产生job
3、状态设置为INITIALIZED
下一节介绍上面例子中的operation部分
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值