

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

# Amazon Keyspaces 中的用戶端時間戳記
<a name="client-side-timestamps"></a>

在 Amazon Keyspaces 中，用戶端時間戳記是 Cassandra 相容的時間戳記，會針對資料表中的每個儲存格保留這些時間戳記。您可以讓用戶端應用程式決定寫入順序，藉此使用用戶端時間戳記來解決衝突。例如，當全域分佈應用程式的用戶端更新相同的資料時，用戶端時間戳記會保留在用戶端上進行更新的順序。Amazon Keyspaces 使用這些時間戳記來處理寫入。

Amazon Keyspaces 用戶端時間戳記受到完整管理。您不需要管理低階系統設定，例如清除和壓縮策略。

當您刪除資料時，資料列會標示為使用 tombstone 刪除。Amazon Keyspaces 會自動 （通常在 10 天內） 移除斷層資料，而不會影響您的應用程式效能或可用性。Tombstoned 資料不適用於資料處理語言 (DML) 陳述式。當您繼續對包含斷層資料的列執行讀取和寫入時，斷層資料會繼續計入儲存體、讀取容量單位 (RCUs) 和寫入容量單位 WCUs)，直到從儲存體中刪除為止。

在資料表的用戶端時間戳記開啟後，您可以在 Data Manipulation Language (DML) CQL 查詢中指定具有 `USING TIMESTAMP`子句的時間戳記。如需詳細資訊，請參閱[在 Amazon Keyspaces 中的查詢中使用用戶端時間戳記](client-side-timestamps-how-to-queries.md)。如果您未在 CQL 查詢中指定時間戳記，Amazon Keyspaces 會使用用戶端驅動程式傳遞的時間戳記。如果用戶端驅動程式不提供時間戳記，Amazon Keyspaces 會自動指派儲存格層級時間戳記，因為時間戳記不能是 `NULL`。若要查詢時間戳記，您可以在 DML 陳述式中使用 `WRITETIME`函數。

