Reading items

We can read items using get-item, query, or scan:

  1. Use the aws dynamodb get-item CLI command, passing all key fields:
aws dynamodb get-item 
--table-name my_table
--key '{"id":{"S":"002"},"datetime":{"N":"1536898295"}}'
--return-consumed-capacity TOTAL
--region us-east-1
--profile admin

This command will return a single item, along with metadata describing the result. The result part returned is shown in the following screenshot:

The metadata part is shown in the following screenshot:

I had added the return-consumed-capacity option to return the aggregate ConsumedCapacity

  1. Use the query action, passing the partition key field and a filter expression:
aws dynamodb query 
--table-name my_table
--key-condition-expression "id=:id"
--filter-expression "field1=:field1"
--expression-attribute-values '{":id":{"S":"001"}, ":field1":{"N":"20"}}'
--return-consumed-capacity TOTAL
--region us-east-1
--profile admin

The sort key is optional here. The previous command's execution will return a single matching item, along with the following metadata that describes the result:

It scans for the document within the whole current partition, as we specified only the partition key, and hence ScannedCount is 2

  1. Use this query, passing all key fields and a filter expression:
aws dynamodb query 
--table-name my_table
--key-condition-expression "id=:id and dateandtime=:dateandtime"
--filter-expression "field1=:field1"
--expression-attribute-values '{":id":{"S":"001"}, ":field1":{"N":"20"}, ":dateandtime":{"N":"1536898265"}, ":field1":{"N":"20"}}'
--return-consumed-capacity TOTAL
--region us-east-1
--profile admin

 This command will return a single matching item along with the following metadata:

It scans for one document as we specified both partition key and sort key, and hence ScannedCount is 1You may also query with only the partition key without the sort key or filter expression (usage available with code files), in which case the count will be the number of matching items (here 2) and the scanned count will be the partition size (here 2).

  1. Execute the same command as a strongly consistent read using the consistent-read option:
aws dynamodb query 
--table-name my_table
--key-condition-expression "id=:id and dateandtime=:dateandtime"
--filter-expression "field1=:field1"
--expression-attribute-values '{":id":{"S":"001"}, ":field1":{"N":"20"}, ":dateandtime":{"N":"1536898265"}, ":field1":{"N":"20"}}'
--consistent-read
--return-consumed-capacity TOTAL
--region us-east-1
--profile admin

This command will return a single matching item along with the following metadata:

Note that the command now utilizes one capacity unit for read with strong consistency, instead of 0.5 with eventual consistency.

  1. Use scan, passing key fields and a filter expression:
aws dynamodb scan 
--table-name my_table
--filter-expression "id=:id and dateandtime=:dateandtime and field1=:field1"
--expression-attribute-values '{":id":{"S":"002"}, ":dateandtime":{"N":"1536898295"}, ":field1":{"S":"DEF"}}'
--return-consumed-capacity TOTAL
--region us-east-1
--profile admin

This command will return a single matching item along with the following metadata:

Here, the ScannedCount is 3, which is the table size. The scan command scans the entire table even if we specify both partition key and sort key. You may scan with the filter expression without the partition key or sort key (usage available with the code files), in which case the count will be the number of matching rows (here 1) and the scanned count will still be the table size, which is 3. You may scan without the filter expression (usage available with the code files), in which case the count and scanned count both will be the number of items in the table. You may also scan with strong consistency using the consistent-read option, similar to the query example we saw before.

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

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