将 UpdateTable 与 Amazon SDK 或 CLI 配合使用 - Amazon DynamoDB
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

UpdateTable 与 Amazon SDK 或 CLI 配合使用

以下代码示例演示如何使用 UpdateTable

CLI
Amazon CLI

示例 1:修改表的计费模式

以下 update-table 示例增加了 MusicCollection 表上的预置读取和写入容量。

aws dynamodb update-table \ --table-name MusicCollection \ --billing-mode PROVISIONED \ --provisioned-throughput ReadCapacityUnits=15,WriteCapacityUnits=10

输出:

{ "TableDescription": { "AttributeDefinitions": [ { "AttributeName": "AlbumTitle", "AttributeType": "S" }, { "AttributeName": "Artist", "AttributeType": "S" }, { "AttributeName": "SongTitle", "AttributeType": "S" } ], "TableName": "MusicCollection", "KeySchema": [ { "AttributeName": "Artist", "KeyType": "HASH" }, { "AttributeName": "SongTitle", "KeyType": "RANGE" } ], "TableStatus": "UPDATING", "CreationDateTime": "2020-05-26T15:59:49.473000-07:00", "ProvisionedThroughput": { "LastIncreaseDateTime": "2020-07-28T13:18:18.921000-07:00", "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 15, "WriteCapacityUnits": 10 }, "TableSizeBytes": 182, "ItemCount": 2, "TableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection", "TableId": "abcd0123-01ab-23cd-0123-abcdef123456", "BillingModeSummary": { "BillingMode": "PROVISIONED", "LastUpdateToPayPerRequestDateTime": "2020-07-28T13:14:48.366000-07:00" } } }

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的更新表

示例 2:创建全局二级索引

下面的示例在 MusicCollection 表上添加了全局二级索引。

aws dynamodb update-table \ --table-name MusicCollection \ --attribute-definitions AttributeName=AlbumTitle,AttributeType=S \ --global-secondary-index-updates file://gsi-updates.json

gsi-updates.json 的内容:

[ { "Create": { "IndexName": "AlbumTitle-index", "KeySchema": [ { "AttributeName": "AlbumTitle", "KeyType": "HASH" } ], "ProvisionedThroughput": { "ReadCapacityUnits": 10, "WriteCapacityUnits": 10 }, "Projection": { "ProjectionType": "ALL" } } } ]

输出:

{ "TableDescription": { "AttributeDefinitions": [ { "AttributeName": "AlbumTitle", "AttributeType": "S" }, { "AttributeName": "Artist", "AttributeType": "S" }, { "AttributeName": "SongTitle", "AttributeType": "S" } ], "TableName": "MusicCollection", "KeySchema": [ { "AttributeName": "Artist", "KeyType": "HASH" }, { "AttributeName": "SongTitle", "KeyType": "RANGE" } ], "TableStatus": "UPDATING", "CreationDateTime": "2020-05-26T15:59:49.473000-07:00", "ProvisionedThroughput": { "LastIncreaseDateTime": "2020-07-28T12:59:17.537000-07:00", "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 15, "WriteCapacityUnits": 10 }, "TableSizeBytes": 182, "ItemCount": 2, "TableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection", "TableId": "abcd0123-01ab-23cd-0123-abcdef123456", "BillingModeSummary": { "BillingMode": "PROVISIONED", "LastUpdateToPayPerRequestDateTime": "2020-07-28T13:14:48.366000-07:00" }, "GlobalSecondaryIndexes": [ { "IndexName": "AlbumTitle-index", "KeySchema": [ { "AttributeName": "AlbumTitle", "KeyType": "HASH" } ], "Projection": { "ProjectionType": "ALL" }, "IndexStatus": "CREATING", "Backfilling": false, "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 10, "WriteCapacityUnits": 10 }, "IndexSizeBytes": 0, "ItemCount": 0, "IndexArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection/index/AlbumTitle-index" } ] } }

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的更新表

示例 3:在表上启用 DynamoDB Streams

以下命令在 MusicCollection 表上启用 DynamoDB Streams。

aws dynamodb update-table \ --table-name MusicCollection \ --stream-specification StreamEnabled=true,StreamViewType=NEW_IMAGE

输出:

{ "TableDescription": { "AttributeDefinitions": [ { "AttributeName": "AlbumTitle", "AttributeType": "S" }, { "AttributeName": "Artist", "AttributeType": "S" }, { "AttributeName": "SongTitle", "AttributeType": "S" } ], "TableName": "MusicCollection", "KeySchema": [ { "AttributeName": "Artist", "KeyType": "HASH" }, { "AttributeName": "SongTitle", "KeyType": "RANGE" } ], "TableStatus": "UPDATING", "CreationDateTime": "2020-05-26T15:59:49.473000-07:00", "ProvisionedThroughput": { "LastIncreaseDateTime": "2020-07-28T12:59:17.537000-07:00", "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 15, "WriteCapacityUnits": 10 }, "TableSizeBytes": 182, "ItemCount": 2, "TableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection", "TableId": "abcd0123-01ab-23cd-0123-abcdef123456", "BillingModeSummary": { "BillingMode": "PROVISIONED", "LastUpdateToPayPerRequestDateTime": "2020-07-28T13:14:48.366000-07:00" }, "LocalSecondaryIndexes": [ { "IndexName": "AlbumTitleIndex", "KeySchema": [ { "AttributeName": "Artist", "KeyType": "HASH" }, { "AttributeName": "AlbumTitle", "KeyType": "RANGE" } ], "Projection": { "ProjectionType": "INCLUDE", "NonKeyAttributes": [ "Year", "Genre" ] }, "IndexSizeBytes": 139, "ItemCount": 2, "IndexArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection/index/AlbumTitleIndex" } ], "GlobalSecondaryIndexes": [ { "IndexName": "AlbumTitle-index", "KeySchema": [ { "AttributeName": "AlbumTitle", "KeyType": "HASH" } ], "Projection": { "ProjectionType": "ALL" }, "IndexStatus": "ACTIVE", "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 10, "WriteCapacityUnits": 10 }, "IndexSizeBytes": 0, "ItemCount": 0, "IndexArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection/index/AlbumTitle-index" } ], "StreamSpecification": { "StreamEnabled": true, "StreamViewType": "NEW_IMAGE" }, "LatestStreamLabel": "2020-07-28T21:53:39.112", "LatestStreamArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection/stream/2020-07-28T21:53:39.112" } }

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的更新表

