Chapter 7:Spark Streaming
前言
本章以Spark Streaming为重点,重点介绍Spark Streaming的基本概念、Spark Streaming架构、StreamingContext对象、DStream基本原理及相关操作,最后一步一步讲解一个完整的案例等相关内容。
一、Spark Streaming
(一)Introduction
Spark Streaming是Spark核心API的扩展,支持可扩展、高吞吐量和容错的实时数据流处理。 数据可以从Kafka、Flume、Kinesis或TCP sockets等多种来源获得,流数据处理可以使用map、reduce、join和window等高级功能开发的复杂算法来执行。 最后将处理后的数据推送到文件系统、数据库和实时仪表板中。 此外,您还可以在数据流上应用Spark提供的机器学习和图形处理算法。
Spark Streaming is an extension of Apache Spark that enables scalable, high-throughput, fault-tolerant stream processing of real-time data streams. It allows you to process real-time data from various sources such as Kafka, Flume, Kinesis, or TCP sockets, and perform complex computations on this data using the Spark API.

(二)Feature
1.Ease of Use 易用性
与Spark API无缝集成,允许用户对流数据进行与批处理数据相同的操作。
Integrates seamlessly with the Spark API, allowing users to apply the same operations to streaming data as to batch data.
2.Fault Tolerance
通过Spark原生的弹性分布式数据集(resilient distributed dataset, rdd)提供端到端的容错保障。
Provides end-to-end fault tolerance guarantees through Spark’s native resilient distributed datasets (RDDs).
3.Scalability
利用Spark的分布式处理能力,可以高效地处理大量数据。
Leverages Spark’s distributed processing power, enabling it to scale efficiently with large data volumes.
4.Integration with Batch and Interactive Queries
允许使用Spark SQL对批处理和流数据进行查询,简化了代码的开发和维护。
Allows usage of Spark SQL queries for both batch and streaming data, simplifying the development and maintenance of code.
5.Advanced Analytics
支持高级分析,如窗口计算、连接和流上的聚合。
Supports advanced analytics like windowed computations, joins, and aggregations over streams.
(三)关键概念
1.Discretized Stream (DStream) 离散化流
Spark Streaming中的主要抽象。它将连续的数据流表示为一系列RDD, RDD是不可变的分布式对象集合。
The main abstraction in Spark Streaming. It represents a continuous stream of data as a series of RDDs, which are immutable distributed collections of objects.
2.Input DStreams
从数据源接收的数据流,如Kafka或Flume。
These are streams of data received from a source, such as Kafka or Flume.
3.transformations
DStream上产生其他DStream的操作,如map、filter、reduce等。
Operations on DStreams that yield other DStreams, like map, filter, reduce, etc.
4.Output Operations
将数据写入外部系统的操作,如saveAsTextFiles、saveAsObjectFiles、saveAsHadoopFiles、foreachRDD等。
(四)Structure
DStream表示连续的数据流,这些数据被分成小的、可管理的批次。dstream建立在rdd (Resilient Distributed Datasets,弹性分布式数据集)之上,rdd是Spark的核心数据抽象,它可以与其他Spark组件(如MLlib和Spark SQL)无缝集成。
其他系统要么有专门的数据流处理引擎,要么有编译到不同引擎上的独立的批处理api和流api,与之不同的是,Spark streaming使用单一的执行引擎和统一的编程模型来处理批处理数据和流数据。与传统的流媒体系统相比,这种方法具有显著的优势,特别是在简单性和效率方面。

二、DStream
(一)概念
DStream(离散化流)是Spark Streaming的核心抽象。
A DStream (Discretized Stream) is the core abstraction in Spark Streaming.
它将连续的数据流表示为一系列弹性分布式数据集(Resilient Distributed dataset, rdd),使Spark Streaming能够以统一的方式处理实时流数据和批量数据。
It represents a continuous stream of data as a series of Resilient Distributed Datasets (RDDs), enabling Spark Streaming to process both real-time streaming and batch data with a unified approach.
DStream本质上是一个由RDD组成的序列,其中每个RDD对应于在特定时间间隔内接收到的数据。如下图所示
A DStream is essentially a sequence of RDDs, where each RDD corresponds to data received over a specific time interval.

