AWS DynamoDB

AWS DynamoDB #

DynamoDB Query with CSV output from CLI #

Requires jq to be installed

aws dynamodb query ⮠
  --table-name MY_TABLE ⮠
  --index-name MY_INDEX ⮠
  --key-condition-expression "myKey = :myKeyP" ⮠
  --filter-expression "begins_with(fNamee, :fNameP)" ⮠
  --expression-attribute-values "{\":fNameP\": {\"S\" : \"Nic\"}, \":myKeyP\": {\"S\" : \"SAMPLE\"}}" ⮠
  --projection-expression "#d,#data.attr1,#data.attr2,fName,lName,id" ⮠
  --expression-attribute-names "{\"#d\":\"date\", \"#data\":\"data\"}" ⮠
  --output json ⮠
  --region us-east-1 ⮠
  | jq ".Items[] |{date: .date[\"S\"], attr1: .data[\"M\"][\"attr1\"][\"M\"][\"S\"][\"S\"], id: .id[\"N\"], fName: .fName[\"S\"], attr2: .data[\"M\"][\"attr2\"][\"M\"][\"S\"][\"S\"]} | join(\",\")" -r

Find how many items are in a table #

aws dynamodb scan --table-name <TABLE_NAME> --select "COUNT"

Single Table Design #

Hands-on labs for Amazon DynamoDB #

https://amazon-dynamodb-labs.com/hands-on-labs.html

PartiQL: Referencing columns that are reserved works #

Use double quotes: i.e. “date”

PartiQL: Query Nested attributes example #

SELECT  "date", "data"."user"."M"."email"."S", "data"."details"."M"."address"."M"."street"
FROM "MyTable"."MyIndex"
where 
	"pkAttr" = 'val'
   AND "skAttr" >= '2022-08-08T06'
   AND "data"."details"."M"."address"."M"."country"."S" = 'US'

Import/Export data #

Import/Export data from DynamoDB (small amounts) by Vic’s StoryTime

Export

aws dynamodb scan --table-name source-table-name > data.json

Import

cat data.json | jq -c '.Items[]' | while read -r line; do aws dynamodb put-item --table-name destination-table-name --item "$line"; done