AWS Command Line Interface v2 を使用して DynamoDB グローバルセカンダリインデックスを管理する
次のコード例は、グローバルセカンダリインデックスのライフサイクル全体を管理する方法を示しています。
グローバルセカンダリインデックスを含むテーブルを作成します。
新しい GSI を既存のテーブルに追加します。
GSI ウォームスループットを更新 (増加) します。
GSI を使用してデータをクエリします。
GSI を削除します。
- Bash
-
- Bash スクリプトを使用した AWS CLI
-
グローバルセカンダリインデックスを含むテーブルを作成します。
# Create a table with a GSI aws dynamodb create-table \ --table-name MusicCollection \ --attribute-definitions \ AttributeName=Artist,AttributeType=S \ AttributeName=SongTitle,AttributeType=S \ AttributeName=AlbumTitle,AttributeType=S \ --key-schema \ AttributeName=Artist,KeyType=HASH \ AttributeName=SongTitle,KeyType=RANGE \ --billing-mode PAY_PER_REQUEST \ --global-secondary-indexes \ "IndexName=AlbumIndex,\ KeySchema=[{AttributeName=AlbumTitle,KeyType=HASH}],\ Projection={ProjectionType=ALL}"
新しい (オンデマンド) GSI を既存のテーブルに追加します。
# Add a new GSI to an existing table aws dynamodb update-table \ --table-name MusicCollection \ --attribute-definitions \ AttributeName=Genre,AttributeType=S \ --global-secondary-index-updates \ "[{\"Create\":{\"IndexName\":\"GenreIndex\",\ \"KeySchema\":[{\"AttributeName\":\"Genre\",\"KeyType\":\"HASH\"}],\ \"Projection\":{\"ProjectionType\":\"ALL\"}}}]"
GSI ウォームスループットを更新 (増加) します。
# Increase the warm throughput of a GSI (default values are 12k reads, 4k writes) aws dynamodb update-table \ --table-name MusicCollection \ --global-secondary-index-updates \ "[{\"Update\":{\"IndexName\":\"AlbumIndex\",\ \"WarmThroughput\":{\"ReadUnitsPerSecond\":15000,\"WriteUnitsPerSecond\":6000}}}]"
GSI を使用してデータをクエリします。
# Query the AlbumIndex GSI aws dynamodb query \ --table-name MusicCollection \ --index-name AlbumIndex \ --key-condition-expression "AlbumTitle = :album" \ --expression-attribute-values '{":album":{"S":"Let It Be"}}' # Query the GenreIndex GSI aws dynamodb query \ --table-name MusicCollection \ --index-name GenreIndex \ --key-condition-expression "Genre = :genre" \ --expression-attribute-values '{":genre":{"S":"Jazz"}}'
GSI を削除します。
# Delete a GSI from a table aws dynamodb update-table \ --table-name MusicCollection \ --global-secondary-index-updates \ "[{\"Delete\":{\"IndexName\":\"GenreIndex\"}}]"
-
API の詳細については、「AWS CLI コマンドリファレンス」の以下のトピックを参照してください。
-
AWS SDK デベロッパーガイドとコード例の詳細なリストについては、「AWS SDK で DynamoDB を使用する」を参照してください。このトピックには、使用開始方法に関する情報と、以前の SDK バージョンの詳細も含まれています。
ブラウザからの Lambda 関数の呼び出し
リソースベースのポリシーを管理する