Spark Streaming之一:StreamingContext解析

本文介绍了Spark Streaming中StreamingContext的创建方法,包括通过SparkContext、SparkConf、参数及checkpointfile创建。并讲解了如何使用Dstream监听不同类型的输入源如fileStream、textFileStream、socketTextStream等。最后讨论了Checkpointing在状态操作中的重要性,提供了设置检查点的示例。
摘要由CSDN通过智能技术生成

1.0 前言

目前Spark Streaming编程指南地址:

http://spark.apache.org/docs/latest/streaming-programming-guide.html


1.1 创建StreamingContext对象

1.1.1通过SparkContext创建

源码如下:

def this(sparkContext:SparkContext, batchDuration: Duration) = {

   this(sparkContext,null,batchDuration)

  }

第一参数为sparkContext对象,第二个参数为批次时间;

创建实例:

val ssc = new StreamingContext(sc, Seconds(5))

 

1.1.2通过SparkConf创建

源码如下:

 defthis(conf:SparkConf, batchDuration: Duration) = {

   this(StreamingContext.createNewSparkContext(conf),null,batchDuration)

  }

第一参数为SparkConf对象,第二个参数为批次时间;

创建实例:

   valconf =newSparkConf().setAppName("StreamTest")

val ssc = newStreamingContext(conf,Seconds(5))

 

1.1.3通过SparkConf参数创建

源码如下:

 defthis(

     master: String,

     appName: String,

     batchDuration: Duration,

     sparkHome: String = null,

     jars: Seq[String] = Nil,

     environment: Map[String, String] = Map()) = {

   this(StreamingContext.createNewSparkContext(master,appName, sparkHome, jars, environment),

         null, batchDuration)

  }

第一参数为需要创建SparkConf对象的详细参数,master-spark地址,appName-对象名称,sparkHome- sparkHome环境变量,jars, environment,第二个参数为批次时间;

创建实例:

val ssc = newStreamingContext(“ spark://host:port”, "StreamTest", Seconds(5),      System.getenv("SPARK_HOME"),StreamingContext.jarOfClass(this.getClass))

 

1.1.4通过checkpointfile参数创建

源码如下:

 defthis(path:String, hadoopConf: Configuration) =

    this(null, CheckpointReader.read(path,new SparkConf(), hadoopConf).get,null)

第一参数为checkpoint file的路径,第二个参数为haoop的配置

源码如下:

def this(path:String) = this(path,newConfiguration)

第一参数为checkpoint file的路径

 

1.2创建Dstream监听对象

1.2.1 fileStream

源码如下:

/**

  * Create a input stream that monitors a Hadoop-compatible filesystem

  * for new files and reads them using the given key-value types and inputformat.

  * Files must be written to the monitored directory by "moving"them from another

  * location within the same file system. File names starting with . areignored.

  * @param directory HDFS directory to monitor for new file

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值