Azure Cosmos DB 中的更改源

适用对象: NoSQL MongoDB Cassandra Gremlin

Azure Cosmos DB 中的更改源是一种持久记录,按发生顺序记录对容器所做的更改。 Azure Cosmos DB 中更改源支持的工作原理是侦听 Azure Cosmos DB 容器中发生的任何更改。 然后,它会按照所更改文档的修改顺序输出这些文档的排序列表。 持久保存的更改能够以异步和增量方式进行处理,而且输出可以分发到一个或多个使用者进行并行处理。

详细了解更改源设计模式

支持的 API 和客户端 SDK

以下 Azure Cosmos DB SDK 目前支持更改源功能。

客户端驱动程序 NoSQL Apache Cassandra MongoDB Apache Gremlin PostgreSQL
.NET Icon indicating that this feature is supported in the .NET SDK for the API for NoSQL. Icon indicating that this feature is supported in the .NET SDK for the API for Apache Cassandra. Icon indicating that this feature is supported in the .NET SDK for the API for MongoDB. Icon indicating that this feature is supported in the .NET SDK for the API for Apache Gremlin. Icon indicating that this feature is not supported in the .NET SDK for the API for Table. Icon indicating that this feature is not supported in the .NET SDK for the API for PostgreSQL.
Java Icon indicating that this feature is supported in the Java SDK for the API for NoSQL. Icon indicating that this feature is supported in the Java SDK for the API for Apache Cassandra. Icon indicating that this feature is supported in the Java SDK for the API for MongoDB. Icon indicating that this feature is supported in the Java SDK for the API for Apache Gremlin. Icon indicating that this feature is not supported in the Java SDK for the API for Table. Icon indicating that this feature is not supported in the Java SDK for the API for PostgreSQL.
Python Icon indicating that this feature is supported in the Python SDK for the API for NoSQL. Icon indicating that this feature is supported in the Python SDK for the API for Apache Cassandra. Icon indicating that this feature is supported in the Python SDK for the API for MongoDB. Icon indicating that this feature is supported in the Python SDK for the API for Apache Gremlin. Icon indicating that this feature is not supported in the Python SDK for the API for Table. Icon indicating that this feature is not supported in the Python SDK for the API for PostgreSQL.
Node/JavaScript Icon indicating that this feature is supported in the JavaScript SDK for the API for NoSQL. Icon indicating that this feature is supported in the JavaScript SDK for the API for Apache Cassandra. Icon indicating that this feature is supported in the JavaScript SDK for the API for MongoDB. Icon indicating that this feature is supported in the JavaScript SDK for the API for Apache Gremlin. Icon indicating that this feature is not supported in the JavaScript SDK for the API for Table. Icon indicating that this feature is not supported in the JavaScript SDK for the API for PostgreSQL.

使用更改源

可通过以下选项使用更改源:

更改源可用于 Azure Cosmos DB 容器的分区键范围。 因此,更改源可以分布在一个或多个使用者之间以供并行处理,如下图所示。

Distributed processing of Azure Cosmos DB change feed

注意

使用更改源处理器时,分区键范围将映射到物理分区;使用拉取模型时,它们将映射到 FeedRanges

更改源的功能

  • 默认情况下,所有 Azure Cosmos DB 帐户都启用了更改源。

  • 有多种更改源模式,其中一些模式需要经过额外的配置才能启用。

  • 就像执行任何其他 Azure Cosmos DB 操作一样,可使用预配吞吐量在与 Azure Cosmos DB 帐户关联的任何区域中从更改源进行读取。

  • 更改源包括针对容器中的项所执行的插入和更新操作。 如果你使用所有版本和删除模式(预览版),则还会从删除操作和 TTL 过期配置中获取更改。

  • 每项更改在更改源中确切地显示一次,客户端必须管理检查点逻辑。 如果想要避免复杂的检查点管理过程,更改源处理器提供了自动检查点和“至少一次”语义。 有关详细信息,请参阅将更改源与更改源处理器配合使用一文。

  • 可以对 Azure Cosmos DB 容器的分区键范围并行进行更改。 多个使用者可以使用此功能并行处理大型容器中发生的更改。

  • 应用程序可针对同一容器同时请求多个更改源。

  • 可以自定义更改源的起点,并可以对每种模式使用不同的选项。

更改源中项的排序顺序

更改源项按其修改时间排序。 将按分区键保证这种排序顺序,但跨分区键值的顺序没有保证。

多区域 Azure Cosmos DB 帐户中的更改源

在多区域 Azure Cosmos DB 帐户中,一个区域中发生的更改将传播到所有区域。 如果写入区域发生故障转移,更改源会在整个手动故障转移操作中发生作用,并且是连续的。 对于具有多个写入区域的帐户,无法保证何时提供更改。 如果另一个区域中发生了更新的更改,则可能会在最新版本模式下删除对同一文档的传入更改,并在所有版本和删除模式下捕获所有更改。

更改源模式

有两种可用的更改源模式:最新版本模式,以及所有版本和删除模式。 更改源的读取模式决定了从哪些操作中捕获更改,以及每个更改可用的元数据。 对于同一个 Azure Cosmos DB 容器,可以跨多个应用程序以不同的模式使用更改源。

最新版本模式

在最新版本更改源模式下,你会看到源中所有项的插入或更新操作的最新更改,并且源在容器的生命周期内可用。 系统不会指示给定的更改是来自插入操作还是更新操作,并且不会捕获删除操作。 可以从任意时间点读取更改,最早可以追溯到容器的源。 但是,如果删除了某个项,则会从更改源中移除该项。 有关详细信息,请参阅最新版本更改源模式一文。

所有版本和删除模式(预览版)

所有版本和删除模式允许查看创建、更新和删除操作对项做出的所有更改。 可以按对项做出每个更改的顺序获取对应更改的记录,包括在读取更改源期间对项进行的中途的更改。 若要通过所有版本的更改源中读取内容并删除模式,必须为 Azure Cosmos DB 帐户配置连续备份,这将创建所有版本的 Azure Cosmos DB 并删除更改源。在此模式下,只能读取为帐户配置的连续备份期间发生的更改。 有关详细信息,包括如何注册预览版,请参阅所有版本和删除更改源模式一文。

适用于 Cassandra 和 MongoDB 的 API 中的更改源

在 API for MongoDB 中,更改源功能显示为更改流;在 API for Cassandra 中,它是以包含谓词的查询形式提供的。 若要详细了解 API for MongoDB 的实现细节,请参阅 Azure Cosmos DB API for MongoDB 中的更改流

本机 Apache Cassandra 提供变更数据捕获 (CDC)。CDC 是一种机制,用于标记要存档的特定表,并在达到 CDC 日志的可配置磁盘空间时拒绝写入这些表。 Azure Cosmos DB for Apache Cassandra 中的更改源功能增强了通过 CQL 使用谓词查询更改的能力。 若要详细了解实现细节,请参阅 Azure Cosmos DB for Apache Cassandra 中的更改源

度量更改源请求单位消耗量

不管是否使用更改源,都会在每个容器中提供更改源。 更改源只会根据租约容器的预配吞吐量和每个请求的 RU 数产生费用。 使用 Azure Monitor 度量更改源的请求单位 (RU) 消耗量。 有关详细信息,请参阅在 Azure Cosmos DB 中监视吞吐量或请求单位使用情况

后续步骤

接下来,请通过以下文章继续详细了解更改源: