Gestion des index secondaires globaux DynamoDB à l'aide de la version 2 AWS Command Line Interface - Amazon DynamoDB

Les traductions sont fournies par des outils de traduction automatique. En cas de conflit entre le contenu d'une traduction et celui de la version originale en anglais, la version anglaise prévaudra.

Gestion des index secondaires globaux DynamoDB à l'aide de la version 2 AWS Command Line Interface

L’exemple de code suivant montre comment gérer le cycle de vie complet des index secondaires globaux.

  • Création d’une table avec un index secondaire global.

  • Ajout d’un nouveau GSI à une table existante.

  • Mise à jour (augmentation) du débit à chaud du GSI.

  • Interrogez les données en utilisant GSIs.

  • Suppression d’un GSI.

Bash
AWS CLI avec le script Bash

Création d’une table avec un index secondaire global.

# 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}"

Ajout d’un nouveau GSI (à la demande) à une table existante.

# 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\"}}}]"

Mise à jour (augmentation) du débit à chaud du 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}}}]"

Interrogez les données en utilisant GSIs.

# 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"}}'

Suppression d’un GSI.

# Delete a GSI from a table aws dynamodb update-table \ --table-name MusicCollection \ --global-secondary-index-updates \ "[{\"Delete\":{\"IndexName\":\"GenreIndex\"}}]"

Pour obtenir la liste complète des guides de développement du AWS SDK et des exemples de code, consultezUtilisation de DynamoDB avec un kit AWS SDK. Cette rubrique comprend également des informations sur le démarrage et sur les versions précédentes de SDK.