Spark Streaming 将接收到的实时流数据,按照一定时间间隔,对数据进行拆分,交给 Spark Engine 引擎,最终得到一批批的结果。
DStream:Spark Streaming 提供了表示连续数据流的、高度抽象的被称为离散流的 DStream。
假如外部数据不断涌入,按照一分钟切片,每个一分钟内部的数据是连续的(连续数据流),而一分钟与一分钟的切片却是相互独立的(离散流)。
- DStream 是 Spark Streaming 特有的数据类型
- Spark 2.3.1 开始延迟1毫秒(之前约100毫秒)
- Each micro-batch is an RDD – can share code between batch and streaming
四、工作原理
4.1 Streaming Context
Streaming Context consumes a stream of data in Spark.
from pyspark import SparkContext
from pyspark.streaming import StreamingContext
# Create a local StreamingContext with two working threads and a batch interval of 2 seconds
# local[2] – spark executor runs on 2 cores.
sc = StreamingContext( SparkContext("local[2]", "NetWordCount"), 2 )
# ….
sc.start()
- JVM中只能激活一个StreamingContext。
- StreamingContext不能在停止后重新启动,但是可以重新创建。
4.2 DStream
DStream 离散流由一系列连续的RDD组成,每个RDD都包含了确定时间间隔内的数据:
Spark 的 RDD 可以理解为空间维度,Dstream 的 RDD 理解为在空间维度上又加了个时间维度。
例如上图,数据流进切分为四个分片,内部处理逻辑都是相同的,只是时间维度不同。
# Create a local StreamingContext with two working threads and a batch interval of 2 seconds
sc = StreamingContext( SparkContext("local[2]", "NetWordCount"), 2 )
lines = ssc.socketTextStream("localhost", 3333) # Create a DStream
words = lines.flatMap(lambda line: line.split(" ")) # Split each line into words
pairs = words.map(lambda word: (word, 1)) # Count each word in each batch
wordCounts = pairs.reduceByKey(lambda x, y: x + y)
ssc.start() # Start the computation
ssc.awaitTermination() # Wait for the computation to terminate
Spark 与 Spark Streaming 区别:
Spark -> RDD:transformation action + RDD DAG
Spark Streaming -> Dstream:transformation output(它不能让数据在中间激活,必须保证数据有输入有输出) + DStreamGraph
任何对DStream的操作都会转变为对底层RDD的操作(通过算子):
总结:将连续的数据持久化,离散化,然后进行批量处理。
- 持久化:接收到的数据暂存。
为什么持久化:做容错的,当数据流出错了,因为没有得到计算,需要把数据从源头进行回溯,暂存的数据可以进行恢复。
- 离散化:按时间分片,形成处理单元。
分片处理:分批处理。
4.3 Input DStreams & Receivers
- Input DStreams represent the stream of input data received from streaming sources.
- 每个Input DStream(文件流除外)都与 Receiver 接收方对象相关联,接收方对象从源接收数据并将其存储在Spark内存中进行处理。
- 可以在相同的 StreamingContext 下创建多个 Input DStreams
五、DStream 操作
1.1 普通的转换操作:map、flatMap、flter、union、count、join等
1.2 transform(func)操作:允许DStream 上应用任意RDD-to-RDD函数
1.3 updateStateByKey操作:
1.4 窗口转换操作: 允许你通过滑动窗口对数据进行转换,如countByWindow、 reduceByKeyAndWindow等,(批处理间隔、窗口间隔和滑动间隔)
2.输出操作:允许DStream的数据被输出到外部系统,如数据库或文件系统,有print()、foreachRDD(func)、saveAsTextFiles()、 saveAsHadoopFiles()等
3.持久化:通过persist()方法将数据流存放在内存中,有利于高效的迭代运算
六、Spark Streaming 架构
Master:记录Dstream之间的依赖关系或者血缘关系,并负责任务调度以生成新的RDD
Worker:从网络接收数据,存储并执行RDD计算
Client:负责向Spark Streaming中灌入数据
调度:按照时间触发。
Master:维护了DStream Graph这张图。(不是节点级别的,是任务级别的)
Worker:按照图去执行。
Worker 里面有个重要的角色:receiver,接收外部数据流,然后数据流通过 receiver 传入整个 Spark Streaming 内部( receiver 最终把数据流包装成 Spark Streaming 能处理的格式)
receiver:接收器,接收不同的数据源,进行针对性的获取,Spark Streaming 也提供了不同的接收器分布在不同的节点上,每个接收器都是一个特定的进程,每个节点接收一部分作为输入。,receiver接受完不马上做计算,先存储到它的内部缓存区。因为Streaming 是按照时间不断的分片,所以需要等待,一旦定时器到时间了,缓冲区就会把数据转换成数据块block(缓冲区的作用:按照用户定义的时间间隔切割),然后把数据块放到一个队列里面去,然后Block manager从队列中把数据块拿出来,把数据块转换成一个spark能处理的数据块。
为什么是一个进程?
container -> Executor 所以是一个进程
Spark Streaming 作业提交:
• Network Input Tracker:跟踪每一个网络received数据,并且将其映射到相应的input Dstream上
• Job Scheduler:周期性的访问DStream Graph并生成Spark Job,将其交给Job Manager执行
• Job Manager:获取任务队列,并执行Spark任务
Spark Streaming 窗口操作:
• Spark 提供了一组窗口操作,通过滑动窗口技术对大规模数据的增量更新进行统计分析
• Window Operation:定时进行一定时间段内的数据处理
任何基于窗口操作需要指定两个参数:
- 窗口总长度(window length):你想计算多长时间的数据
- 滑动时间间隔(slide interval):你每多长时间去更新一次
七、Key Points for InputStream
- When running Spark-Streaming program locally, always use
“local[n]” as the master URL, where n > number of
receivers; - When running on a cluster, the number of cores allocated to
the Spark Streaming application must be more than the
number of receivers.
八、Sources of Spark Streaming
- Spark StreamingContext has the following built-in Support for creating
Streaming Sources:
def textFileStream(directory: String): DStream[String]
Process files in directory – hdfs://namenode:8020/logs/
def socketTextStream(hostname: String, port: Int, storageLevel: StorageLevel
StorageLevel.MEMORY_AND_DISK_SER_2): ReceiverInputDStream[String]
Create an input stream from a TCP source
- Flume Sink for Spark Streaming



**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化资料的朋友,可以戳这里获取](https://gitee.com/vip204888)**