Etapa 6: criar um índice secundário global - Amazon DynamoDB

Etapa 6: criar um índice secundário global

Nesta etapa, você cria um índice secundário global para a tabela Music que você criou em Etapa 1: criar uma tabela.

Para obter mais informações sobre índices secundários globais, consulte Como usar índices secundários globais no DynamoDB.

Para usar o console do Amazon DynamoDB para criar um índice secundário global AlbumTitle-index para a tabela Music:

  1. Abra o console do DynamoDB em https://console.aws.amazon.com/dynamodb/.

  2. No painel de navegação à esquerda, selecione Tables (Tabelas).

  3. Escolha a tabela Music (Música) na lista de tabelas.

  4. Escolha a guia Indexes (Índices) da tabela Music.

  5. Escolha Create index (Criar índice).

  6. Na página Criar índice secundário global, faça o seguinte:

    1. Em Partition key (Chave de partição), insira AlbumTitle.

    2. (Opcional) Em Nome do índice, insira AlbumTitle-index.

    3. Mantenha a seleção padrão para as outras configurações na página e escolha Criar índice.

      nota

      A criação de um índice requer algum tempo de espera até que o índice seja atualizado para aparecer como ACTIVE.

O exemplo da AWS CLI a seguir cria um índice secundário global AlbumTitle-index para a tabela Music usando update-table.

Linux

aws dynamodb update-table \ --table-name Music \ --attribute-definitions AttributeName=AlbumTitle,AttributeType=S \ --global-secondary-index-updates \ "[{\"Create\":{\"IndexName\": \"AlbumTitle-index\",\"KeySchema\":[{\"AttributeName\":\"AlbumTitle\",\"KeyType\":\"HASH\"}], \ \"ProvisionedThroughput\": {\"ReadCapacityUnits\": 10, \"WriteCapacityUnits\": 5 },\"Projection\":{\"ProjectionType\":\"ALL\"}}}]"

CMD do Windows

aws dynamodb update-table ^ --table-name Music ^ --attribute-definitions AttributeName=AlbumTitle,AttributeType=S ^ --global-secondary-index-updates "[{\"Create\":{\"IndexName\": \"AlbumTitle-index\",\"KeySchema\":[{\"AttributeName\":\"AlbumTitle\",\"KeyType\":\"HASH\"}], \"ProvisionedThroughput\": {\"ReadCapacityUnits\": 10, \"WriteCapacityUnits\": 5},\"Projection\":{\"ProjectionType\":\"ALL\"}}}]"

O uso de update-table retorna o seguinte resultado de exemplo.

{ "TableDescription": { "TableArn": "arn:aws:dynamodb:us-west-2:111122223333:table/Music", "AttributeDefinitions": [ { "AttributeName": "AlbumTitle", "AttributeType": "S" }, { "AttributeName": "Artist", "AttributeType": "S" }, { "AttributeName": "SongTitle", "AttributeType": "S" } ], "GlobalSecondaryIndexes": [ { "IndexSizeBytes": 0, "IndexName": "AlbumTitle-index", "Projection": { "ProjectionType": "ALL" }, "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "WriteCapacityUnits": 5, "ReadCapacityUnits": 10 }, "IndexStatus": "CREATING", "Backfilling": false, "KeySchema": [ { "KeyType": "HASH", "AttributeName": "AlbumTitle" } ], "IndexArn": "arn:aws:dynamodb:us-west-2:111122223333:table/Music/index/AlbumTitle-index", "ItemCount": 0 } ], "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "WriteCapacityUnits": 5, "ReadCapacityUnits": 10 }, "TableSizeBytes": 0, "TableName": "Music", "TableStatus": "UPDATING", "TableId": "a04b7240-0a46-435b-a231-b54091ab1017", "KeySchema": [ { "KeyType": "HASH", "AttributeName": "Artist" }, { "KeyType": "RANGE", "AttributeName": "SongTitle" } ], "ItemCount": 0, "CreationDateTime": 1558028402.69 } }

Observe que o valor do campo IndexStatus está definido como CREATING.

Para verificar se o DynamoDB terminou de criar o índice secundário global AlbumTitle-index, use o comando describe-table.

Linux

aws dynamodb describe-table --table-name Music | grep IndexStatus

CMD do Windows

aws dynamodb describe-table --table-name Music | findstr IndexStatus

Esse comando retorna o seguinte resultado. O índice estará pronto para ser utilizado quando o valor retornado do campo IndexStatus for ACTIVE.

"IndexStatus": "ACTIVE",

Depois, você poderá consultar o índice secundário global. Para obter detalhes, consulte Etapa 7: consultar o índice secundário global.

Importante

Aguarde um tempo depois de concluir a etapa 6. A próxima etapa requer a conclusão de todas as ações da etapa 6 e da tabela de GSI para ficar ACTIVE.