这种离散特性让Spark Streaming可以利用RDD API的强大功能来高效地进行流处理。
This discrete nature allows Spark Streaming to leverage the power of the RDD API for efficient stream processing.
DStream可以由各种输入源创建,如Kafka、Flume、Kinesis和TCP套接字。
DStreams can be created from various input sources such as Kafka, Flume, Kinesis, and TCP sockets.
你可以对dstream应用常见的RDD转化操作,如map、flatMap、filter、reduceByKey和window,从而构建复杂的数据处理流水线。
You can apply common RDD transformations to DStreams, like map, flatMap, filter, reduceByKey, and window, enabling you to build complex data processing pipelines.
最后,输出操作允许用户将处理后的数据写入外部系统,如HDFS、数据库或live dashboard
Finally, output operations allow you to write processed data to external systems, such as HDFS, databases, or live dashboard
(二)工作方式
要理解dstream的工作原理,必须掌握Spark Streaming容错和可扩展数据处理的基本机制。下面是DStream如何运作的一步一步解释。
To understand how DStreams work, it’s important to grasp the fundamental mechanics of how Spark Streaming processes data in a fault-tolerant and scalable manner.
1.Data Ingestion 数据摄取
Spark Streaming从各种数据源接收数据,如Kafka、Flume、TCP sockets或任何自定义数据源。
数据以较小的时间间隔采集(例如,每1秒一次),并存储在Spark的内存中。
2. Batch Creation - Micro-Batch Processing 批量创建-微批量处理
传入的数据被分成小批量的rdd,这些rdd以间隔的方式进行处理。
Incoming data is divided into small batches represented by RDDs, which are processed in intervals.
3. Transformations 转换
(1)介绍
DStream上的转化操作是生成新DStream的操作。这些转化操作应用于DStream中的每个RDD。
Transformations on DStreams are operations that produce a new DStream.These transformations are applied to each RDD within the DStream.
(2)Common Transformations 常见转换
常见的转换操作
map: Applies a function to each element.
flatMap: Similar to map, but each input item can map to 0 or more output items.
filter: Filters elements based on a predicate.
reduceByKey: Combines values with the same key.
window: Aggregates elements over a sliding window.
4.RDD Generation
每个时间间隔都会创建一个新的RDD,包含在该时间间隔内接收到的数据。DStream上的转换会在底层的RDD上产生相应的转换。
5.Fault Tolerance
DStream使用谱系图和rdd来确保数据丢失时可以重新计算。
如果数据丢失,可以使用原始转换重新计算数据。
DStreams use lineage graphs and RDDs to ensure data can be recomputed if lost.If data is lost, it can be recomputed using the original transformations.
6.Output Operations
输出操作将处理过的数据推送到外部系统,如HDFS、数据库或实时仪表板。
Output operations push the processed data to external systems such as HDFS, databases, or live dashboards.
常见的输出操作有print、saveAsTextFiles、saveAsHadoopFiles、foreachRDD等。
Some common output operations include pprint, saveAsTextFiles, saveAsHadoopFiles, and foreachRDD.
7.Execution
启动流上下文:需要启动StreamingContext才能开始处理数据。处理将继续,直到流上下文手动停止或由于错误而停止。
The StreamingContext needs to be started to begin processing the data.
The processing continues until the streaming context is stopped manually or due to an error.
三、Windows operation窗口操作
Spark Streaming中的窗口操作允许在指定的时间窗口上处理数据,而不仅仅是处理单个数据块。
如果只需要查看更长一段时间内的数据,而不是只查看单个块的数据。这就是窗口操作的用武之地。窗口操作允许你在指定的时间(窗口持续时间)内查看数据流,并以固定的时间间隔(滑动间隔)更新结果。
Window operations in Spark Streaming allow the processing of data over specified time windows rather than just individual chunks of data. sometimes you want to look at the data over a longer period of time instead of just individual chunks. This is where window operations come in. A window operation lets you look at a stream of data over a specified period (window duration) and update the results at regular intervals (sliding interval).
(一)关键概念
窗口持续时间: 时间窗口的长度(30秒)。
Window Duration: How long the time window is (30 sec).
滑动间隔(Sliding Interval): 窗口向前滑动处理下一组数据的频率(10)。
Sliding Interval: How often the window slides forward to process the next set of data(10).
(二)例子

