> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

> ClickPipes 支持将 DynamoDB 连接到 ClickHouse。

# 从 DynamoDB 到 ClickHouse 的 CDC（变更数据捕获）

export const Image = ({img, alt, size = "lg"}) => {
  const normalizedSize = ["sm", "md", "lg"].includes(size) ? size : "lg";
  return <div className={`ch-image-${normalizedSize}`}>
      <Frame>
        <img src={img} alt={alt} />
      </Frame>
    </div>;
};

export const CloudNotSupportedBadge = () => {
  return <div className="cloudNotSupportedBadge">
            <div className="cloudNotSupportedIcon">
            <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path strokeWidth="1.5" d="M6.33366 12.6666L12.3739 12.6667C13.6593 12.6667 14.7073 11.6187 14.7073 10.3334C14.7073 9.04804 13.6593 8.00003 12.3739 8.00003C12.3739 8.00003 12.3337 7.66659 12.0003 7.33325M10.667 5.33322C8.00033 2.33325 4.45395 4.78537 4.14195 6.68203C2.55728 6.7627 1.29395 8.06203 1.29395 9.6667C1.29395 11.3234 2.66699 12.6666 4.00033 12.6666" stroke="currentColor" strokeLinecap="round" strokeLinejoin="round" />
                <path strokeWidth="1.5" d="M2.66699 14L12.0003 4.66663" stroke="currentColor" strokeLinecap="round" strokeLinejoin="round" />
            </svg>

        </div>
            ClickHouse Cloud 不支持此功能
        </div>;
};

本页介绍如何使用 ClickPipes 配置从 DynamoDB 到 ClickHouse 的 CDC (变更数据捕获)  (变更数据捕获) 。此集成包含 2 个组件：

1. 通过 S3 ClickPipes 执行初始快照
2. 通过 Kinesis ClickPipes 进行实时更新

数据将被摄取到 `ReplacingMergeTree` 中。该表引擎常用于 CDC (变更数据捕获)  场景，以便应用更新操作。有关此模式的更多信息，请参阅以下博客文章：

