Verwalten globaler sekundärer Indizes in DynamoDB mit AWS Command Line Interface v2 - Amazon-DynamoDB

Verwalten globaler sekundärer Indizes in DynamoDB mit AWS Command Line Interface v2

Im folgenden Codebeispiel wird gezeigt, wie Sie den gesamten Lebenszyklus globaler sekundärer Indizes verwalten.

  • Erstellen Sie eine Tabelle mit einem globalen sekundären Index.

  • Fügen Sie einer vorhandenen Tabelle einen neuen GSI hinzu.

  • Aktualisieren (erhöhen) Sie den GSI-Warmdurchsatz.

  • Fragen Sie Daten mithilfe von GSIs ab.

  • Löschen Sie einen GSI.

Bash
AWS CLI mit Bash-Skript

Erstellen Sie eine Tabelle mit einem globalen sekundären Index.

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

Fügen Sie einer vorhandenen Tabelle einen neuen (On-Demand-)GSI hinzu.

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

Aktualisieren (erhöhen) Sie den GSI-Warmdurchsatz.

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

Fragen Sie Daten mithilfe von GSIs ab.

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

Löschen Sie einen GSI.

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

Eine vollständige Liste der AWS-SDK-Entwicklerhandbücher und Code-Beispiele finden Sie unter Verwenden von DynamoDB mit einem AWS-SDK. Dieses Thema enthält auch Informationen zu den ersten Schritten und Details zu früheren SDK-Versionen.