

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 為 Amazon Keyspaces 中的現有資料表啟用 CDC 串流
<a name="keyspaces-enable-cdc-alter-table"></a>

若要為現有資料表啟用 CDC 串流，您可以在 CQL 中使用 `ALTER TABLE`陳述式、將 `update-table`命令與 搭配使用 AWS CLI，也可以使用 主控台。

對於資料表中每個變更的資料列，Amazon Keyspaces 可以根據您`cdc_specification`選取的 `view_type`的 擷取下列變更：
+ `NEW_AND_OLD_IMAGES` – 變更前後的兩個資料列版本。這是預設值。
+ `NEW_IMAGE` – 變更後的列版本。
+ `OLD_IMAGE` – 變更前的資料列版本。
+ `KEYS_ONLY` – 已變更之資料列的分割區和叢集索引鍵。

如需如何標記串流的詳細資訊，請參閱 [將新標籤新增至串流](Tagging.Operations.existing.stream.md)。

**注意**  
Amazon Keyspaces CDC 需要存在服務連結角色 (`AWSServiceRoleForAmazonKeyspacesCDC`)，以代表您將指標資料從 Amazon Keyspaces CDC 串流發佈到 CloudWatch 帳戶中`"cloudwatch:namespace": "AWS/Cassandra"`的 。系統會自動建立此角色。如需詳細資訊，請參閱[使用 Amazon Keyspaces CDC 串流的角色](using-service-linked-roles-CDC-streams.md)。

------
#### [ Cassandra Query Language (CQL) ]

**使用 CQL 啟用串流 (CDC 串流）**

您可以使用 `ALTER TABLE`為現有資料表啟用串流。

1. 下列範例會建立僅擷取變更資料列之分割區和叢集索引鍵變更的串流。

   ```
   ALTER TABLE mykeyspace.mytable
   WITH cdc = TRUE
   AND CUSTOM_PROPERTIES={'cdc_specification': {'view_type': 'KEYS_ONLY'}};
   ```

1. 若要驗證串流設定，您可以使用下列陳述式。

   ```
   SELECT keyspace_name, table_name, cdc, custom_properties FROM system_schema_mcs.tables WHERE keyspace_name = 'mykeyspace' AND table_name = 'mytable';
   ```

   陳述式的輸出看起來與此類似。

   ```
    keyspace_name | table_name | cdc  | custom_properties
   ---------------+------------+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
       mykeyspace |    mytable | True | {'capacity_mode': {'last_update_to_pay_per_request_timestamp': '1741385897045', 'throughput_mode': 'PAY_PER_REQUEST'}, 'cdc_specification': {'latest_stream_arn': 'arn:aws:cassandra:us-east-1:111122223333:/keyspace/mykeyspace/table/mytable/stream/2025-03-07T22:20:10.454', 'status': 'ENABLED', 'view_type': 'KEYS_ONLY'}, 'encryption_specification': {'encryption_type': 'AWS_OWNED_KMS_KEY'}, 'point_in_time_recovery': {'status': 'disabled'}}
   ```

------
#### [ CLI ]

**使用 建立 CDC 串流 AWS CLI**

1. 若要為現有資料表建立串流，您可以使用下列語法。

   ```
   aws keyspaces update-table \
   --keyspace-name 'mykeyspace' \
   --table-name 'mytable' \
   --cdc-specification status=ENABLED,viewType=NEW_AND_OLD_IMAGES
   ```

1. 該命令的輸出會顯示標準`create-table`回應，看起來與此範例類似。

   ```
   { "resourceArn": "arn:aws:cassandra:us-east-1:111122223333:/keyspace/mykeyspace/table/mytable" }
   ```

------
#### [ Console ]

**使用 Amazon Keyspaces 主控台啟用 CDC 串流**

1. 登入 AWS 管理主控台，並在 https：//[https://console.aws.amazon.com/keyspaces/home](https://console.aws.amazon.com/keyspaces/home) 開啟 Amazon Keyspaces 主控台。

1. 在導覽窗格中，選擇**資料表**，然後從清單中選擇資料表。

1. 選擇**串流**索引標籤。

1. 選擇**編輯**以啟用串流。

1. 選取**開啟串流**。

1. 選擇串流的**檢視類型**。以下是可用的選項。請注意，您無法在建立串流之後變更串流的檢視類型。
   + **新舊映像** – Amazon Keyspaces 會擷取變更前後的兩個資料列版本。這是預設值。
   + **新映像** – Amazon Keyspaces 只會擷取變更後的資料列版本。
   + **舊映像** – Amazon Keyspaces 只會擷取變更前的資料列版本。
   + **僅限主索引鍵** – Amazon Keyspaces 只會擷取已變更資料列的分割區和叢集索引鍵資料欄。

1. 若要完成，請選擇**儲存變更**。

------