示例 4:启用服务器端加密

以下示例在 MusicCollection 表上启用服务器端加密。

aws dynamodb update-table \ --table-name MusicCollection \ --sse-specification Enabled=true,SSEType=KMS

输出:

{ "TableDescription": { "AttributeDefinitions": [ { "AttributeName": "AlbumTitle", "AttributeType": "S" }, { "AttributeName": "Artist", "AttributeType": "S" }, { "AttributeName": "SongTitle", "AttributeType": "S" } ], "TableName": "MusicCollection", "KeySchema": [ { "AttributeName": "Artist", "KeyType": "HASH" }, { "AttributeName": "SongTitle", "KeyType": "RANGE" } ], "TableStatus": "ACTIVE", "CreationDateTime": "2020-05-26T15:59:49.473000-07:00", "ProvisionedThroughput": { "LastIncreaseDateTime": "2020-07-28T12:59:17.537000-07:00", "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 15, "WriteCapacityUnits": 10 }, "TableSizeBytes": 182, "ItemCount": 2, "TableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection", "TableId": "abcd0123-01ab-23cd-0123-abcdef123456", "BillingModeSummary": { "BillingMode": "PROVISIONED", "LastUpdateToPayPerRequestDateTime": "2020-07-28T13:14:48.366000-07:00" }, "LocalSecondaryIndexes": [ { "IndexName": "AlbumTitleIndex", "KeySchema": [ { "AttributeName": "Artist", "KeyType": "HASH" }, { "AttributeName": "AlbumTitle", "KeyType": "RANGE" } ], "Projection": { "ProjectionType": "INCLUDE", "NonKeyAttributes": [ "Year", "Genre" ] }, "IndexSizeBytes": 139, "ItemCount": 2, "IndexArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection/index/AlbumTitleIndex" } ], "GlobalSecondaryIndexes": [ { "IndexName": "AlbumTitle-index", "KeySchema": [ { "AttributeName": "AlbumTitle", "KeyType": "HASH" } ], "Projection": { "ProjectionType": "ALL" }, "IndexStatus": "ACTIVE", "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 10, "WriteCapacityUnits": 10 }, "IndexSizeBytes": 0, "ItemCount": 0, "IndexArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection/index/AlbumTitle-index" } ], "StreamSpecification": { "StreamEnabled": true, "StreamViewType": "NEW_IMAGE" }, "LatestStreamLabel": "2020-07-28T21:53:39.112", "LatestStreamArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection/stream/2020-07-28T21:53:39.112", "SSEDescription": { "Status": "UPDATING" } } }

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的更新表

  • 有关 API 详细信息,请参阅《Amazon CLI 命令参考》中的 UpdateTable

PowerShell
适用于 PowerShell 的工具

示例 1:更新给定表的预置吞吐量。

Update-DDBTable -TableName "myTable" -ReadCapacity 10 -WriteCapacity 5
  • 有关 API 详细信息,请参阅《Amazon Tools for PowerShell Cmdlet 参考》中的 UpdateTable

有关 Amazon SDK 开发人员指南和代码示例的完整列表,请参阅 结合使用 DynamoDB 与 Amazon SDK。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。