* [PostgreSQL 与 ClickHouse 的变更数据捕获 (CDC (变更数据捕获) ) - 第 1 部分](https://clickhouse.com/blog/clickhouse-postgresql-change-data-capture-cdc-part-1?loc=docs-rockest-migrations)
* [PostgreSQL 与 ClickHouse 的变更数据捕获 (CDC (变更数据捕获) ) - 第 2 部分](https://clickhouse.com/blog/clickhouse-postgresql-change-data-capture-cdc-part-2?loc=docs-rockest-migrations)

<Steps>
  <Step title="设置 Kinesis 数据流" id="1-set-up-kinesis-stream">
    首先，您需要在 DynamoDB 表上启用 Kinesis 数据流，以实时捕获变更。我们希望在创建快照之前先完成这一步，以免遗漏任何数据。
    AWS 指南可见[此处](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/kds.html)。

    <Image img="https://mintcdn.com/private-7c7dfe99/dZZG_-B0EzCG8L4V/images/integrations/data-ingestion/dbms/dynamodb/dynamodb-kinesis-stream.webp?fit=max&auto=format&n=dZZG_-B0EzCG8L4V&q=85&s=939ff3cb23f96ab215db4148fa61f351" size="lg" alt="DynamoDB Kinesis 数据流" border width="1238" height="215" data-path="images/integrations/data-ingestion/dbms/dynamodb/dynamodb-kinesis-stream.webp" />
  </Step>

  <Step title="创建快照" id="2-create-the-snapshot">
    接下来，我们将为 DynamoDB 表创建一个快照。这可以通过 AWS 导出到 S3 的方式来实现。可在[此处](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/S3DataExport.HowItWorks.html)查看 AWS 指南。
    **你需要以 DynamoDB JSON 格式执行“完整导出”。**

    <Image img="https://mintcdn.com/private-7c7dfe99/dZZG_-B0EzCG8L4V/images/integrations/data-ingestion/dbms/dynamodb/dynamodb-s3-export.webp?fit=max&auto=format&n=dZZG_-B0EzCG8L4V&q=85&s=483c56ff98809c4b31281691b80b47ae" size="md" alt="DynamoDB S3 导出" border width="686" height="929" data-path="images/integrations/data-ingestion/dbms/dynamodb/dynamodb-s3-export.webp" />
  </Step>

  <Step title="将快照导入 ClickHouse" id="3-load-the-snapshot-into-clickhouse">
    ### 创建所需的表

    来自 DynamoDB 的快照数据大致如下：

    ```json theme={null}
    {
      "age": {
        "N": "26"
      },
      "first_name": {
        "S": "sally"
      },
      "id": {
        "S": "0A556908-F72B-4BE6-9048-9E60715358D4"
      }
    }
    ```

    请注意，这些数据采用嵌套格式。在加载到 ClickHouse 之前，需要先将其展平。可以通过在 ClickHouse 的 materialized view 中使用 `JSONExtract` 函数来实现。

    我们需要创建三个表：

    1. 一个用于存储来自 DynamoDB 的原始数据的表
    2. 一个用于存储最终展平后数据的表 (目标表)
    3. 一个用于展平数据的 materialized view

    对于上面的 DynamoDB 示例数据，对应的 ClickHouse 表如下所示：

    ```sql theme={null}
    /* Snapshot table */
    CREATE TABLE IF NOT EXISTS "default"."snapshot"
    (
        `item` String
    )
    ORDER BY tuple();

    /* Table for final flattened data */
    CREATE MATERIALIZED VIEW IF NOT EXISTS "default"."snapshot_mv" TO "default"."destination" AS
    SELECT
        JSONExtractString(item, 'id', 'S') AS id,
        JSONExtractInt(item, 'age', 'N') AS age,
        JSONExtractString(item, 'first_name', 'S') AS first_name
    FROM "default"."snapshot";

    /* Table for final flattened data */
    CREATE TABLE IF NOT EXISTS "default"."destination" (
        "id" String,
        "first_name" String,
        "age" Int8,
        "version" Int64
    )
    ENGINE ReplacingMergeTree("version")
    ORDER BY id;
    ```

    目标端表需要满足以下要求：

    * 该表必须是 `ReplacingMergeTree` 表
    * 该表必须包含一个 `version` 列
      * 在后续步骤中，我们会将 Kinesis 数据流 中的 `ApproximateCreationDateTime` 字段映射到 `version` 列。
    * 该表应将分区键用作排序键 (由 `ORDER BY` 指定)
      * 具有相同排序键的行会根据 `version` 列去重。

    ### 创建快照 ClickPipe

    现在，您可以创建一个 ClickPipe，将快照数据从 S3 加载到 ClickHouse。请遵循[此处](/docs/zh/integrations/clickpipes/object-storage/amazon-s3/overview)的 S3 ClickPipe 指南，但使用以下设置：

    * **摄取路径**：您需要找到 S3 中已导出的 JSON 文件路径。该路径大致如下：

    ```text theme={null}
    https://{bucket}.s3.amazonaws.com/{prefix}/AWSDynamoDB/{export-id}/data/*
    ```

    * **格式**: JSONEachRow
    * **表**: 你的快照表 (例如上面示例中的 `default.snapshot`)

    创建完成后，数据会开始写入快照表和目标端表。你无需等到快照加载完成后再继续下一步。
  </Step>

  <Step title="创建 Kinesis ClickPipe" id="4-create-the-kinesis-clickpipe">
    现在我们可以设置 Kinesis ClickPipe，以捕获来自 Kinesis 数据流的实时变更。请按照[这里](/docs/zh/integrations/clickpipes/kinesis/overview)的 Kinesis ClickPipe 指南操作，但使用以下设置：

    * **Stream**：第 1 步中使用的 Kinesis 数据流
    * **Table**：你的目标端表 (例如上面示例中的 `default.destination`)
    * **Flatten object**：true
    * **Column mappings**：
      * `ApproximateCreationDateTime`：`version`
      * 按照下图所示，将其他字段映射到相应的目标端列

    <Image img="https://mintcdn.com/private-7c7dfe99/dZZG_-B0EzCG8L4V/images/integrations/data-ingestion/dbms/dynamodb/dynamodb-map-columns.webp?fit=max&auto=format&n=dZZG_-B0EzCG8L4V&q=85&s=0261fe733d26d110dd5490b47d78a71b" size="md" alt="DynamoDB 映射列" border width="1784" height="1630" data-path="images/integrations/data-ingestion/dbms/dynamodb/dynamodb-map-columns.webp" />
  </Step>

  <Step title="清理（可选）" id="5-cleanup-optional">
    快照 ClickPipe 完成后，您可以删除快照表和 materialized view。

    ```sql theme={null}
    DROP TABLE IF EXISTS "default"."snapshot";
    DROP TABLE IF EXISTS "default"."snapshot_clickpipes_error";
    DROP VIEW IF EXISTS "default"."snapshot_mv";
    ```
  </Step>
</Steps>
