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

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

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

您可以使用 Amazon Keyspaces(Apache Cassandra 兼容)控制台、Cassandra Query Language (CQL)、Amazon SDK 和 Amazon Command Line Interface (Amazon CLI) 来打开客户端时间戳。本节提供了有关如何在新表和现有表上打开客户端时间戳以及如何在查询中使用客户端时间戳的示例。有关该 API 的更多信息,请参阅 Amazon Keyspaces API Reference

重要

客户端时间戳无法关闭。打开客户端时间戳是一次性更改。Amazon Keyspaces 没有在不删除表的情况下关闭客户端时间戳的选项。

创建打开客户端时间戳的新表(控制台)

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

创建打开客户端时间戳的新表(控制台)
  1. 登录 Amazon Web Services Management Console并打开 Amazon Keyspaces 控制台:https://console.aws.amazon.com/msk/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/msk/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": "" } }

在 Data Manipulation Language (DML) 语句中使用客户端时间戳

打开客户端时间戳后,您可以在 INSERTUPDATEDELETE 语句中使用 USING TIMESTAMP 子句传递时间戳。时间戳值是一个 bigint 值,表示自标准基本时间(称为 epoch)以来的微秒数:1970 年 1 月 1 日 00:00:00 GMT。客户端提供的时间戳必须介于当前挂钟时间的过去 2 天和未来 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;