spark streaming源码分析1 StreamingContext


博客地址: http://blog.csdn.net/yueqian_zhu/


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

object NetworkWordCount {
  def main(args: Array[String]) {
    if (args.length < 2) {
      System.err.println("Usage: NetworkWordCount <hostname> <port>")
      System.exit(1)
    }

    StreamingExamples.setStreamingLogLevels()

    // Create the context with a 1 second batch size
    val sparkConf = new SparkConf().setAppName("NetworkWordCount")
    val ssc = new StreamingContext(sparkConf, Seconds(1))

    // Create a socket stream on target ip:port and count the
    // words in input stream of \n delimited text (eg. generated by 'nc')
    // Note that no duplication in storage level only for running locally.
    // Replication necessary in distributed scenario for fault tolerance.
    val lines = ssc.socketTextStream(args(0), args(1).toInt, StorageLevel.MEMORY_AND_DISK_SER)
    val words = lines.flatMap(_.split(" "))
    val wordCounts = words.map(x => (x, 1)).reduceByKey(_ + _)
    wordCounts.print()
    ssc.start()
    ssc.awaitTermination()
  }
}
本小节主要介绍StreamingContext的构造

class StreamingContext private[streaming] (
    sc_ : SparkContext,
    cp_ : Checkpoint,
    batchDur_ : Duration
  )

一、API:

1、cp_为null

def this(sparkContext: SparkContext, batchDuration: Duration)
2、方法内部也是通过conf自动创建一个sparkContext,cp_为null
def this(conf: SparkConf, batchDuration: Duration)
3、conf由默认的和参数部分组合而成,cp_为null
def this(
    master: String,
    appName: String,
    batchDuration: Duration,
    sparkHome: String = null,
    jars: Seq[String] = Nil,
    environment: Map[String, String] = Map())
4、从path目录下读取checkpoint的信息来重建streamingContext,也就不需要sparkContext和Duration参数
def this(path: String, hadoopConf: Configuration)
def this(path: String)//hadoopConf使用默认的hadoop配置文件自动构造
5、使用存在的sparkContext和checkpoint路径来构造
def this(path: String, sparkContext: SparkContext)
6、需要注意的是,streamingContext对象内部有一个getOrCreate方法,指明如果在checkpointPath路径下读取不到,则调用creatingFunc创建新的streamingContext
def getOrCreate(
    checkpointPath: String,
    creatingFunc: () => StreamingContext,
    hadoopConf: Configuration = new Configuration(),
    createOnError: Boolean = false
  ): 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部分

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spark StreamingSpark的一个扩展库,提供了实时流处理的能力。它的源码可以在Apache Spark的官方仓库中找到:https://github.com/apache/spark。 在源码中,Spark Streaming的主要实现位于`streaming`子目录中。其中,最重要的文件是`StreamingContext.scala`,它定义了Spark Streaming的核心类`StreamingContext`,用于创建和配置Spark Streaming应用程序。其他重要的文件包括: - `DStream.scala`:定义了DStream(离散流)的抽象类,它是Spark Streaming中最基本的抽象概念,表示一个连续的、无界的数据流。 - `InputDStream.scala`:定义了InputDStream(输入离散流)的抽象类,它是所有输入流的基类。 - `ReceiverInputDStream.scala`:定义了ReceiverInputDStream(接收器输入离散流)的类,它表示通过接收器从数据源中获取数据的输入流。 - `DirectKafkaInputDStream.scala`:定义了DirectKafkaInputDStream(直接Kafka输入离散流)的类,它表示直接从Kafka中获取数据的输入流。 - `TransformedDStream.scala`:定义了TransformedDStream(转换离散流)的抽象类,它表示对一个DStream进行转换操作后得到的新的DStream。 除了上述文件之外,还有许多与Spark Streaming相关的文件,如`streaming.kafka`、`streaming.flume`、`streaming.twitter`等,它们分别实现了与Kafka、Flume、Twitter等数据源的集成。此外,还有一些与内部实现相关的文件,如`streaming.scheduler`、`streaming.util`等。 总体来说,Spark Streaming源码结构清晰,代码质量高,是一个很好的学习实时流处理的资源。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值