对于数据流(例如聊天消息),你可以统计过去2分钟的消息,每1分钟更新一次。
For a stream of data (e.g., chat messages), you can count messages over the last 2 minutes, updated every 1 minutes.
想象一下,你每秒都有数据流传入,就像来自聊天室的聊天消息一样。
Imagine you have a stream of data coming in every second, like chat messages from a chatroom.
您希望计算过去两分钟的消息数,但希望每一分钟更新此计数。
You want to count the number of messages over the last 2 minutes, but you want to update this count every 1 minute.
这样,您可以查看在过去两分钟内发送了多少消息,每一分钟更新一次。
This way, you can see how many messages were sent in the last 2 minutes, updated every 1 minute.
窗口持续时间(2分钟):这是我们查看计数消息的时间长度。
Window Duration (2 Minutes): This is the length of time we are looking at to count the messages.
每次计数都将包括最近两分钟的消息。
Every count will include messages from the last 2 minutes.
滑动间隔(1分钟):这是我们更新计数的频率。
Sliding Interval (1 Minutes): This is how often we update our count.
每隔1分钟,Spark会打印出最近2分钟的消息计数。
Every 1 minutes, Spark will print the count of messages from the last 2 minutes.
我们可以说,每分钟我都想知道,在过去两分钟里我们收到了多少条消息
We can say, every minute I want to know, how many messages we have received in last 2 minutes
四、Windows Function
(一)window:
基于源DStream生成的带窗口的批量数据计算一个新的DStream
Calculate a new DStream based on the windowed batch data generated by the source DStream
1.用法
将DStream中的数据分组为更大的时间窗口
Usage: Groups the data in a DStream into larger time windows.
2.实例
将数据行分组到30秒的窗口中,并每10秒更新一次。
Example: Grouping lines of data into 30-second windows and updating every 10 seconds.
(二)countByWindow
1.用法
在滑动窗口上计算DStream中元素的数量。
Usage: Counts the number of elements in a DStream over a sliding window.
2.实例
每10秒计算30秒窗口滑动中的行数。
Example: Counting the number of lines in a 30-second window sliding every 10 seconds.
(三)reduceByWindow
1.用法
使用指定的函数在滑动窗口上减少DStream中的元素。
Usage: Reduces elements in a DStream over a sliding window using a specified function.
2.实例
每10秒滑动窗口,30秒内求和。
Example: Summing numbers in a 30-second window sliding every 10 seconds.
(四)reduceByKeyAndWindow:
1.用法
使用指定的函数减少滑动窗口上的键值对。
Reduces key-value pairs over a sliding window using a specified function.
2.实例
每10秒滑动窗口30秒计算单词数。
Example: Counting words over a 30-second window sliding every 10 seconds.
(五)countByValueAndWindow
1.用法
在滑动窗口上对DStream中每个键的值进行分组。
Usage: Groups values of each key in a DStream over a sliding window.
2.实例
在每10秒滑动30秒的窗口中,按键对值进行分组。
Example: Grouping values by key over a 30-second window sliding every 10 seconds.
(六)Maven Dependency
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.12</artifactId> <!-- Use the 2.12 version -->
<version>3.3.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming_2.12</artifactId> <!-- Use 2.12 for Scala 2.12, adjust if using a different version -->
<version>3.3.0</version> <!-- Use the appropriate Spark version -->
</dependency>
总结
本章主要讲述了Spark Streaming的基本概念、Spark Streaming架构、StreamingContext对象、DStream基本原理以及滑动窗口处理
更多推荐

所有评论(0)