Problem:
Retrieve Items from DynamoDB Tables - Get Item, Query & Scan.
Solution Summary:
You can retrieve items through various ways including Get, Query and Scan APIs.
Prerequisites:
For this lab, you need to have completed previous labs in this section: http://cloudericks.com/en/recipe/creating-and-doing-basic-operations-dyn...
Solution Steps:
Get Item
Gets an item based on the unique key (s) provided.
aws dynamodb get-item --table-name course_data --key {\"course_id\":{\"S\":\"c002\"},\"course_date\":{\"S\":\"2017/4/30\"}} --profile dynamodbdev
Note:
-
Default is eventual consistency; can be changed by adding –consistent-read.
-
Can’t be used against an index; local or global.
-
All key attributes needs to be provided (as applicable); else will get ValidationException.
-
You can get the consumed write capacity units for the command adding following option: --return-consumed-capacity TOTAL.
-
For no items, returns nothing.
Query
aws dynamodb query --table-name course_data --key-condition-expression "course_id=:id" --expression-attribute-values {\":id\":{\"S\":\"c002\"}} --profile dynamodbdev
aws dynamodb query --table-name course_data --key-condition-expression "course_id=:id AND course_date=:dat" --expression-attribute-values {\":id\":{\"S\":\"c002\"},\":dat\":{\"S\":\"2017/4/30\"}} --profile dynamodbdev -- return-consumed-capacity TOTAL
aws dynamodb query --table-name course_data --key-condition-expression "course_id=:id AND course_date BETWEEN :dat1 AND :dat2" --expression-attribute-values {\":id\":{\"S\":\"c002\"},\":dat1\":{\"S\":\"2017/4/30\"},\":dat2\":{\"S\":\"2017/4/30\"}} --profile dynamodbdev --return-consumed-capacity TOTAL
aws dynamodb query --table-name course_data --key-condition-expression "course_id=:id AND course_date BETWEEN :dat1 AND :dat2" --filter-expression "duration_in_days BETWEEN :d1 AND :d2" --expression-attribute-values {\":id\":{\"S\":\"c002\"},\":dat1\":{\"S\":\"2017/4/30\"},\":dat2\":{\"S\":\"2017/4/30\"},\":d1\":{\"N\":\"30\"},\":d2\":{\"N\":\"60\"}} --profile dynamodbdev --return-consumed-capacity TOTAL
Sample Output:
{
"Count": 1,
"Items": [
{
"course_id": {
"S": "c002"
},
"duration_in_days": {
"N": "40"
},
"course_date": {
"S": "2017/4/30"
}
}
],
"ScannedCount": 1,
"ConsumedCapacity": {
"CapacityUnits": 0.5,
"TableName": "course_data"
}
}
Note:
-
The count variable in output refers to actual items returned, scanned count refers to actual items read and billed. Items matching key and scanned and then filtered out of it.
-
Input can be partition key, or partition key with sort key, or a range of sort keys.
-
For no items, returns empty block.
-
Default is eventual consistency; can be changed by adding –consistent-read.
-
Can filter non key values; but any discarded values are still charged capacity wise.
-
Can be used against an index; local or global.
-
You can do a query from the console by going to the Items tab, selecting Query in the drop down and filling necessary fields.
Scan
aws dynamodb scan --table-name course_data --filter-expression "duration_in_days BETWEEN :d1 AND :d2" --expression-attribute-values {\":d1\":{\"N\":\"45\"},\":d2\":{\"N\":\"55\"}} --profile dynamodbdev --return-consumed-capacity TOTAL
Sample Output
{
"Count": 1,
"Items": [
{
"course_id": {
"S": "c003"
},
"duration_in_days": {
"N": "50"
},
"course_date": {
"S": "2017/4/30"
}
}
],
"ScannedCount": 3,
"ConsumedCapacity": {
"CapacityUnits": 0.5,
"TableName": "course_data"
}
}
Scan with NOT BETWEEN
aws dynamodb scan --table-name course_data --filter-expression "NOT duration_in_days BETWEEN :d1 AND :d2" --expression-attribute-values {\":d1\":{\"N\":\"45\"},\":d2\":{\"N\":\"55\"}} --profile dynamodbdev --return-consumed-capacity TOTAL
Scan with BETWEEN and NOT BETWEEN
aws dynamodb scan --table-name course_data --filter-expression "duration_in_days BETWEEN :d1 and :d2 OR NOT duration_in_days BETWEEN :d3 AND :d4" --expression-attribute-values {\":d1\":{\"N\":\"30\"},\":d2\":{\"N\":\"70\"},\":d3\":{\"N\":\"45\"},\":d4\":{\"N\":\"55\"}} --profile dynamodbdev --return-consumed-capacity TOTAL
Note:
-
BETWEEN and NOT BETWEEN operators has higher precedence over AND/OR. Read more here.
-
Input is table name without keys.
-
Filtering is possible, but still ALL DATA is read.
-
The count variable in output refers to actual items returned, scanned count refers to actual items read and billed. Here, scanned count will be equal to number of items in table.
-
Default is eventual consistency; can be changed by adding –consistent-read.
-
Scans can be done in parallel after splitting them into segments, for improving performance.
-
Query is always preferred over scan if possible.
-
You can do a scan from the console by going to the Items tab, selecting Scan in the drop down and filling necessary fields.
Recent comments