Skip to Content
使用 Apache Spark 进行流处理
book

使用 Apache Spark 进行流处理

by Gerard Maas, Francois Garillot
July 2025
Intermediate to advanced
452 pages
4h 54m
Chinese
O'Reilly Media, Inc.
Content preview from 使用 Apache Spark 进行流处理

第 12 章 基于事件时间的流处理 基于事件时间的流处理

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

"时间的影响 "一文中,我们从一般角度讨论了 流处理中时间的影响。

我们还记得,事件时间处理指的是从事件产生的时间轴来观察事件流,并从这个角度来应用处理逻辑。当我们有兴趣分析事件数据随时间变化的模式时,有必要像观察事件产生时那样来处理事件。 为此,我们要求产生事件的设备或系统为事件 "加盖 "创建时间的戳记。 因此,"时间戳记 "这个通常的名称指的是特定的有界事件时间。 我们将该时间作为时间如何演变的参照基准。

为了说明这一概念,让我们来探讨一个熟悉的例子。 考虑一个用于监测当地天气状况的气象站网络。 一些偏远的气象站通过移动网络连接,而另一些设在志愿者家中的气象站则可以使用不同质量的网络连接。 天气监测系统不能依赖于事件的到达顺序,因为该顺序主要取决于它们所连接的网络的速度和可靠性。 相反,天气应用依赖于每个气象站对所传送的事件进行时间戳处理。然后,我们的流处理利用这些时间戳来计算 基于时间的聚合,为天气预报系统提供信息。

流处理引擎使用事件时间的能力非常重要,因为我们通常感兴趣的是事件产生的相对顺序,而不是事件处理的顺序。 在本章中,我们将了解结构化流如何为事件时间处理提供无缝支持。

了解结构化数据流中的事件时间

在 服务器端,时间概念由运行任何给定应用程序的计算机的内部时钟决定。对于在机器集群上运行的分布式应用程序,必须使用 时钟同步技术和协议(如Network Time Protocol(NTP))将所有时钟调整到同一时间。这样做的目的是,在计算机集群上运行的分布式应用程序的不同部分可以就时间线和事件的相对顺序做出一致的决定。

然而,当数据来自外部设备时,如传感器 Network+ 、其他数据中心、手机或联网汽车等,我们无法保证它们的时钟与我们的计算机集群一致。 我们需要从产生系统的角度来解释传入事件的时间线,而不是参照处理系统的内部时钟。图 12-1描述了这种情况。

spas 1201
图 12-1. 内部事件时间轴

图 12-1 中,我们可以直观地看到结构化数据流是如何处理时间的:

  • x 轴为处理时间,即处理系统的时钟时间。

  • y 轴表示事件时间轴的内部表示法。

  • 事件用圆圈表示,圆圈旁边有相应的事件时间标签。

  • 事件到达时间与 x 轴上的时间相对应。

随着事件到达系统,我们内部的时间概念也在不断进步:

  1. 第一个事件00:08 到达系统的时间是00:07 ,从机器时钟的角度来看是 "早期"。我们可以看出,内部时钟时间并不影响我们对事件时间线的感知。

  2. 事件时间轴前进到00:08

  3. 下一批事件00:10,00:12,00:18 开始处理。事件时间轴前进到00:18 ,因为这是迄今为止观察到的最长时间。

  4. 00:15 进入系统。由于 早于当前的内部时间,因此事件时间轴保持当前值 。00:15 00:18

  5. 同样,也收到了00:1100:09 。我们是否应该处理这些事件?

  6. 当处理下一组事件00:14,00:25,00:28 时,流时钟会增加到最大值00:28

一般来说,结构化数据流通过保持事件中声明为时间戳的字段的单调递增上界,推断出用事件时间处理的事件的时间轴。 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

大模型应用开发极简入门 : 基于GPT-4和ChatGPT

大模型应用开发极简入门 : 基于GPT-4和ChatGPT

Olivier Caelen, Marie-Alice Blete
Flink基础教程

Flink基础教程

Ellen Friedman, Kostas Tzoumas
Rx.NET in Action

Rx.NET in Action

Tamir Dresher

Publisher Resources

ISBN: 9798341662971