在 Amazon Keyspaces 中使用客户端时间戳 - Amazon Keyspaces (for Apache Cassandra)
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

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

在 Amazon Keyspaces 中使用客户端时间戳

你可以使用 Amazon Keyspaces(适用于 Apache Cassandra)控制台、Cassandra 查询语言Amazon Command Line Interface (CQLAmazon CLI)、Amazon SDK 和 () 开启客户端时间戳。本节提供有关如何在新表和现有表上启用客户端时间戳以及如何在查询中使用客户端时间戳的示例。有关 API 的更多信息,请参阅亚马逊Keyspaces API 参考

重要

无法关闭客户端时间戳。开启客户端时间戳是一次性的更改。Amazon Keyspaces 不提供在不删除表的情况下将其关闭的选项。

在启用客户端时间戳的情况下创建新表(控制台)

按照以下步骤使用 Amazon Keyspaces 控制台创建包含客户端时间戳的新表。

使用客户端时间戳创建新表(控制台)
  1. 登录并通过以下Amazon Web Services Management Console打开 Amazon Keyspaces 控制台:https://console.aws.amazon.com/keyspaces/home

  2. 在导航窗格中,选择 Tables (表),然后选择 Create table (创建表)

  3. 表详细信息部分的创建页面上,选择一个键空间并为新表提供名称。

  4. 架构部分中,为您的表创建架构。

  5. 表格设置部分中,选择自定义设置

  6. 继续使用客户端时间戳

    选择 “开启客户端时间戳” 以启用表格的客户端时间戳。

  7. 选择 Create Table(创建表)。您的表是在启用客户端时间戳的情况下创建的。

在现有表上启用客户端时间戳(控制台)

按照以下步骤使用 Amazon Keyspaces 为现有表开启客户端时间戳Amazon Web Services Management Console。

为现有表启用客户端时间戳(控制台)
  1. 登录并通过以下Amazon Web Services Management Console打开 Amazon Keyspaces 控制台:https://console.aws.amazon.com/keyspaces/home

  2. 选择要更新的表,然后选择其他设置选项卡。

  3. 其他设置选项卡上,转到修改客户端时间戳,然后选择开启客户端时间戳

  4. 选择 “保存更改” 以更改表格的设置。

在启用客户端时间戳的情况下创建新表 (CQL)

要在创建新表时启用客户端时间戳,可以使用以下 CQL 语句。

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

要确认新表的客户端时间戳设置,请使用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'}

使用ALTER TABLE (CQL) 为现有表开启客户端时间戳

要为现有表启用客户端时间戳,可以使用以下 CQL 语句。

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

要确认新表的客户端时间戳设置,请使用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'}

在启用客户端时间戳的情况下创建新表 (CLI)

要在创建新表时启用客户端时间戳,可以使用以下 CLI 语句。

./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}]'

要确认新表的客户端时间戳已启用,请运行以下代码。

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

该输出应该如下所示。

{ "keyspaceName": "my_keyspace", "tableName": "my_table", "resourceArn": "arn:aws:cassandra:us-east-2:555555555555:/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": "" } }

在现有表上启用客户端时间戳 (CLI)

要使用 CLI 为现有表启用客户端时间戳,可以使用以下代码。

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

要确认表的客户端时间戳已启用,请运行以下代码。

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

该输出应该如下所示。

{ "keyspaceName": "my_keyspace", "tableName": "my_table", "resourceArn": "arn:aws:cassandra:us-east-2:555555555555:/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": "" } }

在数据操作语言 (DML) 语句中使用客户端时间戳

开启客户端时间戳后,可以在带有子句的INSERTUPDATE、和DELETEUSING TIMESTAMP句中传递时间戳。时间戳值 abigint 表示自标准基准时间(即epoch:格林威治标准时间 1970 年 1 月 1 日 00:00:00)以来的微秒数。客户端提供的时间戳必须介于当前挂钟时间的过去 2 天和future 5 分钟之间。Amazon Keyspaces 在数据的生命周期内保留时间戳元数据。您可以使用该WRITETIME函数查找过去几年出现的时间戳。有关 CQL 语法的更多信息,请参阅Amazon Keyspaces 中的 DML 语句(数据操作语言)

以下 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;