Amazon Keyspaces 不會額外收費以開啟用戶端時間戳記。不過，使用用戶端時間戳記，您可以為資料列中的每個值存放和寫入其他資料。這可能會導致額外的儲存體用量，在某些情況下可能會導致額外的輸送量用量。如需 Amazon Keyspaces 定價的詳細資訊，請參閱 [Amazon Keyspaces （適用於 Apache Cassandra) 定價](https://aws.amazon.com/keyspaces/pricing)。

在 Amazon Keyspaces 中開啟用戶端時間戳記時，每一列的每一欄都會儲存時間戳記。這些時間戳記大約需要 20-40 個位元組 （取決於您的資料），並對資料列的儲存和輸送量成本做出貢獻。這些中繼資料位元組也會計入 1-MB 的資料列大小配額。若要判斷儲存空間的整體增加 （以確保資料列大小保持在 1 MB 以下），請考慮資料表中的資料欄數目和每一列中的集合元素數目。例如，如果資料表有 20 個資料欄，每欄存放 40 個位元組的資料，資料列的大小會從 800 個位元組增加到 1200 個位元組。如需如何估計資料列大小的詳細資訊，請參閱 [估計 Amazon Keyspaces 中的資料列大小](calculating-row-size.md)。除了額外 400 個位元組的儲存，在此範例中，每次寫入消耗的寫入容量單位 (WCUs) 數量從 1 個 WCU 增加到 2 WCUs。如需如何計算讀取和寫入容量的詳細資訊，請參閱 [在 Amazon Keyspaces 中設定讀取/寫入容量模式](ReadWriteCapacityMode.md)。

資料表的用戶端時間戳記開啟後，您就無法將其關閉。

若要進一步了解如何在查詢中使用用戶端時間戳記，請參閱 [在 Amazon Keyspaces 中的查詢中使用用戶端時間戳記](client-side-timestamps-how-to-queries.md)。

**Topics**
+ [Amazon Keyspaces 用戶端時間戳記如何與 AWS 服務整合](#client-side-timestamps_integration)
+ [在 Amazon Keyspaces 中建立具有用戶端時間戳記的新資料表](client-side-timestamps-create-new-table.md)
+ [在 Amazon Keyspaces 中設定資料表的用戶端時間戳記](client-side-timestamps-existing-table.md)
+ [在 Amazon Keyspaces 中的查詢中使用用戶端時間戳記](client-side-timestamps-how-to-queries.md)

## Amazon Keyspaces 用戶端時間戳記如何與 AWS 服務整合
<a name="client-side-timestamps_integration"></a>

下列用戶端時間戳記指標可在 Amazon CloudWatch 中使用，以啟用持續監控。
+ `SystemReconciliationDeletes` – 移除斷層資料所需的刪除操作數目。

如需如何監控 CloudWatch 指標的詳細資訊，請參閱 [使用 Amazon CloudWatch 監控 Amazon Keyspaces](monitoring-cloudwatch.md)。

使用 時 CloudFormation，您可以在建立 Amazon Keyspaces 資料表時啟用用戶端時間戳記。如需詳細資訊，請參閱[「AWS CloudFormation 使用者指南」](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cassandra-table.html)。

# 在 Amazon Keyspaces 中建立具有用戶端時間戳記的新資料表
<a name="client-side-timestamps-create-new-table"></a>

請依照這些範例，使用 Amazon Keyspaces AWS 管理主控台、Cassandra 查詢語言 (CQL) 或 建立啟用用戶端時間戳記的新 Amazon Keyspaces 資料表 AWS Command Line Interface

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

**使用用戶端時間戳記建立新的資料表 （主控台）**

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

1. 在導覽窗格中，選擇 **Tables** (資料表)，然後選擇 **Create table** (建立資料表)。

1. 在**資料表詳細資訊區段的建立**資料表頁面上，選取金鑰空間，並提供新資料表的名稱。 ****

1. 在**結構描述**區段中，建立資料表的結構描述。

1. 在**資料表設定**區段中，選擇**自訂設定**。

1. 繼續前往**用戶端時間戳記**。

   選擇**開啟用戶端時間戳記**，以開啟資料表的用戶端時間戳記。

1. 選擇 **Create Table** (建立資料表)。您的資料表會在用戶端時間戳記開啟的情況下建立。

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

**使用 CQL 建立新資料表**

1. 若要使用 CQL 建立啟用用戶端時間戳記的新資料表，您可以使用下列範例。

   ```
   CREATE TABLE my_keyspace.my_table (
      userid uuid,
      time timeuuid,
      subject text,
      body text,
      user inet,
      PRIMARY KEY (userid, time)
   ) WITH CUSTOM_PROPERTIES = {'client_side_timestamps': {'status': 'enabled'}};
   ```

1. 若要確認新資料表的用戶端時間戳記設定，請使用 `SELECT`陳述式來檢閱 `custom_properties`，如下列範例所示。

   ```
   SELECT custom_properties from system_schema_mcs.tables where keyspace_name = 'my_keyspace' and table_name = 'my_table';
   ```

   此陳述式的輸出會顯示用戶端時間戳記的狀態。

   ```
   'client_side_timestamps': {'status': 'enabled'}
   ```

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

**使用 建立新的資料表 AWS CLI**

1. 若要在啟用用戶端時間戳記的情況下建立新的資料表，您可以使用下列範例。

   ```
   ./aws keyspaces create-table \
   --keyspace-name my_keyspace \
   --table-name my_table \
   --client-side-timestamps 'status=ENABLED' \
   --schema-definition 'allColumns=[{name=id,type=int},{name=date,type=timestamp},{name=name,type=text}],partitionKeys=[{name=id}]'
   ```

1. 若要確認新資料表的用戶端時間戳記已開啟，請執行下列程式碼。

   ```
   ./aws keyspaces get-table \
   --keyspace-name my_keyspace \
   --table-name my_table
   ```

   輸出看起來應該與此範例類似。

   ```
   {
       "keyspaceName": "my_keyspace",
       "tableName": "my_table",
       "resourceArn": "arn:aws:cassandra:us-east-1:111122223333:/keyspace/my_keyspace/table/my_table",
       "creationTimestamp": 1662681206.032,
       "status": "ACTIVE",
       "schemaDefinition": {
           "allColumns": [
               {
                   "name": "id",
                   "type": "int"
               },
               {
                   "name": "date",
                   "type": "timestamp"
               },
               {
                   "name": "name",
                   "type": "text"
               }
           ],
           "partitionKeys": [
               {
                   "name": "id"
               }
           ],
           "clusteringKeys": [],
           "staticColumns": []
       },
       "capacitySpecification": {
           "throughputMode": "PAY_PER_REQUEST",
           "lastUpdateToPayPerRequestTimestamp": 1662681206.032
       },
       "encryptionSpecification": {
           "type": "AWS_OWNED_KMS_KEY"
       },
       "pointInTimeRecovery": {
           "status": "DISABLED"
       },
       "clientSideTimestamps": {
           "status": "ENABLED"
       },
       "ttl": {
           "status": "ENABLED"
       },
       "defaultTimeToLive": 0,
       "comment": {
           "message": ""
       }
   }
   ```

------

# 在 Amazon Keyspaces 中設定資料表的用戶端時間戳記
<a name="client-side-timestamps-existing-table"></a>

請依照這些範例，使用 Amazon Keyspaces AWS 管理主控台、Cassandra Query Language (CQL) 或 開啟現有資料表的用戶端時間戳記 AWS Command Line Interface。

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

**開啟現有資料表的用戶端時間戳記 （主控台）**

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

1. 選擇您要更新的資料表，然後選擇**其他設定**索引標籤。

1. **在其他設定**索引標籤上，前往**修改用戶端時間戳記**，然後選取**開啟用戶端時間戳記**

1. 選擇**儲存變更**以變更資料表的設定。

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

**使用 CQL 陳述式**

1. 使用 `ALTER TABLE` CQL 陳述式開啟現有資料表的用戶端時間戳記。

   ```
   ALTER TABLE my_table WITH custom_properties = {'client_side_timestamps': {'status': 'enabled'}};;
   ```

1. 若要確認新資料表的用戶端時間戳記設定，請使用 `SELECT`陳述式來檢閱 `custom_properties`，如下列範例所示。

   ```
   SELECT custom_properties from system_schema_mcs.tables where keyspace_name = 'my_keyspace' and table_name = 'my_table';
   ```

   此陳述式的輸出會顯示用戶端時間戳記的狀態。

   ```
   'client_side_timestamps': {'status': 'enabled'}
   ```

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

**使用 AWS CLI**

1. 您可以使用 AWS CLI 下列範例，使用 開啟現有資料表的用戶端時間戳記。

   ```
   ./aws keyspaces update-table \
   --keyspace-name my_keyspace \
   --table-name my_table \
   --client-side-timestamps 'status=ENABLED'
   ```

1. 若要確認資料表的用戶端時間戳記已開啟，請執行下列程式碼。

   ```
   ./aws keyspaces get-table \
   --keyspace-name my_keyspace \
   --table-name my_table
   ```

   輸出看起來應該類似於此範例，並將用戶端時間戳記的狀態顯示為 `ENABLED`。

   ```
   {
       "keyspaceName": "my_keyspace",
       "tableName": "my_table",
       "resourceArn": "arn:aws:cassandra:us-east-1:111122223333:/keyspace/my_keyspace/table/my_table",
       "creationTimestamp": 1662681312.906,
       "status": "ACTIVE",
       "schemaDefinition": {
           "allColumns": [
               {
                   "name": "id",
                   "type": "int"
               },
               {
                   "name": "date",
                   "type": "timestamp"
               },
               {
                   "name": "name",
                   "type": "text"
               }
           ],
           "partitionKeys": [
               {
                   "name": "id"
               }
           ],
           "clusteringKeys": [],
           "staticColumns": []
       },
       "capacitySpecification": {
           "throughputMode": "PAY_PER_REQUEST",
           "lastUpdateToPayPerRequestTimestamp": 1662681312.906
       },
       "encryptionSpecification": {
           "type": "AWS_OWNED_KMS_KEY"
       },
       "pointInTimeRecovery": {
           "status": "DISABLED"
       },
       "clientSideTimestamps": {
           "status": "ENABLED"
       },
       "ttl": {
           "status": "ENABLED"
       },
       "defaultTimeToLive": 0,
       "comment": {
           "message": ""
       }
   }
   ```

------

# 在 Amazon Keyspaces 中的查詢中使用用戶端時間戳記
<a name="client-side-timestamps-how-to-queries"></a>

開啟用戶端時間戳記之後，您可以使用 `USING TIMESTAMP`子句在 `INSERT`、 `UPDATE`和 `DELETE`陳述式中傳遞時間戳記。

時間戳記值`bigint`表示自稱為 的標準基準時間以來的微秒數epoch：1970 年 1 月 1 日，格林威治標準時間 00：00：00。由用戶端提供的時間戳記必須介於過去 2 天到未來 5 分鐘之間，從目前的牆壁時鐘時間起算。

Amazon Keyspaces 會在資料生命週期內保留時間戳記中繼資料。您可以使用 `WRITETIME`函數來查詢過去幾年發生的時間戳記。如需 CQL 語法的詳細資訊，請參閱 [Amazon Keyspaces 中的 DML 陳述式 （資料處理語言）](cql.dml.md)。

下列 CQL 陳述式是如何使用時間戳記做為 的範例`update_parameter`。

```
INSERT INTO catalog.book_awards (year, award, rank, category, book_title, author, publisher)
   VALUES (2022, 'Wolf', 4, 'Non-Fiction', 'Science Update', 'Ana Carolina Silva', 'SomePublisher') 
   USING TIMESTAMP 1669069624;
```

如果您未在 CQL 查詢中指定時間戳記，Amazon Keyspaces 會使用用戶端驅動程式傳遞的時間戳記。如果用戶端驅動程式未提供時間戳記，Amazon Keyspaces 會為您的寫入操作指派伺服器端時間戳記。

若要查看針對特定資料欄存放的時間戳記值，您可以在`SELECT`陳述式中使用 `WRITETIME`函數，如下列範例所示。

```
SELECT year, award, rank, category, book_title, author, publisher, WRITETIME(year), WRITETIME(award), WRITETIME(rank),
  WRITETIME(category), WRITETIME(book_title), WRITETIME(author), WRITETIME(publisher) from catalog.book_awards;
```