Accessing DynamoDB through the CLI

When accessing the DynamoDB service via the CLI, we can use the AWS CLI integrated shorthand or direct JSON commands and JMESpath scripting. In the next example, we will be creating the same DynamoDB table as before: users, with primary key (also known as the HASH key) username and lastname as the sort key (also known as the RANGE key). Once we have our AWS CLI set up, we simply send the aws dynamodb create-table command with the following attributes:

  • --table name: Defines the name of the table
  • --attribute-definitions: Defines the attributes
  • AttributeName: The name of our attribute (not the value!)
  • AttributeType: The type of value (S = string, N = number, B = binary)
  • --key-schema: Defines the primary and sort key to use
  • KeyType: HASH defines the primary key, RANGE defines the sort key
  • --provisione-throughput: Defines the RCUs and WCU

So, now, let's put all of this together:

aws dynamodb create-table 
--table-name users
--attribute-definitions
AttributeName=username,AttributeType=S
AttributeName=last_name,AttributeType=S
--key-schema AttributeName=username,KeyType=HASH AttributeName=lastn_name,KeyType=RANGE
--provisioned-throughput ReadCapacityUnits=100,WriteCapacityUnits=400

Now that we have the table, we can address the table and write items with the aws dynamodb put-item command:

  • --table-name: Specifies the table we want to write into
  • --item: Defines the item in key-value pairs
  • "keyname": {"data type":"value"}: Defines the key we are writing to, the data type (S/N/B), and the value of the data being written

So, let's put this all together:

aws dynamodb put-item 
--table-name users
--item '{
"username": {"S": "thownsr"},
"last_name": {"S": "Thowns"},
"first_name": {"S": "Rick"} }'

If we want to know how many capacity units our write has consumed, we can add the --return-consumed-capacity TOTAL switch at the end of our command to determine the TOTAL number of capacity units the command consumed in the API response.

To read the whole item from the DynamoDB table, we can issue the aws dynamodb get-item command, where we need to specify the --table-name for naming our table and --key, which is the primary and optionally the sort key for the item. To retrieve the data from the DynamoDB table for our user with the primary key username:thownsr and sort key lastn_name:Thowns, we would issue the following command:

aws dynamodb get-item 
--table-name users
--key '{
"username": {"S": "thownsr"},
"last_name": {"S": "Thowns"},

The response from the query would be the following:

HTTP/1.1 200 OK
x-amzn-RequestId: <RequestId>
x-amz-crc32: <Checksum>
Content-Type: application/x-amz-json-1.0
Content-Length: <PayloadSizeBytes>
Date: <Date>
{
"Item": {
"username": { "S": ["thownsr"] },
"last_name": { "S": ["Thowns"] },
"first_name": { "S": ["Rick"] },
}
}

For each command, we can source the information from a file by specifying file://input-file.json as the value for each shorthand command. This gives us the ability to easily import large datasets from JSON-formatted files into DynamoDB. As you can see, the DynamoDB service can be easily controlled through the CLI and this gives us a lot of power when performing queries.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.224.226