Sunday, 23 February 2020

Using Amazon DynamoDB with the AWS CLI - Part 6

To list the AWS CLI commands for DynamoDB, use the following command.
aws dynamodb help
Before you run any commands, set your default credentials. For more information, see Configuring the AWS CLI.
For example, the following command creates a table named MusicCollection.
$ aws dynamodb create-table \ --table-name MusicCollection \ --attribute-definitions AttributeName=Artist,AttributeType=S AttributeName=SongTitle,AttributeType=S \ --key-schema AttributeName=Artist,KeyType=HASH AttributeName=SongTitle,KeyType=RANGE \ --provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1
{
  "TableDescription": {
    "AttributeDefinitions": [
      {
        "AttributeName": "Artist",
        "AttributeType": "S"
      },
      {
        "AttributeName": "SongTitle",
        "AttributeType": "S"
      }
    ],
    "TableName": "MusicCollection",
    "KeySchema": [
      {
        "AttributeName": "Artist",
        "KeyType": "HASH"
      },
      {
        "AttributeName": "SongTitle",
        "KeyType": "RANGE"
      }
    ],
    "TableStatus": "CREATING",
    "CreationDateTime": 1582461871.325,
    "ProvisionedThroughput": {
      "NumberOfDecreasesToday": 0,
      "ReadCapacityUnits": 1,
      "WriteCapacityUnits": 1
    },
    "TableSizeBytes": 0,
    "ItemCount": 0,
    "TableArn": "arn:aws:dynamodb:ap-south-1:809160705184:table/MusicCollection",
    "TableId": "00c2767a-1337-4f6d-ae81-87792185da75"
  }
}

You can add new lines to the table with commands similar to those shown in the following example. These examples use a combination of shorthand syntax and JSON.

$ aws dynamodb put-item \ --table-name MusicCollection \ --item '{ "Artist": {"S": "No One You Know"}, "SongTitle": {"S": "Call Me Today"} , "AlbumTitle": {"S": "Somewhat Famous"} }' \ --return-consumed-capacity TOTAL { "ConsumedCapacity": { "CapacityUnits": 1.0, "TableName": "MusicCollection" } } $ aws dynamodb put-item \ --table-name MusicCollection \ --item '{ "Artist": {"S": "Acme Band"}, "SongTitle": {"S": "Happy Day"} , "AlbumTitle": {"S": "Songs About Life"} }' \ --return-consumed-capacity TOTAL
{
  "ConsumedCapacity": {
    "CapacityUnits": 1.0,
    "TableName": "MusicCollection"
  }

}
You can use that file to issue a query request using the AWS CLI.
$ aws dynamodb query --table-name MusicCollection \ --key-condition-expression "Artist = :v1 AND SongTitle = :v2" \ --expression-attribute-values file://expression-attributes.json
{
  "Count": 1,
  "Items": [
    {
      "AlbumTitle": {
        "S": "Somewhat Famous"
      },
      "SongTitle": {
        "S": "Call Me Today"
      },
      "Artist": {
        "S": "No One You Know"
      }
    }
  ],
  "ScannedCount": 1,
  "ConsumedCapacity": null

}

For example, the following command delete a table named MusicCollection.

$ aws dynamodb delete-table --table-name MusicCollection
{ "TableDescription": { "TableName": "MusicCollection", "TableStatus": "DELETING", "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 1, "WriteCapacityUnits": 1 }, "TableSizeBytes": 0, "ItemCount": 0, "TableArn": "arn:aws:dynamodb:ap-south-1:809160705184:table/MusicCollection", "TableId": "00c2767a-1337-4f6d-ae81-87792185da75" } }


For example, the following command list a table named.
$ aws dynamodb list-tables { "TableNames": [ "MusicsCollection", "VideosCollection", "SongsCollection", "DocumentCollection" ] }

No comments:

Post a Comment