第 1 步:创建一个表
在这一步中,您将在 Amazon DynamoDB 中创建一个 Music
表。该表具有以下详细信息:
-
分区键 —
Artist
-
排序键 —
SongTitle
有关表操作的更多信息,请参阅 使用 DynamoDB 中的表和数据。
开始之前,请确保您已完成 先决条件 – 入门教程 中的步骤。
要使用 DynamoDB 控制台创建新的 Music
表:
登录 Amazon Web Services Management Console,打开 DynamoDB 控制台:https://console.aws.amazon.com/dynamodb/
。 -
在控制台左侧的导航窗格中,选择 Dashboard (控制面板)。
-
在控制台的右侧,选择 Create Table (创建表)。
-
按以下所示输入表详细信息:
-
对于表名称,输入
Music
。 -
对于分区键,输入
Artist
。 -
输入
SongTitle
作为排序键。 -
保持选择 Default settings (默认设置)。
-
-
选择 Create (创建) 以创建表。
以下 Amazon CLI 示例使用 create-table
创建一个新的 Music
表。
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": { "TableArn": "arn:aws:dynamodb:us-west-2:522194210714:table/Music", "AttributeDefinitions": [ { "AttributeName": "Artist", "AttributeType": "S" }, { "AttributeName": "SongTitle", "AttributeType": "S" } ], "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "WriteCapacityUnits": 5, "ReadCapacityUnits": 5 }, "TableClassSummary": { "LastUpdateDateTime": 1558028402.69, "TableClass": "STANDARD" }, "TableSizeBytes": 0, "TableName": "Music", "TableStatus": "CREATING", "TableId": "d04c7240-0e46-435d-b231-d54091fe1017", "KeySchema": [ { "KeyType": "HASH", "AttributeName": "Artist" }, { "KeyType": "RANGE", "AttributeName": "SongTitle" } ], "ItemCount": 0, "CreationDateTime": 1558028402.69 } }
请注意,TableStatus
字段的值设置为 CREATING
。
要验证 DynamoDB 是否已完成创建 Music
表,请使用 describe-table
命令。
aws dynamodb describe-table --table-name Music | grep TableStatus
此命令将返回以下结果。在 DynamoDB 创建完表后,TableStatus
字段的值将设置为 ACTIVE
。
"TableStatus": "ACTIVE",
表处于 ACTIVE
状态后,通过运行以下命令在表上启用 DynamoDB 的时间点恢复就被认为是最佳实践:
aws dynamodb update-continuous-backups \ --table-name Music \ --point-in-time-recovery-specification \ PointInTimeRecoveryEnabled=true
使用时间点恢复实现连续备份会带来成本影响。有关定价的更多信息,请参阅 Amazon DynamoDB 定价
创建新表后,继续完成 第 2 步:使用控制台或 Amazon CLI 向表中写入数据。