

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# Amazon Keyspaces 中的客户端时间戳
<a name="client-side-timestamps"></a>

在 Amazon Keyspaces 中，客户端时间戳是与 Cassandra 兼容的时间戳，表中的每个单元格都会保留这些时间戳。您可以让您的客户端应用程序确定写入顺序，从而使用客户端时间戳来解决冲突。例如，当全球分布式应用程序的客户端对相同的数据进行更新时，客户端时间戳将保留在客户端上进行更新的顺序。Amazon Keyspaces 使用这些时间戳来处理写入操作。

Amazon Keyspaces 客户端时间戳是完全托管的。您不必管理诸如清理和压缩策略之类的低级系统设置。

删除数据时，会使用 Tombstone 标记行来进行删除。Amazon Keyspaces 会自动移除 Tombstoned 数据（通常在 10 天内），而不会影响您的应用程序性能或可用性。Tombstoned 数据不可用于 Data Manipulation Language (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 中配置 read/write 容量模式](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`：删除 Tombstoned 数据所需的删除操作数。

有关如何监控 CloudWatch 指标的更多信息，请参阅[使用亚马逊监控亚马逊密钥空间 CloudWatch](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>

按照这些示例使用亚马逊密钥空间、Cassandra 查询语言 (CQL) 或 Amazon Keyspaces 创建启用客户端时间戳的新 Amazon Keyspaces AWS 管理控制台表 AWS Command Line Interface

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

**使用客户端时间戳创建新表（控制台）**

1. [登录并在家中打开 Amazon Keyspaces 控制台。 AWS 管理控制台 https://console.aws.amazon.com/keyspaces/](https://console.aws.amazon.com/keyspaces/home)

1. 在导航窗格中，选择**表**，然后选择**创建表**。

1. 在**创建表**页面的**表详细信息**部分中，选择一个键空间并为新表提供一个名称。

1. 在**架构**部分，为您的表创建架构。

1. 在**表设置**部分，选择**自定义设置**。

1. 继续查看**客户端时间戳**。

   选择**打开客户端时间戳**，为表打开客户端时间戳。

1. 选择**创建表**。您的表是在打开客户端时间戳的情况下创建的。

------
#### [ 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、Cassandra 查询语言 (CQL) AWS 管理控制台或，为现有表开启客户端时间戳。 AWS Command Line Interface

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

**为现有表打开客户端时间戳（控制台）**

1. [登录并在家中打开 Amazon Keyspaces 控制台。 AWS 管理控制台 https://console.aws.amazon.com/keyspaces/](https://console.aws.amazon.com/keyspaces/home)

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>

打开客户端时间戳后，您可以在 `INSERT`、`UPDATE` 和 `DELETE` 语句中使用 `USING TIMESTAMP` 子句传递时间戳。

时间戳值是一个 `bigint` 值，表示自标准基本时间（称为 epoch）以来的微秒数：1970 年 1 月 1 日 00:00:00 GMT。客户端提供的时间戳必须介于当前挂钟时间的过去 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;
```