第 1 步:创建一个表 - Amazon DynamoDB
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

第 1 步:创建一个表

在这一步中,您将在 Amazon DynamoDB 中创建一个 Music 表。该表具有以下详细信息:

  • 分区键 — Artist

  • 排序键 — SongTitle

有关表操作的更多信息,请参阅 使用 DynamoDB 中的表和数据

注意

开始之前,请确保您已完成 先决条件 – 入门教程 中的步骤。

要使用 DynamoDB 控制台创建新的 Music 表:

  1. 登录 Amazon Web Services Management Console,并打开 DynamoDB 控制台:https://console.aws.amazon.com/dynamodb/

  2. 在左侧导航窗格中,选择

  3. 选择创建表

  4. 按以下所示输入表详细信息

    1. 对于 表名称,输入 Music

    2. 对于分区键,输入 Artist

    3. 对于排序键,输入 SongTitle

  5. 对于表设置,保留默认设置中的默认选择内容。

  6. 选择创建表以创建表。

    “创建表”页面,其中已填写表详细信息。
  7. 在表处于 ACTIVE 状态后,建议您按照以下步骤在表上启用 DynamoDB 的时间点恢复

    1. 选择表名以打开表。

    2. 选择备份

    3. 时间点故障恢复(PITR)部分中选择编辑

    4. 编辑时间点故障恢复设置页面上,选择开启时间点故障恢复

    5. 选择 Save changes(保存更改)

以下 Amazon CLI 示例使用 create-table 创建一个新的 Music 表。

Linux

aws dynamodb create-table \ --table-name Music \ --attribute-definitions \ AttributeName=Artist,AttributeType=S \ AttributeName=SongTitle,AttributeType=S \ --key-schema \ AttributeName=Artist,KeyType=HASH \ AttributeName=SongTitle,KeyType=RANGE \ --provisioned-throughput \ ReadCapacityUnits=5,WriteCapacityUnits=5 \ --table-class STANDARD

Windows CMD

aws dynamodb create-table ^ --table-name Music ^ --attribute-definitions ^ AttributeName=Artist,AttributeType=S ^ AttributeName=SongTitle,AttributeType=S ^ --key-schema ^ AttributeName=Artist,KeyType=HASH ^ AttributeName=SongTitle,KeyType=RANGE ^ --provisioned-throughput ^ ReadCapacityUnits=5,WriteCapacityUnits=5 ^ --table-class STANDARD

使用 create-table 返回以下示例结果。

{ "TableDescription": { "AttributeDefinitions": [ { "AttributeName": "Artist", "AttributeType": "S" }, { "AttributeName": "SongTitle", "AttributeType": "S" } ], "TableName": "Music", "KeySchema": [ { "AttributeName": "Artist", "KeyType": "HASH" }, { "AttributeName": "SongTitle", "KeyType": "RANGE" } ], "TableStatus": "CREATING", "CreationDateTime": "2023-03-29T12:11:43.379000-04:00", "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 5, "WriteCapacityUnits": 5 }, "TableSizeBytes": 0, "ItemCount": 0, "TableArn": "arn:aws:dynamodb:us-east-1:111122223333:table/Music", "TableId": "60abf404-1839-4917-a89b-a8b0ab2a1b87", "TableClassSummary": { "TableClass": "STANDARD" } } } }

请注意,TableStatus 字段的值设置为 CREATING

要验证 DynamoDB 是否已完成创建 Music 表,请使用 describe-table 命令。

Linux

aws dynamodb describe-table --table-name Music | grep TableStatus

Windows CMD

aws dynamodb describe-table --table-name Music | findstr TableStatus

此命令将返回以下结果。在 DynamoDB 创建完表后,TableStatus 字段的值将设置为 ACTIVE

"TableStatus": "ACTIVE",

表处于 ACTIVE 状态后,通过运行以下命令在表上启用 DynamoDB 的时间点恢复就被认为是最佳实践:

Linux

aws dynamodb update-continuous-backups \ --table-name Music \ --point-in-time-recovery-specification \ PointInTimeRecoveryEnabled=true

Windows CMD

aws dynamodb update-continuous-backups --table-name Music --point-in-time-recovery-specification PointInTimeRecoveryEnabled=true

此命令将返回以下结果。

{ "ContinuousBackupsDescription": { "ContinuousBackupsStatus": "ENABLED", "PointInTimeRecoveryDescription": { "PointInTimeRecoveryStatus": "ENABLED", "EarliestRestorableDateTime": "2023-03-29T12:18:19-04:00", "LatestRestorableDateTime": "2023-03-29T12:18:19-04:00" } } }
注意

使用时间点恢复实现连续备份会带来成本影响。有关定价的更多信息,请参阅 Amazon DynamoDB 定价

创建新表后,继续完成 第 2 步:使用控制台或 Amazon CLI 向表中写入数据